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

Pet*_*rMI 2 java swing bufferedimage

长话短说我正在使用BufferedImage绘制Mandelbrot,我将其放入自定义JPanel中.我已经完成了设置的缩放,但是在重新连接时有重新绘制的问题.当unzooming我将图像的值更改为图像的先前状态(我保持堆栈中的每个状态)并重新绘制面板.问题是堆栈中的最后一个图像被弹出但是没有绘制.

这些是我的实例变量

    private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);  
    private Stack<BufferedImage> zooms = new Stack<BufferedImage>();  
    private boolean unzoom = false;  
Run Code Online (Sandbox Code Playgroud)

这就是我如何缩放和推送我想要保存在堆栈上的图像

public void mouseReleased(MouseEvent e)
{
    zooms.push(image);
    <some code for zooming that works>
    repaint();
}  
Run Code Online (Sandbox Code Playgroud)

现在我想通过滚动来取消缩放

class WheelZoomListener implements MouseWheelListener
{
    public void mouseWheelMoved(MouseWheelEvent e) 
    {
        unzoom = true;  
    //this is how I assign the current image to be the one before the last zoom
        image = zooms.pop();
        repaint();
    }
}  
Run Code Online (Sandbox Code Playgroud)

最后这是我的绘画方法

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;
}  
Run Code Online (Sandbox Code Playgroud)

FIX:事实证明,我不需要保留最后一张图像并且每次都创建临时图像.相反,现在我只将复平面的坐标保持在堆栈中.这就是我需要再次重新绘制旧图像.

Jas*_*n C 5

这个:

private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Run Code Online (Sandbox Code Playgroud)

实例化一个new BufferedImage并存储对该对象的引用image.

这个:

zooms.push(image);
Run Code Online (Sandbox Code Playgroud)

将对BufferedImage您创建的单个引用推送到堆栈.

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

高级效果是每次渲染时将每个先前状态更改为当前状态.

你想BufferedImage为每个州创造一个全新的; 这样你粘在堆栈上的每个引用都指向一个唯一的对象.

看看这篇关于引用如何在Java中工作的好文章.