2012-05-22 48 views
2

我在設計一個gui,它在面板上顯示圖像,並有一個滑塊,可以放大和縮小圖像。但問題是:我的圖像變得放大,但不顯示滾動窗格內的圖像Scrollpanegui在java中的設計問題

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(); 

代碼對於imagePanel類的paintComponent方法

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); 
+0

向我們展示了ImagePanel的代碼。你的問題可能在那裏或在圖像路徑 –

+0

他是一個新用戶,也許他問了很多問題,但他們還沒有提供任何好的(或任何?)答案? – Nealbo

+0

幾乎所有的問題都是非常基本的,只有一個接受。 –

回答

3

因爲JScrollPane依賴於客戶的首選大小,所以您可能需要以反映縮放大小的方式覆蓋ImagePanelgetPreferredSize()方法。

0

它不應該是:

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

如果沒有,你真的試圖直接從類調用,那麼不該它是:

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

add()和validate()方法應該是靜態的,否則你應該使用第一個例子。