//******************************************************************** // Reboun
ID: 3568327 • Letter: #
Question
//********************************************************************
// Rebound.java
// 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
// Represents the primary panel for the Rebound program.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ReboundPanel extends JPanel
{
private static final long serialVersionUID = 1L;
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 (getClass().getResource("happyFace.gif"));
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();
}
}
class MousePressListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
if(event.getX() <= 30 && event.getY() <= 30)
{
if(event.getX() <= 30 && event.getY() <= 30)
{
image = new ImageIcon(getClass().getResource("redSmiley.gif"));
}
else
image = new ImageIcon(getClass().getResource("happyFace.gif"));
}
}
public void mouseClicked(MouseEvent event)
{
if(event.getX() <= 30 && event.getY() <= 30)
{
image = new ImageIcon(getClass().getResource("redSmiley.gif"));
}
else
image = new ImageIcon(getClass().getResource("happyFace.gif"));
}
//Empty methods
public void mouseEntered(MouseEvent arg0)
{
}
public void mouseExited(MouseEvent arg0)
{
}
public void mouseReleased(MouseEvent arg0)
{
}
}
}
I need the two images at the top to switch between each other when the mouse is clicked within 30 pixels of the image. I need help getting the button click to be registered and to switch the images whenever they are clicked.
Explanation / Answer
/** * Rebound.java * * Demonstrates an animation and the use of the Timer class. * @author Lewis and Loftus. Converted to JApplet by Scot Drysdale */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Rebound extends JApplet { private static final long serialVersionUID = 1L; private final int APPLET_WIDTH = 200; private final int APPLET_HEIGHT = 100; private final int IMAGE_SIZE = 35; private final int DELAY = 20; private Timer timer; // Timer object to get regular callbacks private Image image; // Image of thing to be drawn private int x, y, moveX, moveY; // /** * Sets up the applet, including the timer for the animation. */ public void init() { addMouseListener(new ReboundMouseListener()); Container cp = getContentPane(); // Content pane holds components cp.add(new Canvas()); // The canvas is the only component x = 0; y = 40; moveX = moveY = 3; image = getImage(getCodeBase(), "happyFace.gif"); setBackground(Color.black); setSize(APPLET_WIDTH, APPLET_HEIGHT); timer = new Timer(DELAY, new ReboundActionListener()); timer.start(); setVisible(true); // Makes the applet (and its components) visible } private class Canvas extends JPanel { private static final long serialVersionUID = 1L; /** * Draws the image in the current location. * @param page the graphics object to draw on. */ public void paintComponent(Graphics page) { super.paintComponent(page); page.drawImage(image, x, y, this); } } /** * Represents the mouse listener for the applet. */ private class ReboundMouseListener implements MouseListener { /** * Stops or starts the timer (and therefore the animation) * when the mouse button is clicked. * @param event the event that caused this callback */ public void mouseClicked(MouseEvent event) { if (timer.isRunning()) timer.stop(); else timer.start(); } //-------------------------------------------------------------- // Provide empty definitions for unused event methods. //-------------------------------------------------------------- public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} } /** * Represents the action listener for the timer. */ private class ReboundActionListener implements ActionListener { /** * Updates the position of the image and possibly the direction * of movement whenever the timer fires an action event. * @param event the event that caused this callback */ public void actionPerformed(ActionEvent event) { x += moveX; y += moveY; if (x = APPLET_WIDTH - IMAGE_SIZE) moveX = moveX * -1; if (y = APPLET_HEIGHT - IMAGE_SIZE) moveY = moveY * -1; repaint(); } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.