创建一个多彩多姿的板

时间:2013-04-09 00:19:55

标签: java grid

我要创建一个多色板,从第一个方块开始为黑色,然后是蓝色,红色和黄色,方块是对角填充的,没有空的彩色方块。我知道我的算法是错误的,但我不知道如何解决它。 目前,我的代码打印出来像这样

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;

public class Grid extends JPanel {
private static final long serialVersionUID = 1L;
public static final int GRID_COUNT = 8;
private Color[] colors = { Color.black, Color.yellow, Color.red,
        Color.blue };
private int colorIndex = 0;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D graphics = (Graphics2D) g;
    graphics.setColor(Color.black);

    Dimension size = getSize();
    Insets insets = getInsets();
    int w = size.width - insets.left - insets.right;
    int h = size.height - insets.top - insets.bottom;

    int sqrWidth = (int)((double)w / GRID_COUNT);
    int sqrHeight = (int)((double)h / GRID_COUNT);
    for (int row = 0; row < GRID_COUNT; row++) {
        for (int col = 0; col < GRID_COUNT; col++) {
                int x = (int) (row * (double) w / GRID_COUNT);
                int y = (int) (col * (double) h / GRID_COUNT);
                if ((row + col) % 2 == 0) {
                    int colorIndex = (row + col) % 4;
                    graphics.fillRect(x, y, sqrWidth, sqrHeight);
                    graphics.setColor(colors[colorIndex]);
                    colorIndex = (colorIndex + 1) % colors.length;
    }

}

public static void main(String[] args) {
    Grid grid = new Grid();
    grid.setPreferredSize(new Dimension(400, 400));
    JFrame frame = new JFrame("Grid");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(grid);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

}

3 个答案:

答案 0 :(得分:1)

有两个错误,我看到,第一个错误是你的模式,你想要从黑色,“然后蓝色,红色和黄色”,但你做了

 private Color[] colors = { Color.black, Color.yellow, Color.red, Color.blue };

改变这个

第二个错误是你的程序计数均匀,意味着它是均匀填充矩形, 2,4,6,8 .. 让你的程序会在每一个灰色的矩形上进行...

答案 1 :(得分:1)

让我们来看看模式:

Bk Gr Pn Bl
Gr Pn Bl Bk
Pn Bl Bk Gr
Bl Bk Gr Pn

但为了简单起见,让我们调用Bk 0,Gr 1,Pn 2和Bl 3来获得:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

通过为每个图块计算tile[x][y] = (x + y) % 4并使用查找表将这些数字转换为颜色(使用枚举,或者不是为图块指定整数值而是使用整数作为颜色),可以轻松生成此模式查找颜色表并将颜色分配给图块

如果您以前从未见过,%4表示“除以4并返回该部门的剩余部分”。

答案 2 :(得分:1)

我已经快速完成了代码,并且有许多小事情让我感到困惑......

您的颜色计算已被删除,因为您的原始代码正在跳过每隔一个单元格...

if ((row + col) % 2 == 0) {

这意味着当您尝试确定颜色时,您没有得到您期望的颜色。

您还在行循环中设置了单元格的颜色,而不是在列循环中设置颜色,这对我来说似乎很奇怪......

enter image description here

(我添加了文本以便进行调试,随意摆脱它)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Grid {

    private static final long serialVersionUID = 1L;
    public static final int GRID_COUNT = 8;
    private Color[] colors = {Color.black, Color.yellow, Color.red,
        Color.blue};

    public static void main(String[] args) {
        new Grid();
    }

    public Grid() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Grid");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new GridPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public class GridPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D graphics = (Graphics2D) g;
            graphics.setColor(Color.black);

            Dimension size = getSize();
            Insets insets = getInsets();
            int w = size.width - insets.left - insets.right;
            int h = size.height - insets.top - insets.bottom;

            FontMetrics fm = graphics.getFontMetrics();

            int sqrWidth = (int) ((double) w / GRID_COUNT);
            int sqrHeight = (int) ((double) h / GRID_COUNT);
            int colorIndex = 0;
            for (int row = 0; row < GRID_COUNT; row++) {
                for (int col = 0; col < GRID_COUNT; col++) {
                    int x = (int) (col * sqrWidth);
                    int y = (int) (row * sqrHeight);
                    colorIndex = (row + col) % 4;
                    graphics.setColor(colors[colorIndex]);
                    graphics.fillRect(x, y, sqrWidth, sqrHeight);
                    String text = row + "/" + col;
                    graphics.setColor(Color.WHITE);
                    graphics.drawString(
                            text,
                            x + ((sqrWidth - fm.stringWidth(text)) / 2),
                            y + ((sqrHeight - fm.getHeight()) / 2) + fm.getAscent());
                }
            }

        }
    }
}