Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Drawing an image

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3265
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3265
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a Swing Timer and then move the label by using:



 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic