长话短说我正在使用我放入自定义JPanel的BufferedImage绘制Mandelbrot。我已经完成了设置的缩放,但是在重新连接时有重新绘制的问题。当unzooming我将图像的值更改为图像的先前状态(我保持堆栈中的每个状态)并重新绘制面板。问题是堆栈中的最后一个图像被弹出但是没有绘制。
这些是我的实例变量
private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
private Stack<BufferedImage> zooms = new Stack<BufferedImage>();
private boolean unzoom = false;
这是我如何缩放和推送我想要保存在堆栈中的图像
public void mouseReleased(MouseEvent e)
{
zooms.push(image);
<some code for zooming that works>
repaint();
}
现在我想通过滚动来取消缩放
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();
}
}
最后这是我的绘画方法
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;
}
FIX:事实证明,我不需要保留最后一张图像并且每次都创建临时图像。相反,现在我只将复平面的坐标保持在堆栈中。这就是我需要再次重新绘制旧图像。
答案 0 :(得分:5)
此:
private BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
实例化新的BufferedImage
并在image
中存储对该对象的引用。
此:
zooms.push(image);
将对您创建的单个BufferedImage
的引用推送到堆栈。
只要你继续使用相同的BufferedImage
,你所做的就是将对同一个对象的多个引用推送到堆栈上;因此,对象数据的更改会反映在您放置在堆栈中的每个引用中,因为堆栈中的每个项都指向同一个对象。
高级效果是每次渲染时将每个先前状态更改为当前状态。
您需要为每个州创建一个全新的BufferedImage
;这样你粘在堆栈上的每个引用都指向一个唯一的对象。