Little design issue
Im not very used to draw stuff with java but i will try to explain myself the better i can
Im using netbeans to develop this project and im using threads... i have a class named Main which is a JForm and one named Panel (which is a JPanel).
I use the panel to draw some stuff in the Jform.
Main handles another class which is threaded, and this class handles other classes that make very heavy calculations... and on one of this classes (its a static one) i have a method which i want to use to draw some stuff in the panel i mentioned earlier.
So, because its a static class, i tried to pass the Graphics object from the panel directly to this class, but anyway i wouldn't be able to call repaint() so the panel wouldnt update itself.
Panel -> Main(Jform) <- threadedclass <- static class
Im sure its an easy one, but im kind of stuck here.. so any help would be appreciated.
[930 byte] By [
ignusa] at [2007-11-27 5:38:48]

# 1
First read the [url http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html]tutorial[/url] and this [url http://java.sun.com/products/jfc/tsc/articles/painting/#swing]article[/url] to understand how painting is done in Swing.
The simplest solution is to use a BufferedImage as your canvas and draw onto it.
Then draw this BufferedImage in the paintComponent funtion of your Panel class, and finally call repaint on Panel class every time you draw some stuff.
# 2
I finally got it... but i dont think its a clean solution :P
My static class its like:
public class CalculosMapa {
public static BufferedImage buffer;
public static InterfazGrafica.Panel panel;
..........
public static void Renderea(Genoma mejorGenoma){
Graphics2D g = buffer.createGraphics();
.........
panel.buffer = buffer;
panel.repaint();
}
And my panel its like:
public class Panel extends javax.swing.JPanel {
public java.awt.image.BufferedImage buffer;
protected void paintComponent(java.awt.Graphics g){
super.paintComponent(g);
if(this.buffer == null){
CalculosMapa.creaCiudad(g);
int w = this.getWidth();
int h = this.getHeight();
CalculosMapa.buffer = (java.awt.image.BufferedImage)(this.createImage(w,h));
CalculosMapa.panel = this;
} else {
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(this.buffer, null, 0, 0);
}
}
Any help to simplify it would be appreciated :P thank you very much anyway!
ignusa at 2007-7-12 15:12:54 >
