Jump to content
  • Advertisement

Archived

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

Lord Hen

[java] Java-Draw Image to JPanel

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!