2015-04-17 24 views
0
public class ImagePreview extends JPanel { 

    private static final long serialVersionUID = 1L; 
    final float ratio = 1.0f; 
    private RenderedImage image; 

    public ImagePreview (int imgWidth, int imgHeight) { 

     this.setPreferredSize(new Dimension((int) (ratio * imgWidth) + 5, (int) (ratio * imgHeight) + 5)); 
    } 

    public ImagePreview (int imgWidth, int imgHeight, final RenderedImage image) { 
     super(); 
     this.setPreferredSize(new Dimension((int) (ratio * imgWidth) + 5, (int) (ratio * imgHeight) + 5)); 
     this.image = image; 
     repaint(); 
    } 

    @Override 
    public synchronized void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     VectorGraphics g2 = VectorGraphics.create(g); 
     if (image != null) { 
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      g2.drawRenderedImage(image, AffineTransform.getScaleInstance(ratio, ratio)); 
     } 
    } 
} 

調用此方法上的一個按鈕點擊 //調用此方法 ImagePreview購物=新ImagePreview(imgWidth,imgHeight,圖像);問題用的paintComponent()

我得到的圖像,但它沒有得到重新在面板上重繪。我無法在確定後面那個

+0

你正在使用VectorGraphics哪個第三方框架是這樣的?或者這是你的代碼? –

+0

是的,這是我的代碼,我正在使用import org.freehep.graphics2d.VectorGraphics; libary – user3507196

+0

您正在將RenderedImage傳遞給構造函數,可以顯示它是如何加載的? – Kuba

回答

1

的原因,但我只是想創建的圖像

預覽面板當你的風俗畫,你需要重寫面板的getPreferredSize()方法返回圖像的大小,否則大小是(0,0),所以沒有東西可以繪製。

VectorGraphics g2 = VectorGraphics.create(g); 

請勿使用第三方類發佈代碼。我們不知道問題出在您的代碼或課程上。

如果您需要更多幫助,請發佈適當的SSCCE來證明問題。