【问题标题】:How do I draw colored circles in Java on a JPanel?如何在 JPanel 上用 Java 绘制彩色圆圈?
【发布时间】:2013-10-15 18:25:39
【问题描述】:

我正在为我的班级做另一个 Java 项目。在这个任务中,我们必须创建一个棋盘并用适当数量的棋子填充它。我正确构建了显示良好的棋盘格,但我很难使用 Graphics 类进行绘制。

这是我目前的代码:

import javax.swing.*;
import java.awt.*;

public class Checkerboard extends JFrame {
    //Define the default values for the separate checker pieces
    private final int RED_PIECE = 0;
    private final int BLACK_PIECE = 1;

    /** Construct the default checker board */
    public Checkerboard() {
        this.setSize(600, 600);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle("Checkerboard Lab");
        this.setLayout(new GridLayout(8, 8));
        this.setVisible(true);

        for (int a=0; a<2; a++) {
            for (int i=0; i<4; i++) {
                add(new WhiteSpace());
                add(new GraySpace(RED_PIECE));
            }
            for (int j=0; j<4; j++) {
                add(new GraySpace(RED_PIECE));
                add(new WhiteSpace());
            }
        }
        for (int b=0; b<2; b++) {
            for (int k=0; k<4; k++) {
                add(new WhiteSpace());
                add(new GraySpace(RED_PIECE));
            }
            for (int l=0; l<4; l++) {
                add(new GraySpace());
                add(new WhiteSpace());
            }
        }
        for (int c=0; c<2; c++) {
            for (int m=0; m<4; m++) {
                add(new GraySpace());
                add(new WhiteSpace());
            }
            for (int n=0; n<4; n++) {
                add(new GraySpace(BLACK_PIECE));
                add(new WhiteSpace());
            }
        }
        for (int d=0; d<2; d++) {
            for (int o=0; o<4; o++) {
                add(new WhiteSpace());
                add(new GraySpace(BLACK_PIECE));
            }
            for (int p=0; p<4; p++) {
                add(new GraySpace(BLACK_PIECE));
                add(new WhiteSpace());
            }
        }
    }

    /** White Space constructor */
    public class WhiteSpace extends JPanel {
        public WhiteSpace() {
            setBackground(Color.WHITE); //Sets the panel's background color to white
        }
    }

    /** Gray Space constructor */
    /* GraySpace is a little different, since this color space is the only space that will be holding checker
     * pieces. There is a default constructor to create a space without a checker piece on it, and another 
     * constructor that places either a red or black piece on the space, pending an optional parameter.*/
    public class GraySpace extends JPanel {
        //Initial variable for the checker piece
        int checkerPiece;

        //Default GraySpace constructor
        public GraySpace() {
            setBackground(Color.LIGHT_GRAY);
        }

        //The GraySpace constructor with the optional parameter to determine if it holds a checker piece
        public GraySpace(int piece) {
            this.checkerPiece = piece;
            setBackground(Color.LIGHT_GRAY); //Sets the panel's background color to white
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            //Default width and height variables
            int width = getWidth() -10;
            int height = getHeight() - 10;

            //This switch statement determines which checker piece type appears on the square
            switch (checkerPiece) {
            case RED_PIECE:
                g.setColor(Color.RED);
                g.fillOval(5, 5, width, height);
                break;
            case BLACK_PIECE:
                g.setColor(Color.BLACK);
                g.fillOval(5, 5, width, height);
                break;
            }
        }
    }

    /** Initiate the Checker board */
    public static void main(String[] args) {
        JFrame checkerboard = new Checkerboard();
    }
}

这很简单。我的 Checkerboard 类是 JFrame 的子类,我将彩色面板放在 8x8 正方形中。面板是 Checkerboard 类的内部类,每个都扩展 JPanel(分别为 WhiteSpace 和 GraySpace)。由于 GraySpace 是唯一必须拥有检查器的类,因此我认为我只需将 Graphics 代码放入 GraySpace 内部类。

无论如何,对于我的问题:我该如何使用 Graphics 来绘制它?我知道我必须显式声明 paintComponent() 方法才能绘制圆,但我不知道如何指定 GraySpace 的尺寸以便有效绘制。有什么建议吗?

编辑:新问题!

好的,所以我弄清楚了如何将棋子添加到我的棋盘上,而且效果很好。我的 GraySpace 内部类有一个可选的构造函数,它接受 int 值,并据此确定 GraySpace 面板上的颜色块。我对此进行了测试,它确实有效。

然而,我的问题实际上是将棋子放到棋盘上。棋盘必须代表“默认”棋盘游戏,所有可用棋子都在棋盘上。所以,三排红色棋子,三排黑色棋子,用两排空隔开。到目前为止,我有 4 个单独的 for 循环在板上一次绘制两行......但它不能正常工作。有什么建议吗?最新的源代码在上面,替换了我的老问题源代码。再次感谢您的建议!

【问题讨论】:

    标签: java user-interface swing


    【解决方案1】:

    在你所在的组件上调用getHeightgetWidth。由于paintComponent 是你的JPanel 类的成员,你可以直接调用getHeightgetWidth

    我对您的方法做了一些其他更改。

    1. 不要调用this.paintComponent,调用基类(super)
    2. 您需要在绘制之前设置颜色。
    3. 我敢打赌fillOval 是你想要的。

    例如:

    protected void paintComponent(Graphics g) { 
        int h = getHeight();
        int w = getWidth();
        super.paintComponent(g); 
        g.setColor(CHECKER_COLOR);
        g.fillOval(w/2, h/2, w, h); 
    } 
    

    要获得额外的功劳,请打开抗锯齿功能,让您的跳棋看起来很棒!

    【讨论】:

    • 噢噢噢!!感谢那!我对您的代码有几个问题,只是为了澄清一些事情:1)为什么我叫 super 而不是这个? 2) 为了使用paintComponent(),我必须创建一个Graphics 类的对象吗?我知道它是 Graphics 类的一部分,所以这就是我被绊倒的地方。非常感谢您的帮助!我以为我必须画出圆圈,然后再填充它!谢谢!!
    • 1) 如果你调用它,你就是在递归调用你的paintComponent。使用 super 调用基类将为您绘制背景。尝试评论该行,看看会发生什么。
    • 2) 您被传入您的 Graphics 对象作为paintComponent 的参数,因此您不必创建一个。
    【解决方案2】:

    关于你的 for 循环的一件事只是为了将来的知识,当你定义 int i=0 时,那个变量只存在于那个循环中,所以你不需要使用不同的变量 a,b,c,d,e等在您的 4 个 for 循环中,您可以简单地使用 i,j,k 4 次。

    另外,我认为您的循环添加的片段比您想要的要多。

    for (int a=0; a<2; a++) {             //2 (
       for (int i=0; i<4; i++) {          //    4
           add(new WhiteSpace());         //      (1 
           add(new GraySpace(RED_PIECE)); //         + 1)
       }                                  //  +                     
       for (int j=0; j<4; j++) {          //    4
           add(new GraySpace(RED_PIECE)); //      (1 
           add(new WhiteSpace());         //         + 1)
       }                                  //  )
    }                                     //= 2 ( 4 * 2 + 4 * 2) = 32
    

    在这一循环中,您将添加 32 个方格。你这样做 4 次。这已经是您需要的平方的两倍了。

    最简单的解决方案是删除 4 个外部循环。然后你就会得到你想要的。

    【讨论】:

    • 是的,经过一番思考,我偶然发现了这个问题。幸运的是,我及时解决了问题,而且效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2015-01-05
    相关资源
    最近更新 更多