This change can be changed to a specific color
I am trying to change the color of the ball when it hits either side of the wall. I can change the color, but then it goes back to the original color.
This is the section of code I am dealing with. I need help. Is there a way to keep the color change?
If you get to the end of the code, you can see part of the comment // original balloon color. I'm pretty sure that part of the code is the reason it reverts to its original color and doesn't change color. Is there a way to keep the color that it also changes 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);
}
}
}
+3
source to share
1 answer
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 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
}
});
}
}
0
source to share