Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以将此更改为特定颜色_Java_User Interface_Awt - Fatal编程技术网

Java 可以将此更改为特定颜色

Java 可以将此更改为特定颜色,java,user-interface,awt,Java,User Interface,Awt,当球撞到墙的任何一边时,我试图改变球的颜色。 我可以让它改变颜色,但它会回到原来的颜色 这是我正在处理的代码部分。我需要一些帮助。有没有办法保持颜色的变化 如果一直往下看代码,可以看到注释部分//初始球颜色。我确信部分代码是它返回到原始颜色并且不改变颜色的原因。有没有一种方法可以使颜色在碰到墙壁时也保持不变 public void paint (Graphics g) { g.drawRect(rectLeftX, rectTopY, rectRightX

当球撞到墙的任何一边时,我试图改变球的颜色。 我可以让它改变颜色,但它会回到原来的颜色

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

如果一直往下看代码,可以看到注释部分//初始球颜色。我确信部分代码是它返回到原始颜色并且不改变颜色的原因。有没有一种方法可以使颜色在碰到墙壁时也保持不变

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

    } 
} 
public void绘制(图形g){
g、 drawRect(rectLeftX,rectTopY,
rectRightX-rectLeftX,rectBottomY-rectTopY);
r=新随机数();
对于(int n=1;n<500;n++){
Color backgroundcolor=getBackground();
g、 背景色;
g、 圆角(x,y,直径,直径);
if(x+xChange=rectRightX)
{
xChange=-xChange;
g、 setColor(新颜色(r.nextInt(256)、r.nextInt(256)、r.nextInt(256));
g、 圆角(x,y,直径,直径);
}
如果(y+yChange=y)
{
yChange=-yChange;
g、 setColor(新颜色(r.nextInt(256)、r.nextInt(256)、r.nextInt(256));
g、 圆角(x,y,直径,直径);
}
x=x+xChange;
y=y+y变化;
//初始球颜色
g、 setColor(get());
g、 圆角(x,y,直径,直径);
试一试{
睡眠(50);
}捕捉(中断异常e){
g、 抽绳(“睡眠异常”,20,20);
}
} 
} 

我认为此代码符合您的要求

import javax.swing.*;
导入java.awt.*;
导入java.util.Random;
公共类CGBouncingBall扩展JFrame{
//定义命名常量
专用静态最终整型画布宽度=640;
专用静态最终整型画布高度=480;
私有静态最终整型更新\u间隔=50;//毫秒
随机r;
Color ballColor=Color.BLUE;
私有DrawCanvas;//绘图画布(内部类扩展了JPanel)
//运动对象的属性
private int x=100;//左上角(x,y)
私人int y=100;
私有int size=250;//宽度和高度
private int xSpeed=3;//在x和y方向上的移动速度
private int ySpeed=5;//x和y中每一步的位移
//构造函数来设置GUI组件和事件处理程序
公共弹跳球(){
画布=新的DrawCanvas();
setPreferredSize(新维度(画布宽度、画布高度));
这个.setContentPane(画布);
此.setDefaultCloseOperation(关闭时退出);
这个包();
这是setTitle(“弹跳球”);
此.setVisible(true);
//创建一个新线程以定期运行更新
Thread updateThread=新线程(){
@凌驾
公开募捐{
while(true){
update();//更新(x,y)位置
repaint();//刷新JFrame。回调paintComponent()
试一试{
//延迟并给其他线程一个运行的机会
Thread.sleep(更新间隔);//毫秒
}捕获(中断异常忽略){
}
}
}
};
updateThread.start();//调用回运行()
}
//更新移动对象的(x,y)位置
公共无效更新(){
x+=x速度;
y+=y速度;
r=新随机数();
如果(x>CANVAS|u WIDTH-size | x<0){
xSpeed=-xSpeed;
ballColor=新颜色(r.nextInt(256)、r.nextInt(256)、r.nextInt(256));
}
如果(y>CANVAS|U高度-大小| y<0){
ySpeed=-ySpeed;
ballColor=新颜色(r.nextInt(256)、r.nextInt(256)、r.nextInt(256));
}
}
//定义内部类DrawCanvas,它是用于自定义绘图的JPanel
类DrawCanvas扩展了JPanel{
@凌驾
公共组件(图形g){
super.paintComponent(g);//绘制父对象的背景
挫折背景(颜色:黑色);
g、 setColor(ballColor);
g、 圆角椭圆(x,y,大小,大小);//画一个圆
}
}
//输入主方法
公共静态void main(字符串[]args){
//在事件调度线程中运行GUI代码以确保线程安全
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
new CGBouncingBall();//让构造函数来完成这项工作
}
});
}
}
你的get()方法做什么?与g.setColor(Color.BLACK)相同;我把它放入了一个方法中,但不一定非得如此。它可以写为g.setColor(Color.BLACK);而是在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
        }
    });
}
}