• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing an image

 
richard rehl
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I feel really stupid... What's the easiest way to put an image on a JPanel (or whatever)?

I've imported .awt and javax.swing, have declared an ImageIcon, have a JFrame setup with a JPanel, have added the JPanel to the JFrame, have set the visibility to true... just can't figure out how to add the damn ImageIcon. Should I be using a BufferedImage instead? Trying to declare a BufferedImage gets me a "BufferedImage can't be resolved to a type" message in Eclipse.

Thanks for your help, hope I gave you enough info.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the Icon to a JLabel and add the label to a panel.

Read the JLable API and follow the link to the Swing tutorial on "How to Use Labels" for a working examples. There is also a section in the tutorial on "How to Use Icons".
 
richard rehl
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that worked, thanks. Can I animate (move) that image or do I have to use some sort of drawImage method instead to do that?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a Swing Timer and then move the label by using:



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!