Painting with multiple threads

I am implementing a program that creates two instances of the same object called RobotComponent. RobotComponent uses the Java 2D Graphics2D context to draw to the screen.

However when I create the Frame and add two instances of RobotComponent using the following:

import java.awt.Frame;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import javax.swing.JFrame;

publicclass ProductionEmulation{

publicstaticvoid main(String[] args){

//create file readers to read CAN commands - temporary solution until JMS implementation

MyFileReader robotInFile =new MyFileReader("robInCommands.txt");

MyFileReader robotOutFile =new MyFileReader("robOutCommands.txt");

//create a frame to hold the cell components

JFrame frame =new JFrame("Plant Emulation");

frame.getContentPane().add(new RobotComponent(100, 400, robotInFile));

frame.getContentPane().add(new RobotComponent(100, 300, robotOutFile));

frame.setSize(500, 500);

frame.setLocation(500, 200);

frame.addWindowListener(closer);

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setVisible(true );

}

//Allow the window to be closed by implementing a listener

privatestatic WindowListener closer =new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

((Frame)e.getSource()).dispose();

System.exit(0);

}

};

}

Only the second instance shows on the Frame. Is this because creating two objects that use the paint method are overriding each other and this is why only the second object is being drawn?

If this is the case then what is the best way to go about this? Should I only have one thread that uses the Graphics2D context and have only one paint method?

Thanks in advance.

Ozzie

[3008 byte] By [ozzie_1984a] at [2007-10-3 7:58:22]
# 1

Try setting a layout manager on the frame's content pane, and make sure you add each RobotComponent at a different location. It seems like you are adding them on top of each other, so only the top-most component is showing. However, since we cannot compile your code, there is no certain answer to your problem.

jvaudrya at 2007-7-15 3:01:22 > top of Java-index,Security,Cryptography...
# 2

Thanks for your reply. Im pretty sure they aren't drawing over each other because I pass different parameters when they are created, ie.

frame.getContentPane().add( new RobotComponent(100, 400, robotInFile));

frame.getContentPane().add( new RobotComponent(100, 300, robotOutFile));

Ill try setting the layout manager for the frame but I think the problem could be because I'm using multiple threads that use the Graphics2D context. Do you know if this is actually the case?

ozzie_1984a at 2007-7-15 3:01:22 > top of Java-index,Security,Cryptography...