2011-01-13 12 views
0

ImagePanel(JPanelを拡張)に描画したイメージのサイズを変更しようとしていて、動作していないようです。私は、コンポーネントのpaint()メソッドをオーバーライドするインターネットからいくつかのサンプルコードを入手しました。これは問題であるかもしれないと思っています。私のコードです。Java Swing:イメージ付きJPanelのサイズ変更

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if (img != null) { // Scale it by width int scaledWidth = (int)((img.getWidth() * getHeight()/img.getHeight())); 
     // If the image is not off the screen horizontally... 
     if (scaledWidth < getWidth()) { 
      // Center the left and right destination x coordinates. 

      int leftOffset = getWidth()/2 - scaledWidth/2; 
      int rightOffset = getWidth()/2 + scaledWidth/2; 
      g.drawImage(img, leftOffset, 0, rightOffset, getHeight(), 0, 0, img.getWidth(), img.getHeight(), null); 
     } 
     // Otherwise, the image width is too much, even scaled 
     // So we need to center it the other direction 
     else { 
      int scaledHeight = (img.getHeight() * getWidth())/img.getWidth(); 

      int topOffset = getHeight()/2 - scaledHeight/2; 
      int bottomOffset = getHeight()/2 + scaledHeight/2; 

      g.drawImage(img, 0, topOffset, getWidth(), bottomOffset, 0, 0, img.getWidth(), img.getHeight(), null); 
     } 
    } 
} 

と、私はこのblurpの幅と高さが適用される新しい幅と高さ...

public void resetImage(double width, double height) { 
    BufferedImage scaledImage = new BufferedImage((int)width, (int)height,BufferedImage.TYPE_INT_ARGB); 
    // Paint scaled version of image to new image 
    Graphics2D graphics2D = scaledImage.createGraphics(); 
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); 

    this.setSize((int)img.getWidth(), (int)img.getHeight()); 
    graphics2D.drawImage(this.img, 0, 0, (int)width, (int)height, null); graphics2D.dispose(); 
} 

(からそれを呼んでいる。

感謝。

+1

何が問題なのですか?何が起こると思いますか?私たちにもっと多くのものを与えてください。私たちは、あなたを助けるためにあなたの環境を再現するつもりはありません。 – I82Much

答えて

1

paintComponentというメソッドが呼び出されていますか?resetImageの末尾にあるImagePanel.repaint()を呼び出してください。

関連する問題