在Java中重新绘制BuffereImage不会更改面板的内容

在Java中重新绘制BuffereImage不会更改面板的内容,java,swing,bufferedimage,Java,Swing,Bufferedimage,长话短说,我正在使用一个BuffereImage绘制Mandelbrot,我把它放在一个定制的JPanel中。我已经在集合中进行了缩放,但在取消缩放时重新绘制有问题。取消缩放时,我将图像的值更改为图像的上一个状态(我将每个状态保留在堆栈中),然后重新绘制面板。问题是堆栈中的最后一个图像会弹出,但不会绘制 这些是我的实例变量 private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_AR

长话短说,我正在使用一个BuffereImage绘制Mandelbrot,我把它放在一个定制的JPanel中。我已经在集合中进行了缩放,但在取消缩放时重新绘制有问题。取消缩放时,我将图像的值更改为图像的上一个状态(我将每个状态保留在堆栈中),然后重新绘制面板。问题是堆栈中的最后一个图像会弹出,但不会绘制

这些是我的实例变量

    private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);  
    private Stack<BufferedImage> zooms = new Stack<BufferedImage>();  
    private boolean unzoom = false;  
最后这是我的绘画方法

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D  g2d= (Graphics2D) g;  
     //if it is not unzooming draw the mandelbrot set normally by  
      //dealing with every pixel of the Buffered Image separately  
    if (!unzoom)
    {
        for(int i = 0; i < SIZE; i++)
        {
            for (int j = 0; j < SIZE; j++)
            {
                int iterations = getIterations(cnSet[i][j]);
                if (iterations == noIterations)
                {
                    color = Color.BLACK;
                }
                else
                {
                    color = cols[iterations%noIterations];
                }
                image.setRGB(i, j, color.getRGB());
            }
        }
    }  
//zooming or unzooming always draw the image in its current state
    g2d.drawImage(image, 0, 0, this);
    unzoom = false;
}  
公共组件(图形g)
{
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
//如果未取消缩放,则绘制通常由
//分别处理缓冲图像的每个像素
如果(!取消缩放)
{
对于(int i=0;i
修复:结果证明,我不需要每次都保留最后一个图像并创建临时图像。现在,我只在堆栈中保留复杂平面的坐标。这就是我重新绘制旧图像所需的全部内容。

这:

private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
实例化新的
buffereImage
,并将对该对象的引用存储在
image

这:

将对创建的单个
buffereImage
的引用推送到堆栈上

只要您继续使用相同的
buffereImage
,您所做的就是将对同一对象的多个引用推送到堆栈上;因此,对对象数据的更改会反映在堆栈上放置的每个引用中,因为堆栈中的每个项都指向同一个对象

高级效果是每次渲染时都将以前的每个状态更改为当前状态

您需要为每个状态创建一个全新的
buffereImage
;因此,堆栈上粘贴的每个引用都指向一个唯一的对象


看看这个。

可能想检查您的鼠标滚轮方法是否被频繁调用,一个“滚动”可能是多个事件,可能会弹出所有图像。谢谢您的建议,但我已经检查过了。监听器会在需要调用的时候被调用,但重新绘制不会更新到图像的当前状态,而是保留原来的状态。哦,是的,你现在得到了答案,但另一件事情可能不是你想要的是,如果你在一行中有两个重新绘制(比如调整了窗口大小等等),您将进行第二次重新绘制,其中由于paintComponent中的最后一行,unzoom为false。最好不要修改paintComponent中的状态,因为您永远不能太确定何时会调用它。再次感谢您的建议。我会换的。非常感谢!这几天我一直在想办法。非常感谢。
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D  g2d= (Graphics2D) g;  
     //if it is not unzooming draw the mandelbrot set normally by  
      //dealing with every pixel of the Buffered Image separately  
    if (!unzoom)
    {
        for(int i = 0; i < SIZE; i++)
        {
            for (int j = 0; j < SIZE; j++)
            {
                int iterations = getIterations(cnSet[i][j]);
                if (iterations == noIterations)
                {
                    color = Color.BLACK;
                }
                else
                {
                    color = cols[iterations%noIterations];
                }
                image.setRGB(i, j, color.getRGB());
            }
        }
    }  
//zooming or unzooming always draw the image in its current state
    g2d.drawImage(image, 0, 0, this);
    unzoom = false;
}  
private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
zooms.push(image);