使用java中的图形绘制多个圆圈

时间:2015-02-21 02:14:50

标签: 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
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

你应该做的是创建某种模型,以维护哪个玩家选择了哪个单元格的信息。

例如,您可以使用String的2D数组,其中null表示无身体,Red表示红色玩家,Blue表示蓝色播放器。然后,您将使用一个简单的复合循环来遍历数组并相应地绘制UI ...

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

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

答案 1 :(得分:0)

一个人不应该在repaint()方法中调用paintComponent(),因为repaint()会以某种方式调用paintComponent(),从而导致递归调用。

此外,当调用repaint()时,整个JComponent实际上是从初始状态绘制的。为了让您可以在JPanel/JComponent上绘制圆圈,可以使用Collection/Array来跟踪已绘制的内容。因此代码可以迭代此Collection/Array来重绘以前绘制的所有圆圈和要绘制的新圆圈。

以下是小样本示例:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;

public class CircleExample {

    private static final int GAP = 5;

    private JPanel drawingBoard;    

    private void displayGUI() {
        JFrame frame = new JFrame("Circle Drawing Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(GAP, GAP));
        contentPane.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));

        drawingBoard = new DrawingBoard();
        contentPane.add(drawingBoard);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new CircleExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

class DrawingBoard extends JPanel {

    private static final int WIDTH = 300;
    private static final int HEIGHT = 350;

    private List<MyCircle> circles;
    private Random random;

    private MouseAdapter mouseAdapter = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            System.out.println("Mouse Clicked");
            int x = me.getX();
            int y = me.getY();
            circles.add(new MyCircle(x, y, getRandomColour()));
            DrawingBoard.this.repaint();
        }
    };

    public DrawingBoard() {
        super();
        circles = new ArrayList<MyCircle> ();
        random = new Random();
        setOpaque(true);
        addMouseListener(mouseAdapter);
    }

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

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (MyCircle circle : circles) {
            circle.drawCircle(g);
        }
    }

    private Color getRandomColour() {
        return new Color(random.nextFloat(), random.nextFloat(),
                            random.nextFloat(), random.nextFloat());
    }
}

class MyCircle {

    private int x;
    private int y;
    private Color backgroundColour;

    private static final int RADIUS = 20;

    public MyCircle(int x, int y, Color backgroundColour) {
        this.x = x;
        this.y = y;
        this.backgroundColour = backgroundColour;
    }

    public void drawCircle(Graphics g) {
        g.setColor(backgroundColour);
        g.fillOval(x, y, RADIUS, RADIUS);
    }
}