|
-
Reset scrollbar position with Jscrollpane and image
I have routine that has a jscrollpane that allows me to scroll the image drawn to a panel. I'd like to be able to reset the postion of the scrollbars to the upper-left when the drawing is changed.
This is the routine:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
class ImagePanel extends JPanel
{
// Comment for <code>serialVersionUID</code>
private static final long serialVersionUID = 1L;
private BufferedImage image;
private JScrollPane scrllPn;
int px_size = 0;//1500;
int py_size = 0;//1500;
Container cp;
//
public ImagePanel(Container cp_in, int px_size_in, int py_size_in)
{
cp = cp_in;
if (px_size_in > px_size) px_size = px_size_in * 5;
if (py_size_in > py_size) py_size = py_size_in * 5;
scrllPn = new JScrollPane(this);
cp.add(scrllPn, BorderLayout.CENTER);
setOpaque(false);
}
public ImagePanel(int px_size_in, int py_size_in)
{
if (px_size_in > px_size) px_size = px_size_in;
if (py_size_in > py_size) py_size = py_size_in;
setOpaque(false);
}
public ImagePanel()
{
setOpaque(false);
}
public ImagePanel(BufferedImage image)
{
setOpaque(false);
this.image = image;
}
public void setImage(BufferedImage image)
{
this.image = image;
repaint();
}
public BufferedImage getImage()
{
return image;
}
public Dimension getMinimumSize()
{
return new Dimension(px_size, py_size);
}
public Dimension getMaximumSize()
{
return getMinimumSize();
}
public Dimension getPreferredSize()
{
return getMinimumSize();
}
public void paint(Graphics g)
{
super.paint(g);
}
public void paintComponent(Graphics g)
{
if (image != null)
{
int iw, ih;
int ow, oh;
int pw, ph;
int dw, dh;
int plw, plh;
int rounder = 25;
// fetch zoom settings (settings are Auto Zoom/User Zoom -- 300,200,150,100,75,50,25
String zoom_type = GlobalParms.sget_gp("DISPLAY ZOOM TYPE");
float zoom = GlobalParms.iget_gp("DISPLAY ZOOM") / 100.0f;
// get size of display panel
plw = GlobalParms.iget_gp("panel_x");
plh = GlobalParms.iget_gp("panel_y");
// set offset from upper-left corner as the 0,0 for drawing
ow = 10;
oh = 10;
// get the image size
iw = image.getWidth();
ih = image.getHeight();
Dimension ipxy = getSize();
// compute ratio of image to screen
float ratio = Math.min((float) plw / (float) iw, (float) plh
/ (float) ih);
float scale = ((int) (ratio * 100.0 + 0.5) / rounder * rounder) / 100.0f;
// set up image scale info
System.out.println(" * " + scale + " " + zoom + " " + zoom_type);
if (zoom_type.equals("Auto Zoom")) scale = scale * zoom;
if (zoom_type.equals("User Zoom")) scale = zoom;
//
dw = (int) (iw * scale + 0.5);
dh = (int) (ih * scale + 0.5);
g.drawImage(image, ow, oh, dw + ow, dh + oh, 0, 0, iw, ih, null);
g.dispose();
revalidate();
}
}
}
Thanks,
RON C
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks