java - 在java中使用图形绘制多个圆

标签 java swing graphics

我正在开发一款四人连线游戏,我试图在点击屏幕时显示多个圆圈。现在,每次我重新点击时,我刚刚画的圆圈都会消失。

感谢任何帮助。

protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;    // create 2d object
        g2d.setStroke(new BasicStroke(2));  // set thickness of line

        // each box is 100 x 100
        // draws the vertical lines
        for (int i = 0; i < 8; i++) {
            g2d.drawLine(140 + i * DISC_RADIUS, 30, 140 + i * DISC_RADIUS, 690);
        }
        // draws the horizontal lines
        for (int i = 0; i < 7; i++) {
            g2d.drawLine(140, 90 + i * DISC_RADIUS, 840, 90 + i * DISC_RADIUS);
        }
        // draws the circles
        for (int i = 0; i < 6; i++) {       // new vertical row of circles
            for (int j = 0; j < 7; j++) {   // new horizontal row of circles
                g2d.drawOval(140 + j * DISC_RADIUS, 90 + i * DISC_RADIUS, DISC_RADIUS, DISC_RADIUS);
            }
        }

        // if at the start of the game, will not draw the counters
        if (start == true) {
            // draws blue counter
            g2d.drawOval(20, 90, DISC_RADIUS, DISC_RADIUS);
            g2d.setColor(Color.BLUE);       // sets colour to blue
            g2d.fillOval(20, 90, DISC_RADIUS, DISC_RADIUS); // draws the circle

            // draws red counter
            g2d.setColor(Color.BLACK);
            g2d.drawOval(875, 90, DISC_RADIUS, DISC_RADIUS);
            g2d.setColor(Color.RED);            // sets the colour to red
            g2d.fillOval(875, 90, DISC_RADIUS, DISC_RADIUS);    // draws the circle


        }
        //print on the screen who's turn it is


        // draws blue counter stand;
        g2d.setStroke(new BasicStroke(4));  // sets the line width
        g2d.setColor(Color.BLACK);          // changes the outline colour to black
        g2d.drawPolygon(poly1);             // draw the outline
        g2d.setColor(Color.GRAY);           // changes the fill colour to grey
        g2d.fillPolygon(poly1);             // draws the filling

        // draws bred counter stand;
        g2d.setColor(Color.BLACK);  // changes the outline colour to black
        g2d.drawPolygon(poly2);     // draws the outline
        g2d.setColor(Color.GRAY);   // changes the fill colour to grey
        g2d.fillPolygon(poly2);     // draws the filling

        repaint();

        if(Player.getPlayer() == "Blue"){
            g2d.setColor(Color.BLACK);  
            if(draw == true){
                g2d.drawString("Blue's Turn", 40, 300); 
                g2d.drawOval(xPos, yPos, DISC_RADIUS, DISC_RADIUS);
                g2d.setColor(Color.BLUE);       // sets colour to blue
                g2d.fillOval(xPos, yPos, DISC_RADIUS, DISC_RADIUS); // draws the circle
            }
        }

        if(Player.getPlayer() == "Red"){
            g2d.drawString("Red's Turn", 900, 300);
            if(draw == true){
                g2d.drawOval(xPos, yPos, DISC_RADIUS, DISC_RADIUS);
                g2d.setColor(Color.RED);        // sets colour to blue
                g2d.fillOval(xPos, yPos, DISC_RADIUS, DISC_RADIUS); // draws the circle
            }
        }
    }
}

最佳答案

绘制是破坏性的,也就是说,每次调用 paintComponent 时,您都应该从头开始重新绘制组件的整个状态。

您应该做的是创建某种模型来维护有关哪个玩家选择了哪个单元格的信息。

例如,您可以只使用 String 的二维数组,其中 null 表示没有主体,Red 表示红色玩家Blue 代表蓝色玩家。然后,您将使用一个简单的复合循环来遍历数组并相应地绘制 UI...

现在,if(Player.getPlayer() == "Blue"){ 不是 String 比较在 Java 中的工作方式,您应该使用更像 if("Blue".equals(Player.getPlayer())){ 代替。

不要直接或间接地从任何绘制方法中更新或修改 UI 的状态,这可能会设置一个无限循环的绘制请求,从而消耗您的 CPU 周期...

关于java - 在java中使用图形绘制多个圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28641300/

相关文章:

java - JSR305 与 JSR308(Java 类型注释)——哪个将成为标准?

java - tld 文件在网络应用程序中的位置?

java - 如何在android中以表格格式显示ArrayList数据

java swing : custom everything - subclass jcomponent or jpanel or . ..?

java - JTextField 文档监听器/文档事件

java - 文本字段仅接受数字输入

OpenGL:为什么选择三角形作为基本构建 block ?

java - 创建一个 "Command"控制台

image - 在 Node.js 中对图像应用半透明水印

java - 使用 Graphics 绘制的 BufferedImage 中的数据