我正在尝试在Canvas
中使用JFrame
实现Julia Set的图。由于某些原因,setColor()
似乎不起作用。这是负责的代码:
@Override
public void paint(Graphics aGraphics)
{
// store on screen graphics
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
for(int i = 0; i < m_iWidth; i++)
{
for(int j = 0; j < m_iHeight; j++)
{
int r = m_iPixelRed[i][j];
int g = m_iPixelGreen[i][j];
int b = m_iPixelBlue[i][j];
aGraphics.setColor(new Color(r, g, b));
aGraphics.drawRect(i, j, 0, 0);
}
}
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 1, 1, null);
}
最初,我怀疑这些值未正确传递给
m_iPixel...
,因此我在调用函数中将这些值硬编码为0xff
。我通过r
,g
,b
进行了检查,并确定它们都设置为该值,但是画布是黑色的。有趣的是:当我输入
aGraphics.setColor(Color.WHITE)
或aGraphics.setColor(0xff, 0xff, 0xff)
而不是变量r
,g
,b
时,它起作用了!即使我检查了变量的值是否相同,也将它们较早地硬编码为0xff
。我完全不知道可能是什么问题...编辑:
这些值被硬编码如下:
public void setPixelColour(int i, int j, int r, int g, int b)
{
m_iPixelRed[i][j] = 0xff;
m_iPixelGreen[i][j] = 0xff;
m_iPixelBlue[i][j] = 0xff;
}
setPixelColour
由超类在此方法中调用:private void calcColour(int i, int j, int aIterations)
{
m_cCanvas.setPixelColour(i, j, 0XFF, 0xff, 0XFF);
}
该循环又调用了该循环。
for(int i = 0; i < iCanvasHeight; i++){
for(int j = 0; j < iCanvasWidth; j++){
cSum.setRe(m_cCoordPlane[i][j].getRe());
cSum.setIm(m_cCoordPlane[i][j].getIm());
m_iIterations[i][j] = 0;
do{
m_iIterations[i][j]++;
cSum = cSum.square();
cSum = cSum.add(m_cSummand);
m_dAbsSqValues[i][j] = cSum.getAbsSq();
}while((m_iIterations[i][j] < MAXITER) && (m_dAbsSqValues[i][j] < m_iDivergThresh));
this.calcColour(i, j, m_iIterations[i][j]);
m_cMsgIter = "x = " + i + " , y = " + j;
this.repaint();
}
}
我检查确保此循环确实完成。我在
setColor()
之前使用调试器再次检查了这些值。由于我不信任调试器(经验不足),因此我又在控制台上通过在System.out.println("r = " + Integer.toString(r) + " g = " + Integer.toString(g) + " b = " + Integer.toString(b));
之前添加setColor()
来再次检查控制台。编辑:
这是我的
JFrame
绘制方法:public void paint(Graphics aGraphics)
{
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
this.paintComponents(aGraphics);
// drawString() calls are debug code only...
aGraphics.setColor(Color.BLACK);
aGraphics.drawString(m_cSMsg, 10, 450);
aGraphics.drawString(m_cMsgIter, 10, 465);
aGraphics.drawString(m_cMsgDivThresh, 10, 480);
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 0, 0, null);
}
最佳答案
不知道在注释中发布大量代码是否有意义,所以这是我的测试代码:
package test;
import javax.swing.JFrame;
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
MyCanvas canvas = new MyCanvas();
frame.add(canvas);
frame.pack();
frame.setVisible(true);
for(int i = 0; i < 800; i++)
{
for(int j = 0; j < 600; j++)
{
canvas.setPixelColour(i, j, 0XFF, 0xff, 0XFF);
canvas.repaint();
}
}
}
}
这是MyCanvas类:
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class MyCanvas extends java.awt.Canvas
{
private BufferedImage m_cBackGroundImage;
private int[][] m_iPixelRed, m_iPixelGreen, m_iPixelBlue;
private int m_iWidth, m_iHeight;
public MyCanvas()
{
setPreferredSize(new Dimension(800, 600));
m_iWidth = 800;
m_iHeight = 600;
m_cBackGroundImage = new BufferedImage(m_iWidth, m_iHeight, BufferedImage.TYPE_INT_ARGB);
m_iPixelRed = new int[m_iWidth][m_iHeight];
m_iPixelGreen = new int[m_iWidth][m_iHeight];
m_iPixelBlue = new int[m_iWidth][m_iHeight];
}
public void paint(Graphics aGraphics)
{
Graphics cScreenGraphics = aGraphics;
aGraphics = m_cBackGroundImage.getGraphics();
for(int i = 0; i < m_iWidth; i++)
{
for(int j = 0; j < m_iHeight; j++)
{
int r = m_iPixelRed[i][j];
int g = m_iPixelGreen[i][j];
int b = m_iPixelBlue[i][j];
aGraphics.setColor(new Color(r, g, b));
aGraphics.drawRect(i, j, 0, 0);
}
}
cScreenGraphics.drawImage(m_cBackGroundImage, 1, 1, null);
}
public void setPixelColour(int i, int j, int r, int g, int b)
{
m_iPixelRed[i][j] = r;
m_iPixelGreen[i][j] = g;
m_iPixelBlue[i][j] = b;
}
}
我试图尽可能地接近您提供的内容(即使您的命名约定实际上并不是我的意思)。主要更改是在main方法的循环中进行的,因为我不需要大部分代码。我也废除了calcColor方法,因为它只是简单地调用了另一个方法。
无论如何,这对我有用(=我得到一张白色帆布)。我还尝试将0xff s更改为(int)(Math.random()* 255),这将导致...让我们一起使用彩虹色的画布,所以似乎工作正常。