• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding a slider to image display

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you'll find a zoom example here

https://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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really meant panning across the image rather than zooming. can this be done the same way?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic