[英]It is possible to make this change to a certain color
我試圖改變球撞到牆壁任何一側時的顏色。 我可以讓它改變顏色,但之后會恢復原來的顏色。
這是我正在處理的代碼部分。 我需要協助。 有沒有辦法保持顏色變化?
如果你一直向下看代碼,你可以看到評論部分//初始球顏色。 我確信代碼的一部分是它恢復原始顏色並且不會改變顏色的原因。 當它撞到牆壁時,有沒有辦法保持它變化的顏色?
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);
}
}
}
我認為這段代碼可以滿足您的需求。
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.