#### Archived

This topic is now archived and is closed to further replies.

# [java] Java-Draw Image to JPanel

This topic is 5559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi... Im making an application that uses javax.swing and am having a problem with drawing an image... My main class extends JFrame, and all I add is 2 JLabels to the ContentPane of the JFrame. Then, I have
void paint(Graphics g)
{
g.drawImage(myBmp,20,20,this); //also tried this.getContentPane()

super.paint(g); //if I dont do this, my JLabel''s arent drawn

}

myBmp is loaded as follows: myBmp = Toolkit.getDefaultToolkit().getImage("my.bmp"); and is declared as an Image... Can anyone give me an idea as to why my image isnt drawn? I know the image is being loaded correctly, and its in the same directory as my app..its just not being drawn. Can I not draw to a JPanel or JFrame? Thanks, Lord Hen

##### Share on other sites
Hmmm...my
public void paint(...)
is overriding the main class''s paint event, and the main class extends a JFrame...

But I should be painting to a JPanel, right?
Does anyone know how I can override the JPanel''s paint method? the JPanel in question is returned by getContentPane()

Thanks,
Lord Hen

##### Share on other sites
its been a while for me but. Why not just set the jpanel icon to the image you want. That should work just fine.

-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document

##### Share on other sites
Well, you have to draw to the contentPane of the JPanel not the frame. That might be first problem. It could be your image is getting drawn over by the JPanel/JLabel, or whatever you are using. The second that would concern me is you are calling paint() from paint() which is going to recurse infinitely. Are you drawing to JLabels or JPanels? Either way, I''ve had better luck overriding JPanel.paintComponent() then JFrame.paint().

L-

" ''No one has control -- control is just a fantasy. And being human is difficult.'' "

##### Share on other sites
Just create another class which extentds JPanel and then add than panal to your frame..... if this is the effect you want that is......

something like this

 class whatever extends JPanel { Image toDraw; int x,y; public whatever(Image toDraw,Point p){  this.toDraw=toDraw;  x=p.x;  y=p.y; } public void paintComponent(Graphics g) {  Graphics2D graph=(Graphics2D)g;  graph.drawImage(toDraw,x,y,this); }}

or something similar to that....

[edited by - garazdawi on September 25, 2003 11:46:53 AM]

##### Share on other sites
All the swing questions of late are readily and easily covered in a beginner''s tutorial on swing. Look around folks, this is fundamental stuff.

try <a href="http://java.sun.com/docs/books/tutorial/uiswing/">this one</a> out.

##### Share on other sites
Hell I own the o''rielly swing and 2d books and have read them, but they both don''t talk too much about making extensive apps, that you draw on... its the drawing on them part that isn''t really all that cover... how to draw yeah... as longs as all your stuff is the same... and the thousand and one other things you try to do in conjunction with drawing on stuff.

And like Java Development is a really happenning thread... this place is hurting for good questions too be explored.

" ''No one has control -- control is just a fantasy. And being human is difficult.'' "

##### Share on other sites
Sorry -- actually that is great link -- wish I had it about a week ago. Just didn''t go to right source I guess.

##### Share on other sites
quote:
Original post by Lucidquiet
Well, you have to draw to the contentPane of the JPanel not the frame. That might be first problem. It could be your image is getting drawn over by the JPanel/JLabel, or whatever you are using.

You only use the content pane in JFrame, not in JPanel.
quote:
Original post by Lucidquiet
The second that would concern me is you are calling paint() from paint() which is going to recurse infinitely. Are you drawing to JLabels or JPanels?

He is calling super.paint(g), which is the paint of the super class.
quote:
Original post by Lucidquiet
Either way, I''ve had better luck overriding JPanel.paintComponent() then JFrame.paint().

He is right here. It is easier/better to subclass JPanel for custom drawing. Then you just override paintComponent. Also if you are not adding any components to the JPanel, you should not call super.paint(). On a side note, the order you call super.paint() in is significant. If you call it after all your painting code, you might just get your stuff overdrawn by everything the super class is going to paint.

First make it work,
then make it fast.

--Brian Kernighan

"The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were." - John Fitzgerald Kennedy(35th US President)

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013540
×