Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a Graphics image pass across a .jpg Icon image using a JSlider  RSS feed

 
Fred Hutchins
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to make a Graphics image (arrow) pass across a .jpg image(Balloon). The Graphics image is controlled by a JSlider. If you run this program you can see how the Graphics image moves. I commented out the code at Lines 61-64 and 74 that is giving me problems making the underlying ImageIcon. This involves getting both Images the same Frame so that one can pass over the other. I would appreciate your assistance.

Fred

balloon.jpg
[Thumbnail for balloon.jpg]
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to extend JLabel, instead of JPanel. Then you just use the image as the icon for the label and you customize the paintComponent() method to paint the arrow.

 
Fred Hutchins
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLabel was right on point. Thanks.

I started exploring other options with JLabel and found that you can also use the ImageIcon option within the paintComponent method and also specify the position of the icon.
Although it seems awkward having to right out the entire path.

 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should NOT be reading the Icon in the paintComponent() method. This method will be called whenever the component need to repaint itself.

My suggestion to use a JLabel was to take advantage of the default behaviour of how a JLabel paints an Icon. If you are going to do custom painting for both images then you should not extend JLabel, but instead extend JPanel or JComponent.

 
Fred Hutchins
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hutchins wrote:Although it seems awkward having to right out the entire path.

Loading Images Using getResource
 
Fred Hutchins
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I should be able to work this into the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!