0

我有一个二维数组。我希望每个像素在实际图像中总共由四个表示。我尝试了各种代码,但似乎都没有工作,我也不太明白它是如何工作的。

到目前为止,我有:

panel = new JPanel() {
            @Override
            public void paint(Graphics g) {
                Rectangle rect = g.getClipBounds();
                g.setColor(Color.white);
                g.fillRect(rect.x, rect.y, rect.width, rect.height);
                for (int i = 0; i < m.width(); i++) {
                    for (int j=0; j < m.height(); j++) {
                        g.setColor(Color.red);
                        g.fillRect(j*4, i*4, 4, 4);
                    }
                }
                super.paint(g);
            }
        };
        panel.repaint();

我哪里错了?该区域保持完全灰色,没有颜色!

4

1 回答 1

0

虽然覆盖paint()并不是最糟糕的事情,但我强烈建议使用覆盖paintComponent()。此外,您必须在使用对象进行绘图super.paint() 之前Graphics调用,而不是之后调用。它只是以这种方式废弃了你所有的工作。

另外,我不知道您是否这样做,因为您的代码中没有它,但请确保将面板添加到JFrame您正在使用的窗口类或任何窗口类中,以便它实际显示。我希望这有帮助。

于 2013-08-04T02:44:55.377 回答