How to have multiple classess paint themselves using paintComponent()

An example is given in pseudocode. They key thing is that I want my Main class to create a frame and a JPanel.

In the same file as the Main class is a drawForMain class, which uses JComponent.paintComponent() to draw some custom graphics.

What I want to do now, is create ie 10 Node classess, where each of these nodes can draw themselves using JComponent.paintComponent() as well. The problem I get is that 8 out of 10 runs, drawForMain.paintComponent() is never executed at all.

So how can I booth have drawForMain() class paint itself, as well as add several Nodes that also paint themselves?

publicclass Main{

drawForMain draw;

Node node;

JFrame frame =new JFrame;

JPanel panel =new JPanel;

publicstaticvoid main(String[] args){

// CreateAndShowGUI();

// ....

draw =new drawForMain();

panel.add(draw);

draw.setVisible(true);

node =new Node();

panel.add(node);

node.setvisible(true);

}

}

class drawForMainextends JComponent{

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

// paint stuff

}

}

publicclass Node extrends JComponent{

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

// paint stuff

}

}

[2424 byte] By [Toxica] at [2007-11-27 0:51:50]
# 1
I'm addicted to you...
tjacobs01a at 2007-7-11 23:22:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
Well, based on the code provided it would appear the components have a size of 0. You need to set the preferred size of your component when you do custom painting.
camickra at 2007-7-11 23:22:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

Perhaps I wasn't clear enough. This is only pseudo code, where I've only added enough code to demonstrate my problem.

In the real program, I have everything working, but the fact that the Node-class paintComponent() does seem to override the drawForMain-class paintComponent() at times (not always, this is very random).

A backup plan of mine, is to have the drawForMain-class to be the only class with a paintComponent, then it manually makes calls to all the nodes to get them to paint as well. This doesn't strike me as the best way to get it done though.

Toxica at 2007-7-11 23:22:56 > top of Java-index,Desktop,Core GUI APIs...
# 4

Well I've never seen paintComponent(...) not paint when it should. So you have a problem with your code. I'm not a mind reader and I don't have any more guesses.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-11 23:22:56 > top of Java-index,Desktop,Core GUI APIs...
# 5

I haven't been able to reproduce the error in a small program yet. However I noticed that having booth Node and drawForMain being statically nested classess, the drawings does not seem to show at all. I thought a static nested class was the same as an external newclass.java, only it shares the source-file with another class.

I think I managed to figure the problem out. I need to give it a few more testruns, but I believe that the drawForMain and Node class was added to a JPanel, without specifing BorderLayout.DIRECTION, as BorderLayout was the set layoutmanager. What's odd though is that from times it worked, then the next time I launched the exact same code it didn't work

Message was edited by:

Toxic

Toxica at 2007-7-11 23:22:56 > top of Java-index,Desktop,Core GUI APIs...