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 购物车 = new ImagePreview(imgWidth, imgHeight, image);
我正在获取图像,但它没有在面板上重新绘制。我无法确定背后的原因
最佳答案
But i just want to create a preview panel for the image
当你进行自定义绘画时,你需要重写面板的getPreferredSize()
方法来返回图像的大小,否则大小为(0, 0),所以没有什么可画的.
VectorGraphics g2 = VectorGraphics.create(g);
不要使用第 3 方类发布代码。我们不知道问题是出在您的代码还是类上。
如果您需要更多帮助,请发布适当的SSCCE这说明了问题。
关于java - PaintComponent() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29700795/