New to this forum so this is my first post! I'm fairly new to Java game programming so I thought I would start myself off with a nice 2D game based around awt/swing components. I'm having problems calling the paintComponent method in one of my classes. I make a call to repaint() which makes a call to paint which then invokes paintComponent, if my understanding is correct. I therefore placed my paint code inside the overriden method paintComponent. However, when repaint is called, no call to paintComponent is made in the subclass, or parent class for that matter. I inserted break points into my code at the line where repaint is called and it the first line of my paintComponent method and then debugged the project in JBuilder which also confirmed the paintComponent method was not being called. Here is a copy of the class in question. As you can see it extends a class called GameWindow (which extends JPanel) which has its own paintComponent method.

I'm guessing this is probably a fairly straight forward question. I read the following article http://java.sun.com/products/jfc/tsc/articles/painting/index.html and I seem to be following everything that they say on here so I'm not sure what I'm doing wrong. The only way I can get it to work is if I put all this code into the GameWindow class but it's getting quite big and so is the paintComponent method in it so I would like to break the classes down into parts that all perform seperate actions of the game to make it easier to understand.

your calling hierarchy guess is correct... repaint() calls paintComponent() but only not instantly, as fast as it can...maybe your bDealing is false always? Put some output before it to check this and if paintComponent() really gets called, you don't need debugger...one more thing that could make paintComponent() not get called is that you never added it to a visible container... also check if repaint() really gets called by putting some output before it.

and nothing more. As for your point about a visivle container, well this could be where I have made the mistake. My assumption (probably wrong) is that GameWindow extends JPanel and therefore by DealCards extending GameWindow it would therefore be extending JPanel too since the paintComponent method works fine in the GameWindow class.

hello!I think you missed the MediaTracker to get synched with the loading of your bufferedImage. Plus I'd put the log for "repaint called", quoting your code, in the paintComponent() method which will better reflect to time when the painting is actually made.That is the point, each time you want to draw a new Image, add it to the MediaTracker and wait for it to load completely before to start rendering ! MediaTracker javadoc

The MediaTracker is not essential, all it does is ensure the image is loaded before it attempts to draw it and provides you with a way of prioritising the order in which images are loaded.

My problem is that the paintComponent method is not even being called. I have a line in the loadImage method which displays a line of output to my log file when an image is loaded, also in my loadImage method you can see the image is drawn and then dispose is called so this is not the problem.

My GameWindow class uses the same methodology, eg, same loadImage function with paintComponent method and this works fine.

Regardless of where I put my output lines, the fact is when I debug in JBuilder and step into every function called the paintComponent method is never called and as you can see from the output, the value of bDealer is true.

hmm... are you sure you are overriding it? Maybe method from super class has public modifier and not protected? Didn't ever tried that so I don't know if it matters.Well you said it already works for GameWindow so this doesn't matter...

Also, you cannot go into paintComponent() while debugging because paintComponent() is called from AWT-Swing thread (EDT?), it isn't called from repaint() in your thread. Repaint() only queues call to paintComponent() in Swing thread.

I don't know how we can help you anymore... make sure that it's visible (setVisible(true)) and that is added to a visible container.

If you don't solve it, instead if this build simplest JPanel with custom painting and try to implement it. If it works then build it little by little to your class.

In reply to Kova, the paintComponenent method in my super class is set to protected too. The JFrame state is visible and other calls to paintComponent from GameWindow are executed and drawn in the JPane.

I'd be surprised that your GameWindow doesn't paint at all if you were actually calling the repaint() in one main Thread, but I can distiguish that the start() method does invoke the Runnable that is defined with the run(). Then you must have the call to repaint() written in one of those 2 methods, which is obviously not the case. Would you check for that?

In reply to Kova, the paintComponenent method in my super class is set to protected too. The JFrame state is visible and other calls to paintComponent from GameWindow are executed and drawn in the JPane.

how about size? Did you set the size?edit: clarification, maybe you are using null layout manager and in that case you need to set size and position manually as I remember or they will be zero and paintComponent() wouldn't get called (?) ... try to do it manually

my main JFrame has the setSize attribute set, no setLocation but it renders fine....... as for the call to repaint in the start/run method, I tried placing it in there and still no joy.

I wasn't talking about JFrame, I was talking about your container and DealCards. Everything must be visible, put setVisible(true) just in case for every component. By default if you make JFrame visible then all children will be visible, but if you used some unstandard layout manager as null layout manager then you have to set sizes manually, I'm not talking about JFrame but for container.

edit: here's a simple example, if you uncomment any setVisible(false) or setLayout(null) the button will not appear

In the end I could only get it to work when I merged the DealCards class code with the GameWindow class code..... oh well. Now to have fun getting the cards to be dealt to the correct places on the table!

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org