Creating a multicolored board

拈花ヽ惹草 提交于 2019-12-02 16:13:34

问题


I am to create a multicolored board, starting with the first square as black, then blue, red, and yellow, the squares are being filled diagonally and there are no empty colored squares. I know my algorithm is wrong, but I have not a clue as how to fix it. Currently, my code prints out like this

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);

}

}


回答1:


Let's look at the pattern:

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

But to make it simpler, let's call Bk 0, Gr 1, Pn 2 and Bl 3 to get:

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

This pattern is easily produced by calculating tile[x][y] = (x + y) % 4 for every tile, and using a lookup table to convert these numbers to colours (Either use an enumeration, or instead of assigning an integer value to the tile use the integer as a look-up in a table of colours and assign the colour to the tile)

If you've never seen it before, % 4 means 'divide by 4 and return the REMAINDER of the division'.




回答2:


There are two mistake, I saw, first mistake is your pattern, you want to go from black, "then blue, red, and yellow" but you did

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

change this one

and second mistake is that your program is counting evenly, means it is filling rectangle evenly , 2 , 4 , 6, 8.. make your program will go on every single rectangle, which is in grey color...




回答3:


I've run through the code quickly and there are number of little things which seem confusing to me..

Your color calculation is been thrown off because your original code was skipping every second cell...

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

Which meant that when you tried to determine the color, you weren't getting the color you were expecting.

You were also setting the color of the cell within the row loop and not in the column loop, which seems weird to me...

(I added text in to for debugging, feel free to get rid of it)

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());
                }
            }

        }
    }
}


来源:https://stackoverflow.com/questions/15891250/creating-a-multicolored-board

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!