What is the best way to draw on JPanel in a background.

Hello all,

my task is to draw many objects, on the panel and then add names to each object. This may take a long time and should be done in background (thread).

What is the best way, to do it.

I have implemented this concept. If you try to resize the frame, the Thread should make some drawings. But nothing happends. If I put drawString method into paintComponent, I see result immediately.

I would appreciate any help.

If the windows was resized, the drawString method should start from the begining, because the position of the objects, and some other factors could be changed. So, this stops and starts of the thread.

If I use it in an unsafe manner, please let me know.

package test;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

import javax.swing.JPanel;

publicclass Painterextends JFrame{

privatestaticfinallong serialVersionUID = 1;

JPanel panel =new JPanel(){

privatestaticfinallong serialVersionUID = 1;

private ThreadPainter threadPainter;

@Override

protectedvoid paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

g2d.setColor(Color.gray);

g2d.fillRect(0, 0, 800, 600);

g2d.setColor(Color.white);

for (int i = 0; i < 6; i++){

Rectangle2D rec2d =new Rectangle2D.Double(10+i*100,10+i*100,10,10);

g2d.fill(rec2d);

}

//save current Graphics2D in attribute

currG2D = g2d;

//start thread to paint some changes later

if (threadPainter ==null){

threadPainter =new ThreadPainter();

threadPainter.start();

}else{

threadPainter.stopThread();

threadPainter =new ThreadPainter();

threadPainter.start();

}

}

};

private Graphics2D currG2D =null;

publicclass ThreadPainterextends Thread{

privateboolean stop =false;

@Override

publicvoid run(){

while (!stop){

//do some paintings and stop the execution

//of the thread, untill next repaint of the panel component

currG2D.setColor(Color.red);

int cnt = 0;

for (int i = 0; i < 6; i++){

currG2D.drawString("Square "+i, 10+i*100+20, 10+i*100);

System.out.println("count "+i);

//load data, do calculations

try{

//emulate calculation

Thread.sleep(1000);

}catch (InterruptedException e){

e.printStackTrace();

}

if (stop){

break;

}

cnt++;

}

if (cnt == 6){

stopThread();

}

}

};

publicvoid stopThread(){

this.stop =true;

};

};

public Painter(){

this.setLayout(new BorderLayout());

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocation(new Point(100,100));

this.setPreferredSize(new Dimension(800,600));

this.panel.setOpaque(true);

this.add(panel,BorderLayout.CENTER);

}

publicstaticvoid main(String[] args){

Painter painter =new Painter();

painter.pack();

painter.setVisible(true);

}

}

[6624 byte] By [flexeda] at [2007-11-27 9:56:26]
# 1

Java AWT/Swing GUI code uses their own special thread called EDT(event dispatch thread) and the Graphics object is only viable when the component's painting is in progress(in the paint() or paintComponent() method or in the code called from them).

Besides, after all, if it takes time it should take time. You should make a separate thread for your main app task that shuld not be hindered by GUI painting that might take long time. You can't delegate painting task to a separate thread due to the above reason.

hiwaa at 2007-7-13 0:26:33 > top of Java-index,Java Essentials,Java Programming...
# 2

Swing related questions should be posted in the Swing forum.

Don't understand what your are trying to do so I can't give specific help.

In general, I doubt you should be using Threads inside the paintComponent() method. If you want to change the state of your drawing then use a Swing Timer to schedule the repainting. This way you are in control of the animation.

The paintComponent() method is invoked the the RepaintMananger whenever a component needs to be repainted. That is if you minimize and restore the frame, so trying to control the animation from within the paintComponent() method will give wierd results.

camickra at 2007-7-13 0:26:33 > top of Java-index,Java Essentials,Java Programming...