wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes displaying buffered image on jpanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "displaying buffered image on jpanel" Watch "displaying buffered image on jpanel" New topic
Author

displaying buffered image on jpanel

son dunckley
Greenhorn

Joined: Jul 27, 2005
Posts: 17
Hi

Hope someone can help as I have been struggling with this for days..


I have created a graphics2D environment on the buffered image and now my understanding is I should be able to 'draw' on the image?

I have the code as follows:

// creating a blank image
displayImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = displayImage.createGraphics(); // creating 2D graphics environment to represent buffered image as drawing surface
//g2.setPaint(Color.darkGray); // set backgroumd image to darkgray this has no affect ??
//btd.drawWholeTree(g2);
treeLabel = new JLabel(new ImageIcon(displayImage));

//treeLabel.setForeground(Color.darkGray);
binaryDisplayPanel.add(treeLabel);
}

as you can see I am trying to change the background of the label(currently defaulting to black) to dark grey.

I then want to add on top of that my tree - which is done via a method call btd.drawWholeTree(g2); - which I call from another class.

However, none of these changes have made a difference - can anyone help me along the right lines with the code I am trying to use?

- how I can change the background?
- how do I add my drawing to the image?

I have also attempted to create an update image method, as follows as wondering if I need to repaint the image for the tree and new background to appear?

public void updateImage()
{
displayImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = displayImage.createGraphics();
btd.drawWholeTree(g2);
g2.setPaint(Color.darkGray);
treeLabel.setIcon(new ImageIcon(displayImage));
}

I then call it in the init method below:

public void init()
{
btd = new CopyBinaryTreeAction();
btd.generateTree(); // call generateTree method from BinaryTreeAction to setup tree ready for paint
createGUI();
updateImage();


}

or should I be doing this in the paintComponent:

public void paintComponent(Graphics g)
{
super.paintComponents(g);

Graphics2D g2 = (Graphics2D)g;
// updateImage();
// repaint();

}

As you can see, slightly confused with all this. I am sure I am missing something really simple to get my code working but cannot find the right advise anywhere.

Or if someone can tell me how to draw my tree image(via btd.drawWholeTree) directly onto my JPanel that would be great.

I am creating this as an applet if that helps.


Hope someone can help
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Dude, that's a mess of unformatted code. It's kind of hard to look at. Try editing your post and adding UBB Code Tags around your code blocks so that they are easier to read. Once you've done that I bet someone will try and help you out.


GenRocket - Experts at Building Test Data
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
son dunckley
Greenhorn

Joined: Jul 27, 2005
Posts: 17
Thanks very much for the replies. Will have a look at your example in a bit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: displaying buffered image on jpanel
 
Similar Threads
Wanted: Java2D example of zooming and panning with scroll bars
controlling size of part of split pane and another error
V2 - Problem with Image & Browse file.
repaint() problem
Alignment of JTextArea in OverlayLayout