Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to add an image to JFrame

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program that randomly displays different shapes every few seconds.
I was wondering how can i display a JPEG image every few seconds to?



this isn't all the code of the program just most of the main class
so do i just add a new case to the switch statement so it adds an image?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Jlabel and add it into your current pane.

 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call paintComponent() yourself. Don't give it public access: it should be protected.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ibrahim yener i tried your example but it didn't work. there was red lines under it. do i need to add something else for your code to work?

i tried this code (just added the 3 lines to the top of DrawPanel):


but it displays nothing when i run it. it stops the shapes from displaying
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote:Don't call paintComponent() yourself. . . .
You may need to call repaint() from your action however.
 
Nam Ha Minh
Ranch Hand
Posts: 508
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karen Wilson,

If you are using the Graphics object, you can draw an image like this:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic