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();

}

}

}

[5502 byte] By [lars2005a] at [2007-10-1 23:31:29]
# 1

You have two definitions of a frame here, one is graphForm and the other is MyFrame. You do your custom painting in graphForm but the graphForm frame is not visible - you're only displaying the instance of MyFrame. I'm guessing what you really want is one frame, which is graphForm, that contains the close button and the custom painting code?

Jasprea at 2007-7-15 14:14:17 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes, thats what I want.How do I do that? - I mean: How do I do that the right way?
ad87geaoa at 2007-7-15 14:14:17 > top of Java-index,Java Essentials,Java Programming...
# 3

> Yes, thats what I want.

>

> How do I do that? - I mean: How do I do that the

> right way?

Well, typically what you want is to have a single frame with multiple panels (see javax.swing.JPanel). One panel would be where you do your custom painting and other panels would contain your close button, or a toolbar, etc.. The reason is that if you override the paint method of your frame, the frame's children won't be painted. Here's an example: import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class PaintTest extends JFrame implements Runnable {

private JPanel toolbar;

private PaintPanel paintPanel;

public PaintTest() {

super("Test");

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JButton button = new JButton("Close");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dispose();

}

});

toolbar = new JPanel(new FlowLayout());

toolbar.add(button);

paintPanel = new PaintPanel();

JPanel content = new JPanel(new BorderLayout());

content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

content.add(new JLabel("Paint Panel Below:"), BorderLayout.NORTH);

content.add(paintPanel, BorderLayout.CENTER);

content.add(toolbar, BorderLayout.SOUTH);

setContentPane(content);

pack();

Thread t = new Thread(this);

t.start();

}

public void run() {

while (true) {

try { Thread.sleep(30); }

catch (InterruptedException e) {}

paintPanel.repaint();

}

}

private class PaintPanel extends JPanel {

int x;

int dx = 5;

public PaintPanel() {

setPreferredSize(new Dimension(500, 500));

}

public void paintComponent(Graphics g) {

// background

g.setColor(Color.black);

g.fillRect(0,0,getWidth(),getHeight());

// box

g.setColor(Color.red);

g.fillRect(x,100,50,50);

x += dx;

if (x < 0 || x + 50 >= getWidth())

dx = -dx;

}

}

public static void main(String[] args) {

PaintTest t = new PaintTest();

t.setLocationRelativeTo(null);

t.setVisible(true);

}

}

Jasprea at 2007-7-15 14:14:17 > top of Java-index,Java Essentials,Java Programming...