This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding a slider to image display

 
tony navaratnam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really meant panning across the image rather than zooming. can this be done the same way?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic