GeeCON Prague 2014*
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


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding a slider to image display" Watch "adding a slider to image display" New topic
Author

adding a slider to image display

tony navaratnam
Greenhorn

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??

[code]
Graphics2D g1 = (Graphics2D) jLabel1.getGraphics();
g1.fillRect(0,0,500,500);
g1.setColor(Color.black);
g1.drawImage(scaledBI, 0, 0, scaledWidth, scaledHeight, null);
g1.dispose();
[code]
Michael Dunn
Ranch Hand

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

http://www.coderanch.com/t/338284/GUI/java/zoom-or-zoom-out-picture

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
Greenhorn

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
Bartender

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 ]

-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding a slider to image display