Displaying an image on a Jpanel
Hi
I have created some panels on one of them I would like to display an image of a BinaryTree which I have created but I don't know where to begin really as I am not sure which of my classes I should start adding the code to. Eventually I will want the image to only appear on the panel, when certain buttons are clicked.
I have three Classes as follows:
TreeNode - which handles all the tree such, such as drawing the elipses and lines, inserting new nodes
BinaryTreeAction - which calls the methods in the TreeNode class start generating trees.
BinaryTreeDisplay - which handles all the gui, and calls methods from TreeNode class via BinaryTreeAction.
I was hoping to create a new label for example treeNodeLabel - get the image onto the label, as a buffered image, and then place this label onto the correct JPanel - once the button is selected.
Can anyone help me out as to where to start adding some code - I thought it was in the BinaryTreeDisplay but it couldn't resolve all the code I added and so I didn't know if I was in the right class or not. I declared the label OK, but then I didn't know where abouts to put the buffered image line - would this go in its own method or in the init method?
Completely lost with this :confused: hope someone can help.
If I have not provided enough information, or you need to see some code, please let me know.
Look forward to some help
Thanks
[1496 byte] By [
JavaSado] at [2007-11-11 6:28:50]

# 1 Re: Displaying an image on a Jpanel
I have always found the GUI of a program to be the most challanging part of any program. Any custom aspect of painting that I have to to considerable lengthens the time it takes me to create a program. This being said, I hope you do not have a very short time limit to complete this program, as I know it would take me a long time, but I do have a suggestion for a solution:
First, I am assuming that you are just going to have a black rectangle with text in it desplaying the node's name, and having a simple line connecting two nodes. With this being said, you have to know how large each of the rectangles will be so that you can accurately map out the location of every node to be evenly spaced and visible in the panel. This being said, I think it would be best to create a seperate class called TreeNodeBox or something, and have them store what level they are in the tree what its name is (and anything else you might want to know about it, i.e. its size), and find some way to store it in your display class so that they are in order. Once the class knows how to figure out its dimensions, you can then use it to figure out how to space everything out, and finally, use the information to draw the rectangles, text, and lines on the JPanel.
I hope this is helpful, and if you need any more help, I will try to continue watchin this thread.
Zodoz at 2007-11-11 22:41:19 >

# 2 Re: Displaying an image on a Jpanel
Hi Zodoz
Thanks for the reply. I have everything in place for the nodes, just a bit of tidying up so they display the tree perfectly but I'm working on that slowly!
My main problem is I now need to display the tree, however imperfect the image is at the moment, on my JPanel as soon as I can. I just didn't know which class to start trying to add the buffered image to?
Shall I try adding it to the BinaryTreeDisplay - where all the buttons and panels etc.. are? Or should I also add something in the TreeNode Class - where all the working out of the tree dimensions etc is going on?
I can e-mail my classes to you if that would help?
And, yes unfortunately short on time with this as I have been spending most of my time on the trees displaying nicely :(
Thanks for any further input
# 3 Re: Displaying an image on a Jpanel
Hello
I have got a bit further, I hope.
I have now started adding the code to the main class, as I am sure this is where I should be starting.
I have added the following lines to the declaration:
private BufferedImage displayImage; // to display tree
private javax.swing.JLabel treeLabel;
Then in my createGui method I have:
// creating a blank image
displayImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
treeLabel = new JLabel(new ImageIcon(displayImage));
binaryDisplayPanel.add(treeLabel);
It now displays the black label on my Jpanel for me, not quite in the right place and has moved my other label - but am sure can work this out.
Now need to add my tree from my TreeNode class - if anyone has any pointers that would be great, otherwise if I work it out myself I will post the code in case it helps others with similar queries.
Bye for now
# 4 Re: Displaying an image on a Jpanel
well, it might help if you knew how to do level (bredth) order traversal. I believe this code will work:
/**
*pre: root is not null
*/
public Queue bredthOrder(TreeNode root) {
Queue q = new Queue(); // I assume you can use a queue class
Queue reQ = new Queue(); // return Queue
q.enqueue(root);
TreeNode tempNode;
while(!q.isEmpty()) {
tempNode = (TreeNode)q.dequeue();
reQ.enqueue(tempNode);
if(tempNode.getLeft()!=null) {
q.enqueue(tempNodes.getLeft());
}
if(tempNode.getRight()!=null) {
q.enqueue(tempNodes.getRight());
}
}
}
When you use the Queue, you might want to store that new class I was talking about so that you can tell what level it is on when you place your images/labels. I would also recomend that this class extend JPanel so that you are able to draw right on this class and create the graphics there. The JPanel should be able to set its own size and draw itself. Once you have them drawing themselfs, then all you have to do is place them in a specific location in your main GUI container. If you manage this, you will not have to worry about double buffering because the JPane will never update itself (unless you change the name), and will be automatically double buffered (this is from my experience when I created a GUI for a simple UML using this idea).
Tell me how this works for you, and if I did not answer all your questions, I am sorry. Just ask again, and I will try best to answer them in my next post.
Zodoz at 2007-11-11 22:44:22 >

# 5 Re: Displaying an image on a Jpanel
Hi
Thanks for the response.
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 :SICK: with all this. I am sure I am missing something really simple to get my code working but cannot find the right advise anywhere.
Hope someone can help and thanks for your reply zodoz
# 6 Re: Displaying an image on a Jpanel
I do not know what classes each of these methods are in, so, if you are tyring to paint everything in one JPanel, then you need to be sure to send the SAME Graphics2D to each paint method that you are using in everything that you want to paint inside the JPanel. On the other hand, if you were taking my advice from before and making them seperate JPanels, then you would only need to have a pain method in each JPanel to paint itself, and simply add the JPanel object into the main JPanel, and it will update itself without you having to worry about double buffering.
More information/link to a zip file containing the classes would be helpful if this post is not.
Zodoz at 2007-11-11 22:46:19 >

# 7 Re: Displaying an image on a Jpanel
Hi Zodoz
Thanks for your reply.
Have managed to get it working now!!
I was basically not repainting the image - hence my new image of the tree was not being added. I created a new method called updateImage - and this is now called when a button is clicked. So initially the image of the black label is displayed and as soon as someone selects the 'start search' button the updateImage is called and the tree is displayed.
In case it is helpful to others, and so you can see what I wasn't explaining vrey well, I have zipped it up for you.
Thanks again - appreciate all the advise I get ;)