How the paint method run without in main method? - java

Here, we have two classes. One of them is application class.
And the output is a jframe which has a rectangular inside. But I don't get it, we don't write paint method in main method. How can it run?

The paint method is automatically called to refresh your window. For example, the paint method is automatically called when resizing your window.
You can also force to call the paint method by calling the repaint() method of JFrame.
Also, if you're looking for the declaration of paint method, it is in java.awt.Component. Dont hesitate to have a look to the javadoc

Related

How to check who submitting repaint() event in Swing?

I have written a custom component that extends JPanel and overridden its paint() method. Now I can see that this method is called once per 10 milliseconds when the component is displaying. Nothing changes in the component but paint() is still called. I have several calls repaint() but none of them is called actually. How to know what is causing such frequent updates?
UPDATE!
There was "bug" in my code. I was updating inner components form paint() method so it was the root cause of continuous repainting.
But still, the question is not answered: how to understand what supplies events to the queue?
how to understand what supplies events to the queue?
Whenever a property of a Swing component is changed the component will automatically invoke repaint() on itself. The paint request is passed to the RepaintManager.
The RepaintManager will then consolidate multiple repaint requests into a single painting of all components. The consolidation is done to make painting more efficient.
So the individual component that made the request is not available because in many cases multiple components will make the repaint request at the same time.
You can read Painting in AWT and Swing for a more detailed explanation.
using debug
make a break point in the paint() function
and when it called you can watch the stack trace of the call back

Overloading paintComponent()

I'm currently making a basic connect four game with a GUI and I'm trying to figure out how I can place the pieces on the board. I have a method that can locate which space the user would like to put the piece on, but I'm unsure how to paint the pieces in. Since paintComponent() is always called without an actual method call and the default constructor only takes a Graphics object, how could I overload paintComponent() so that it can take different arguments (these arguments are the location of the piece) and the compiler will know to call my new paintComponent() method?
how could I overload paintComponent() so that it can take different arguments
You can't. Or rather, you can create an overloaded method, but the JRE won't call it.
Instead, the code needs to keep a model of the game state, when it changes, call repaint(). In the normal paintComponent(Graphics) method, use the model to determine how it should be drawn.
Obviously, for this to work, the model must be within the scope of the method - so you might make it a class attribute.

Why do I have to use setvisible() on my JFrame when I change components?

So for the sake of simplicity I set up a little test code just to figure out this problem. Basically I have a JFrame and I added 'this' to it (I just extended my main class from JComponent to save time). this component fills in a red background. Then I have it sleep for 2 seconds and then type this.
f.remove(this);
thing t = new thing();
f.add(t);
f.setVisible(true);
f being my JFrame object and 'thing' is just another class extending JComponent that paints a blue background..
when I comment out setvisible() it no longer changes to blue.. I've tried using t.setVisible(true) but it seems I have to make the frame visible again, not the component
does anyone know why I have to call that... or if there is another way to change components within a single frame?
"Basically I have a JFrame and I added 'this' to it (I just extended my main class from JComponent to save time). this component fills in a red background. Then I have it sleep for 2 seconds and then type this."
Don't "sleep" your program. Instead use a java.swing.Timer to perform repeated tasks on the GUI or for animation. See more at How to Use Swing Timers. You can see a bunch of Timer examples here and here and here and here and here
Instead of trying to add and remove panels look into using a CardLayout which allows you to switch between views. It will help you avoid a lot of problems that come with with adding and removing components/containers. See more at How to Use CardLayout. Also see a simple example here.
To answer your main question, whenever you remove and add components from your frame, you need to revalidate() it. setVisible() takes care of that for you.
Side Note
Seems like a lot adding an removing background panels) just to change the background. Why not just setBackround()? You can switch colors with the use of the Timer
Calling setVisible(true) makes the frame appear onscreen. Sometimes you might see the show method used instead. The two usages are equivalent, but we use setVisible(true) for consistency's sake.

Should I override frameInit for initialization code in a Swing GUI?

I noticed that JFrame has a frameInit() method. I'm building a very simple GUI that extends JFrame. The description for the method is:
Called by the constructors to init the JFrame properly.
If I have initialization code (such as setting the title, size, location, etc.), should I override the method, and put my initialization code in there? If not, where should I put initialization code (still assuming that I am extending JFrame, not using composition)?
I guess the best place to put this stuff is in the constructor. Or, if you do overload, don't forget to call super.frameInit().

Overriding the paint() method in JButton

I have a class that extends JButton because the custom look and feel I'm using ignores the isOpaque() call. What I mean is, the background of the button is rendered even though I have called setOpaque (false) on it and all parent panels. I have confirmed that this is an issue with the LAF from the companies design people so there is nothing I can do but extend the class.
So my question is, how can I implement the paint() method to not render the background and just the button icon?
Cheers
SOLVED: The answer I was after in case anyone is interested was to use button.setContentAreaFilled(false);
Painting is done by three methods: paintComponent, paintBorder, and paintChildren. They're called in that order and it's the first that paints the component's background. If you overload that one and leave the other two, you should be fine.

Categories

Resources