可以将此更改为某种颜色

时间:2017-07-22 01:49:54

标签: java user-interface awt

当我撞到墙的任何一侧时,我试图改变球的颜色。 我可以让它改变颜色,但之后会恢复原来的颜色。

这是我正在处理的代码部分。我需要一些帮助。有没有办法保持颜色变化?

如果你一直向下看代码,你可以看到评论部分//初始球颜色。我确信代码的一部分是它恢复原始颜色并且不会改变颜色的原因。当它撞到墙壁时,有没有办法保持它变化的颜色?

public void paint (Graphics g) {
    g.drawRect(rectLeftX, rectTopY,
               rectRightX - rectLeftX, rectBottomY - rectTopY);

    r=new Random();

    for (int n = 1; n < 500 ; n++) {

        Color backgroundColour = getBackground();
        g.setColor(backgroundColour);
        g.fillOval(x, y, diameter, diameter);

        if (x + xChange <= rectLeftX)
        {
            xChange = -xChange;
            g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
            g.fillOval (x, y, diameter, diameter);

        }
        if(x+xChange + diameter >= rectRightX)
        {
            xChange = -xChange;
            g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
            g.fillOval (x, y, diameter, diameter);
        }

        if (y+yChange <= rectTopY)
        {
            yChange = -yChange;
            g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
           g.fillOval (x, y, diameter, diameter);
        }
        if(y + yChange + diameter >= rectBottomY)
        {
            yChange = -yChange;
            g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
           g.fillOval (x, y, diameter, diameter);
        }

        x = x + xChange;
        y = y + yChange;     


        // initial ball color
         g.setColor(get()); 
         g.fillOval (x, y, diameter, diameter);


        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            g.drawString("sleep exception", 20, 20);
        }

    } 
} 

1 个答案:

答案 0 :(得分:0)

我认为这段代码可以满足您的需求。

Ball

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

public class CGBouncingBall extends JFrame {

    // Define named-constants
    private static final int CANVAS_WIDTH = 640;
    private static final int CANVAS_HEIGHT = 480;
    private static final int UPDATE_INTERVAL = 50; // milliseconds
    Random r;
    Color ballColor = Color.BLUE;

    private DrawCanvas canvas;  // the drawing canvas (an inner class extends JPanel)

    // Attributes of moving object
    private int x = 100;     // top-left (x, y)
    private int y = 100;
    private int size = 250;  // width and height
    private int xSpeed = 3;  // moving speed in x and y directions
    private int ySpeed = 5;  // displacement per step in x and y

    // Constructor to setup the GUI components and event handlers
    public CGBouncingBall() {
        canvas = new DrawCanvas();
        canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
        this.setContentPane(canvas);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.pack();
        this.setTitle("Bouncing Ball");
        this.setVisible(true);

        // Create a new thread to run update at regular interval
        Thread updateThread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    update();   // update the (x, y) position
                    repaint();  // Refresh the JFrame. Called back paintComponent()
                    try {
                        // Delay and give other thread a chance to run
                        Thread.sleep(UPDATE_INTERVAL);  // milliseconds
                    } catch (InterruptedException ignore) {
                }
            }
        }
    };
    updateThread.start(); // called back run()
}

// Update the (x, y) position of the moving object
public void update() {
    x += xSpeed;
    y += ySpeed;
    r=new Random();
    if (x > CANVAS_WIDTH - size || x < 0) {
        xSpeed = -xSpeed;
        ballColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
    }
    if (y > CANVAS_HEIGHT - size || y < 0) {
        ySpeed = -ySpeed;
        ballColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
    }
}

// Define Inner class DrawCanvas, which is a JPanel used for custom drawing
class DrawCanvas extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // paint parent's background
        setBackground(Color.BLACK);
        g.setColor(ballColor);
        g.fillOval(x, y, size, size);  // draw a circle
    }
}

// The entry main method
public static void main(String[] args) {
    // Run GUI codes in Event-Dispatching thread for thread safety
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new CGBouncingBall(); // Let the constructor do the job
        }
    });
}
}