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

