Question
ex3-4:Bouncing Ball Perform an animation involving a specific image within the ball in relation to the program. import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings(serial) public
ex3-4:Bouncing Ball Perform an animation involving a specific image within the ball in relation to the program.
import java.awt.*; import java.awt.event.*; import javax.swing.*;
@SuppressWarnings("serial") public class CGBouncingBallSwingTimer extends JFrame { private static final int CANVAS_WIDTH = 640; private static final int CANVAS_HEIGHT = 480; private static final int UPDATE_PERIOD = 50; // milliseconds private DrawCanvas canvas; private int x = 100, y = 100; private int size = 250; private int xSpeed = 3, ySpeed = 5;
public CGBouncingBallSwingTimer() { 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); ActionListener updateTask = new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { update(); repaint(); } }; new Timer(UPDATE_PERIOD, updateTask).start(); } public void update() { x += xSpeed; y += ySpeed; if (x > CANVAS_WIDTH - size || x < 0) { xSpeed = -xSpeed; } if (y > CANVAS_HEIGHT - size || y < 0) { ySpeed = -ySpeed; } } private class DrawCanvas extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.BLACK); g.setColor(Color.BLUE); g.fillOval(x, y, size, size); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new CGBouncingBallSwingTimer(); } }); } }
write java code plz :) thank you
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started