Problems with buffering images.
Hallo
I'm trying to draw a picture in a new frame. The reason I'm usng graohics2D is that I need to draw 2D later on.
I'm overwriting the paint method but it seems like I'm not entering the method, even though I'm calling repaint().
Lars
Here is my code, which is meant to run in a thread:
package box;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
publicclass graphFormextends JFrameimplements Runnable{
BufferedImage bufferedImage;
Image buffer;//Picturebuffer
Graphics bufferGfx;//Reference to the picturebuffers drawingtool
int h = 200;
int w = 180;
public graphForm(){
newFrame();
}
publicvoid run(){
System.out.println("graphForm thread started");
while(true){
try{
Thread.currentThread().sleep(1000);
}
catch (InterruptedException ex){
}
upDataGraph();
}
}
/**
* upDataGraph
*/
privatevoid upDataGraph(){
System.out.println("repaint");
repaint();
}
publicvoid newFrame(){
//Create and set up the window.
JFrame frame =new MyFrame();
// Adds a empty label to the frame to give the frame a size
JLabel emptyLabel =new JLabel("");
emptyLabel.setPreferredSize(new Dimension(180, 200));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//Display the window
frame.pack();
// Set the window in middle of the screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
publicvoid paint(Graphics g){
System.out.println("paint");
Graphics2D graphics2D = (Graphics2D) g;
bufferedImage =new BufferedImage(180, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.setPaint(Color.black);
graphics.drawRect(4,5,179,199);
graphics.dispose();
g.drawImage(bufferedImage, 0, 0,this);
}
class MyFrameextends JFrameimplements ActionListener{
public MyFrame(){
// Super calls the parent constructor to set the frames name
super("Temperature");
JButton jButtonClose =new JButton("Close window");
jButtonClose.addActionListener(this);
// Close this frame when parent closes
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// Put the close button on the contentpane
Container contentPane = getContentPane();
contentPane.setLayout(new XYLayout());
contentPane.add(jButtonClose,new XYConstraints(20, 160, 140, 20));
}
publicvoid actionPerformed(ActionEvent e){
// When ButtonClose is pressed close the window and release ressources
setVisible(false);
dispose();
}
}
}

