Java Graphics setColor 不起作用



我正在尝试使用JFrame内的Canvas实现朱莉娅集的图。出于某种原因,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。我通过rgb检查了这一点,并确定它们都设置为该值,但画布是黑色的。

有趣的是:当我输入aGraphics.setColor(Color.WHITE)aGraphics.setColor(0xff, 0xff, 0xff)而不是变量rgb它起作用了!即使我检查变量的值相同,并将它们硬编码为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()之前使用调试器再次检查了这些值。由于我不信任调试器(出于经验),因此我在控制台上再次检查了一次,方法是在setColor()之前添加System.out.println("r = " + Integer.toString(r) + " g = " + Integer.toString(g) + " b = " + Integer.toString(b));

编辑:

这是我的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),这将导致...让我们使用彩虹色的画布,所以它似乎工作正常。

相关内容

  • 没有找到相关文章

最新更新