简体   繁体   English

可以将此更改为某种颜色

[英]It is possible to make this change to a certain color

I am trying to change the color of the ball when it hits any side of the wall. 我试图改变球撞到墙壁任何一侧时的颜色。 I can make it change color but it goes back to the original color afterwards. 我可以让它改变颜色,但之后会恢复原来的颜色。

This is the section of code I'm dealing with. 这是我正在处理的代码部分。 I need some help. 我需要协助。 Is there a way to keep the color change? 有没有办法保持颜色变化?

If you go all the way down the code you can see the comment part //initial ball color. 如果你一直向下看代码,你可以看到评论部分//初始球颜色。 I am sure that part of the code is the reason why it goes back to the original color and does not change color. 我确信代码的一部分是它恢复原始颜色并且不会改变颜色的原因。 Is there a way to keep the color that it changes too when it hits the wall? 当它撞到墙壁时,有没有办法保持它变化的颜色?

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);
        }

    } 
} 

I think this code does what you are looking for. 我认为这段代码可以满足您的需求。

球

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
        }
    });
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在gridlayout中更改特定窗格的颜色 - Change the color of a certain pane in gridlayout SmartGWT:是否可以为列表网格中的某一行着色? - SmartGWT: is it possible to color a a certain row in a list grid? JCalendar:如何更改某些日子的前景色? - JCalendar: how to change the foreground color of certain days? Android-在特定时间内更改按钮的颜色 - Android - change color of a button for a certain time 是否可以使某些代码不起作用? - Is it possible to make a certain piece of code non functional? 是否可以将 JsonFormat 日期时间的某些部分设为可选? - is it possible to make certain part of the JsonFormat datetime optional? 是否可以在某个子类中使超类归为静态? - is it possible to make an atribute of a superclass static in a certain subclass? 是否可以在排序中更改某些符号的优先级(权重)? - Is it possible to change priority(weight) of certain symbols in sorting? 是否可以在JOptionPane.showMessageDialog()中更改字符串颜色 - Is it possible to change a string color in JOptionPane.showMessageDialog() 可以更改Wicket活动指示器的颜色吗? - Is it possible to change the color of Wicket's activity indicator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM