basically, I have a program that has a class to create a basic GUI, and another class that extends Canvas. This Canvas class is added to a JPanel in the usual fashion myPanel.add(object). Now, in this class, I want to have methods to move objects such as rectangles.
My questions are these; is there a way to essentially have this JPanel or its added object (myCanvas) running on a separate thread?
Also, in the following method:
public void paint(Graphics g){
g.setColor(Color.black);
g.drawRect(0, 0, 50, 50);
}
Is there a way to have these operations split into multiple methods? I.e. multiple methods that draw to the Canvas?
Thanks in advance.
Don’t use a
Canvasis a Swing applicaton. Use a JPanel or JComponent and override the paintComponent() method. Also don’t forget the super.paintComponent(g) at the start of the method.See the section in the Swing tutorial on Performing Custom Painting for more information.