首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中制作双色台板

如何在Java中制作双色台板
EN

Stack Overflow用户
提问于 2015-12-08 23:59:57
回答 1查看 107关注 0票数 1

在我的Java类中,我们将学习“Java基础”一书的第4章。我正在做项目4-11,这是一个黑色和红色的棋盘,但我得到了随机的颜色,我试图完成这本书教我们使用ColorPanels和JFrames的方式。下面是我的代码:

代码语言:javascript
复制
package guiwindow3;
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class GUIWindow3 {

    public static void main(String[] args) {

        //Objects
        JFrame theGUI = new JFrame();

        //Format GUI
        theGUI.setTitle("GUI Example");

        theGUI.setSize(500, 500);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container pane = theGUI.getContentPane();
        pane.setLayout(new GridLayout(8, 8));

        //Loop for grid
        Color lastColor = Color.BLACK;

        for(int i = 1; i < 8; i++) {

            for(int j = 0; j < 8; j++) {

                if((j % 2 == 1 && i %2 == 1) || (j % 2 == 0 && i % 2 == 0)) {

                    lastColor = Color.RED;

                }

                else {

                    lastColor = Color.BLACK;

                }

                ColorPanel panel = new ColorPanel(lastColor);

                pane.add(panel);

            }

        }

        theGUI.setVisible(true);

    }

}

然后对于ColorPanel类,我有:

代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;

class ColorPanel extends JPanel {

    public ColorPanel(Color lastColor) {

        setBackground(lastColor);

    }

}

EN

回答 1

Stack Overflow用户

发布于 2015-12-09 00:56:33

获得随机数的原因是您为每个RGB参数创建了一个随机数。相反,您可以更改以下内容:

代码语言:javascript
复制
for (int i = 1; i <= rows * cols; i++) {
    int red = gen.nextInt(256); //A random Red 
    int green = gen.nextInt(256); //A random Green
    int blue = gen.nextInt(256); //A random Blue
    Color backColor = new Color(red, green, blue); //Join them and you get a random color
    ColorPanel panel = new ColorPanel(backColor); //Paint the panel
    pane.add(panel);
}

要这样做:

代码语言:javascript
复制
Color lastColor = Color.BLACK;
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        if ((j % 2 == 1 && i % 2 == 1) || (j % 2 == 0 && i % 2 == 0)) {
            lastColor = Color.RED; //Set the color to RED
        } else {
            lastColor = Color.BLACK; //Set the color to BLACK
        }
        ColorPanel panel = new ColorPanel(lastColor); //Paint the panel with RED or BLACK color
        pane.add(panel); //Add the painted Panel
    }
}

输出结果类似于:

编辑

另一种获得相同输出并使if条件更容易阅读的方法是@dimo414在他的comment中所说的:

if((j % 2 == 1 && i %2 == 1) || (j % 2 == 0 && i % 2 == 0))if (j % 2 == i % 2)相同

代码语言:javascript
复制
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        if (i % 2 == j % 2) {
            lastColor = Color.RED;
        } else {
            lastColor = Color.BLACK;
        }
        ColorPanel panel = new ColorPanel(lastColor);
        pane.add(panel);
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34160393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档