Question
in java please show output the code compiles correctly. However the program does not yield the expected result. un this code, see what the result
in java please show output the code compiles correctly. However the program does not yield the expected result. un this code, see what the result is and determine where the problem lies
/******************************************************************** // Rebound.java Author: Lewis/Loftus 9.15 // // Demonstrates an animation and the use of the Timer class. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Rebound { //----------------------------------------------------------------- // Displays the main frame of the program. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Rebound"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ReboundPanel()); frame.pack(); frame.setVisible(true); } }
/******************************************************************** // ReboundPanel.java Author: Lewis/Loftus 9.16 // // Represents the primary panel for the Rebound program. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ReboundPanel extends JPanel { private final int WIDTH = 300, HEIGHT = 100; private final int DELAY = 20, IMAGE_SIZE = 35; private ImageIcon image; private Timer timer; private int x, y, moveX, moveY; //----------------------------------------------------------------- // Sets up the panel, including the timer for the animation. //----------------------------------------------------------------- public ReboundPanel() { timer = new Timer(DELAY, new ReboundListener()); image = new ImageIcon("happyFace.jpg"); x = 0; y = 40; moveX = moveY = 3; setPreferredSize(new Dimension(WIDTH, HEIGHT)); setBackground(Color.black); timer.start(); } //----------------------------------------------------------------- // Draws the image in the current location. //----------------------------------------------------------------- public void paintComponent(Graphics page) { super.paintComponent(page); image.paintIcon(this, page, x, y); } //***************************************************************** // Represents the action listener for the timer. //***************************************************************** private class ReboundListener implements ActionListener { //-------------------------------------------------------------- // Updates the position of the image and possibly the direction // of movement whenever the timer fires an action event. //-------------------------------------------------------------- public void actionPerformed(ActionEvent event) { x += moveX; y += moveY; if (x <= 0 || x >= WIDTH-IMAGE_SIZE) moveX = moveX * -1; if (y <= 0 || y >= HEIGHT-IMAGE_SIZE) moveY = moveY * -1; repaint(); } } }
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