• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to add images dynamically to jpanel?  RSS feed

 
Punit Jain
Ranch Hand
Posts: 1028
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, i added some images to jpanel, with some animation.
i put some images in a folder, and i m display/moving them on jpanel simultaneously.
my question is can i add images dynamically??
i mean what i want is, i want to add a button on that panel, and when i click on that button, one more image (randomly) should added to my jpanel from that folder.
how can i do this??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code to add the image is identical to
how you'd add it when the frame first opens.

because the frame is now visible, after
adding it you need to have these lines

panel.add(image);//however you add it
panel.revalidate();
panel.repaint();
 
Punit Jain
Ranch Hand
Posts: 1028
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how even i add add those images on button click one by one, i mean how i get images randomly from my image folder??


above is what i am doing right now.
 
Randall Twede
Ranch Hand
Posts: 4589
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to use Math.random()
check the API and tutorials
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:you need to use Math.random()

Or java.util.Random
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> i mean how i get images randomly from my image folder??

another option, at program startup, is to load them all into a List,
then Collections.shuffle(list) and add them to your panel in the
'shuffled' order of the list.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!