我通过自己的类在 JFrame 中显示了 BufferedImage。我选择使用我自己的类显示 BufferedImage,这样我就可以缩放它。我的paintComponent和更新
public class MyBuffIm{
public void paintComponent(Graphics canvas) {
if (bi == null) {
} else {
//bi, maxWidth, and maxHeight were passed to constructor
canvas.drawImage(bi, 0, 0, maxWidth, maxHeight, null);
}
}
public void update(Graphics canvas) {
super.update(canvas);
if(bi != null){
//Got this from some tutorial in the net.
//Done out of desperation :|
paintComponent(bi.getGraphics());
}
}
}
我覆盖了更新,因为文档说的是“如果此组件不是轻量级组件,则 AWT 调用更新方法以响应对重绘的调用”。我不确定我的组件是否是轻量级的。
无论如何,我的 Runnable 中有以下代码(不按我的预期工作):
BufferedImage p = SomeMyBuffIm.getBuffIm();
Vector<Point> randomPixels = getRandomPixels(500);
int limit = randomPixels.size()
for (i = 0; i < limit; i++) {
Point rp = randomPixels.get(i)
p.setRGB(rp.x, rp.y, Color.red.getRGB());
}
SomeMyBuffIm.repaint();
mainFrame.repaint(); //JFrame call to repaint
我想,由于我正在缩放图像,所以我无法辨别新旧图像之间的差异。但我已经尝试了 getRandomPixels 的最大值仍然没有效果。顺便说一下,我的测试图像只是一张白纸,因此红色像素应该在其中突出。
我做错了什么吗?
最佳答案
I overrode update since the docs are saying something like "If this component is not a lightweight component, the AWT calls the update method in response to a call to repaint". I'm not exactly sure if my component is lightweight or not.
不,你不应该覆盖 update()。使用 AWT 可以做到这一点,但使用 Swing 则不行。
如果您更新 BufferedImage,那么您所需要做的就是在 MyBuffin 类的实例上调用 repaint() 。
如果您需要更多帮助而不是发布您的 SSCCE这说明了问题。
关于java - 更新 JFrame 中的 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059478/