Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes adding a slider to image display Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding a slider to image display" Watch "adding a slider to image display" New topic

adding a slider to image display

tony navaratnam

Joined: Dec 27, 2006
Posts: 14
Iam opening an image into a jLabel using the get graphics/drawimage method.
how can i add sliders to this label so that if an image bigger than the canvas is opened you can use the sliders to view it all??

Graphics2D g1 = (Graphics2D) jLabel1.getGraphics();
g1.drawImage(scaledBI, 0, 0, scaledWidth, scaledHeight, null);
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you'll find a zoom example here

swap the example's JSpinner for your JSlider, and it should do what you want.

> Graphics2D g1 = (Graphics2D) jLabel1.getGraphics();

never use getGraphics(), always override paintComponent() and do your graphics there
tony navaratnam

Joined: Dec 27, 2006
Posts: 14
I really meant panning across the image rather than zooming. can this be done the same way?
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You could just put your JLabel in a JScrollPane...

Although since you're doing painting through getGraphics() it's all temporary - when the scrollpane repaints the JLabel (when you scroll around) the image won't be displayed any more.

You should be loading the image into an ImageIcon and setting the icon on the JLabel - then it will be a permanent change to the JLabel and will survive repaint events.

You would just add the scrollpane to the same location you are currently adding the label to. If the buffered image reference changes, you'll need to create another ImageIcon of it and re-set the new ImageIcon on the JLabel using jLabel1.setIcon(new ImageIcon(scaledBI)); However, if you're just painting to the buffered image's graphics context - through scaledBI.getGraphics() - the buffered image reference stays the same and you won't have to re-set the image icon.
[ March 07, 2007: Message edited by: Nathan Pruett ]

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: adding a slider to image display
It's not a secret anymore!