2

I m designing a gui which displays an image on a panel and has a slider which zooms the image in and out. But the issue is: My image gets zoomed but does not show the image inside the scrollpane Scrollpane.

ImagePanel imageP = new ImagePanel("D:/ScannedImage1.jpg");
JSlider slid = (JSlider) evt.getSource();
float value = (float) slid.getValue();
imageP.setScale(value);
imagePanel.add(new JScrollPane(imageP), BorderLayout.CENTER);
imagePanel.validate();

code For imagePanel class paintComponent Method

super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int w = getWidth();
int h = getHeight();
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
double x = (w - scale * imageWidth) / 2;
double y = (h - scale * imageHeight) / 2;
AffineTransform at = AffineTransform.getTranslateInstance (x, y);
at.scale(scale, scale);
g2.drawRenderedImage(image, at);
4

2 回答 2

3

因为JScrollPane依赖于客户的首选大小,您可能需要以反映缩放大小的方式覆盖getPreferredSize()方法。ImagePanel

于 2012-05-22T14:59:07.297 回答
0

不应该是:

imageP.add(new JScrollPane(), BorderLayout.CENTER);
imageP.validate();

如果不是,并且您确实试图直接从课堂上调用,那么不应该是:

ImagePanel.add(new JScrollPane(), BorderLayout.CENTER);
ImagePanel.validate();

并且 add() 和 validate() 方法应该是静态的,否则你应该使用第一个例子。

于 2012-05-22T13:23:15.190 回答