File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying buffered image on jpanel

 
son dunckley
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Wood
Ranch Hand
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
son dunckley
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic