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 cart = new ImagePreview(imgWidth,imgHeight,ImagePreview);
我正在得到图像,但它没有在面板上重新绘制。我不能确定那背后的原因
发布于 2015-04-17 23:00:56
,但我只想为图像创建一个预览面板
在进行自定义绘制时,需要重写面板的getPreferredSize()方法以返回图像的大小,否则大小为(0,0),因此无需绘制任何内容。
VectorGraphics g2 = VectorGraphics.create(g);不要使用第三方类发布代码。我们不知道问题出在你的代码还是类上。
如果你需要更多的帮助,发布一个正确的SSCCE来说明这个问题。
https://stackoverflow.com/questions/29700795
复制相似问题