我的程序创建1个球而不是2个

时间:2016-11-23 09:34:04

标签: coordinates public

我制作了一个程序,创建一个框架,然后创建2个球并单独移动它们,我的问题是,不知何故,第一个或第二个球正在获得另一个球的坐标,因此彼此被绘制,抱歉对于不好的缩进,这是我第一次发帖提问。

主类:

    public class Game extends JPanel {
        Ball ball01 = new Ball();
        Ball ball02 = new Ball();
        int ball01x = 0,ball01y = 0,ball02x = 0,ball02y = 0;
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.fillOval(ball01x, ball01y, 10, 10);
        g2d.setColor(Color.RED);
        g2d.fillOval(ball02x, ball02y, 10, 10);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Sample Frame");
        Game game = new Game();
        frame.add(game);
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.createBalls();
        while (true) {
            game.getCoords();
            game.updateBalls();
            game.repaint();
            System.out.println("Ball01 x: " + game.ball01x + " Ball02 x " + game.ball02x);
            Thread.sleep(10);
        }
    }

    public void getCoords(){
        ball01x = ball01.getX();
        ball01y = ball01.getY();
        ball02x = ball02.getX();
        ball02y = ball02.getY();
    }

    public void createBalls(){
        ball01.create(300,300);
        ball02.create(50,20);
    }

    public void updateBalls(){
        ball01.Ball();
        ball02.Ball();
    }
}

球类:

public class Ball{
    private static int x;
    private static int y;
    private static int xSize = 30;
    private static int ySize = 30;
    static boolean xright = true,ydown = true;   
    static boolean grow = false;
    public void Ball(){
        this.moveBall();
        this.ballSize();
    }

    public void create(int startX, int startY){
        this.x = startX;
        this.y = startY;
    }

    private void moveBall() {
        if(this.x >= 370){
            xright = false;  
        }
        if(this.x <= 0){
            xright = true;
        }
        if(this.y >= 370){
            ydown = false;
        }
        if(this.y < 0){
            ydown = true;
        }

        if(xright == true){
            this.x = this.x + 1;
        }else if (xright == false){
            this.x = this.x - 1;
        }

        if(ydown == true){
            this.y = this.y + 1;
        }else if (ydown == false){
            this.y = this.y - 1;
        }

    }

    private void ballSize(){
        if (xSize <= 5 && ySize <= 5){
            grow = true;
        }else if (xSize >= 10 && ySize >= 10){
            grow = false;
        }

        if (grow == true){
            xSize = xSize + 1;
            ySize = ySize + 1;
            //System.out.println("Debug");
        }else if (grow == false){
            xSize--;
            ySize--;
            //System.out.println("Debug");
        }
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }
}

0 个答案:

没有答案