- Modify The Java Code So That When The Ball Bounces Off Some Wall Right Left Top Bottom Either The Color Changes T 1 (88.87 KiB) Viewed 43 times
Modify the Java code so that when the ball bounces off some wall (right, left, top, bottom), either the color changes, t
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Modify the Java code so that when the ball bounces off some wall (right, left, top, bottom), either the color changes, t
Modify the Java code so that when the ball bounces off some wall (right, left, top, bottom), either the color changes, the shape changes (to rectangle say), or the size changes (bigger, smaller etc.). Make your Java code changes to the possible combinations of color, shape, size, etc. and capture the screens. 1 5 5 7 3 3 2 1 2 3 ie 5 5 7 3 L 2 3 1 2 3 1 5 5 3 3 5 import java.awt.*; public class Ball extends JFrame { private int x = 70, xChange = 7; private int y = 50, yChange = 2; private int diameter = 10; private int rectLeftx=50, rectRightX = 300; private int rectTopY= 50, rectBottomY = 300; public void paint (Graphics g) ( } O /** super.paint (g); g.drawRect (rectLeftx, rectTopy, for (int n = 1; n < 1000; n++) { Color backgroundColour = getBackground(); g.setColor (backgroundColour); g. filloval (x, y, diameter, diameter); if (x + xChange <= rectLeftX) xChange = -xChange; rectRightx rectLeftX, rectBottomY rectTopY); if (x+xChange + diameter >= rectRightX) xChange-xchange; if (y+yChange <= rectTopy) yChange-yChange; . if(y + yChange + diameter >= rectBottomy) yChange-yChange; x = x + xChange; y = y + yChange; try ( } g.setColor (Color.blue); g. filloval (x, y, diameter, diameter); Thread.sleep (20); //Thread.sleep (100); // slow down 10 times for screen capture } catch (InterruptedException e) { g.drawstring ("sleep exception", 20, 20); import javax.swing. JFrame; public class BallTest { * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Ball ball = new Ball (); ball.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ; ball.setSize (600, 600); ball.setVisible (true); System.out.println ("Ball started");