Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

mai package rebound; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; public class MainFrame { public static void main(String[] args) { JFrame frame = new JFrame(Rebound); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mai

package rebound;

import java.awt.BorderLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class MainFrame {

public static void main(String[] args) {

JFrame frame = new JFrame("Rebound");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(800, 500);

frame.getContentPane().add(new MainPanel());

frame.pack();

frame.setVisible(true);

}

ReboundPanel.java

package guiAnimation;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ReboundPanel extends JPanel {

private int delay = 20;

private int height;

private int width;

private ImageIcon image;

private Timer timer;

private int x, y, moveX, moveY;

//-----------------------------------------------------------------

// Sets up the panel, including the timer for the animation.

//-----------------------------------------------------------------

public ReboundPanel(int w, int h)

{

width = w;

height = h;

timer = new Timer(delay, new ReboundListener());

image = new ImageIcon (this.getClass().getResource("/guiAnimation/Dog.gif"));

// image = new ImageIcon (this.getClass().getResource("/guiAnimation/sun.gif"));

x = 0;

y = 40;

moveX = moveY = 3;

setPreferredSize (new Dimension(w, h));

setBackground (Color.LIGHT_GRAY);

timer.start();

}

public void setDelay (int d) {

timer.setDelay(d);

}

//-----------------------------------------------------------------

// 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.getIconWidth())

moveX = moveX * -1;

if (y <= 0 || y >= height-image.getIconHeight())

moveY = moveY * -1;

repaint();

}

}

}

MainPanel.java

package guiAnimation;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MainPanel extends JPanel {

private final int WIDTH = 800, HEIGHT = 500;

private int delay;

private JPanel controlPanel;

private ReboundPanel imagePanel;

private JButton showIcon;

private JButton changeSpeed;

//-----------------------------------------------------------------

// Sets up the panel, including the timer for the animation.

//-----------------------------------------------------------------

public MainPanel()

{

super(new BorderLayout());

delay = 20;

controlPanel = new JPanel();

controlPanel.setPreferredSize (new Dimension(WIDTH / 2, HEIGHT));

controlPanel.setBackground (Color.black);

showIcon = new JButton("show animation");

showIcon.addActionListener(new ShowIconListener());

controlPanel.add(showIcon);

changeSpeed = new JButton("change speed");

changeSpeed.addActionListener(new ChangeSpeedListener());

controlPanel.add(changeSpeed);

imagePanel = new ReboundPanel(WIDTH/2,HEIGHT);

setPreferredSize (new Dimension(WIDTH, HEIGHT));

setBackground (Color.white);

add(controlPanel, BorderLayout.WEST);

add(imagePanel, BorderLayout.EAST);

}

private class ShowIconListener implements ActionListener {

public void actionPerformed (ActionEvent event) {

if (imagePanel.isVisible())

imagePanel.setVisible(false);

else

imagePanel.setVisible(true);

}

}

private class ChangeSpeedListener implements ActionListener {

public void actionPerformed (ActionEvent event) {

do {

try {

String delayStr = JOptionPane.showInputDialog("Enter an integer 0-100");

delay = Integer.parseInt(delayStr);

} catch (Exception e) {

delay = -1;

}

} while (delay < 0 || delay > 100);

imagePanel.setDelay(delay);

}

}

}

You will need to drag the Dog.gif and the sun.gif files into your package.

Run this and study the code to understand:

What is MainFrame for? What does it contain?

What is MainPanel for? How big is it?

Inside MainPanel:

What is the purpose of super(new BorderLayout());

What is controlPanel for? How big is it? Which container holds it? Where is it placed in the container?

What is ReboundPanel for? Where is it located?

What does the following code do? try { String delayStr = JOptionPane.showInputDialog("Enter an integer 0-100"); delay = Integer.parseInt(delayStr);

} catch (Exception e) {

delay = -1;

}

In the previous code what is JOptionPane? What does it return?

Why is some of the code inside a try..catch?

Why is the previous code in a do while loop?

Inside ReboundPanel:

What is a timer?

What is paintComponent (page)

What is repaint ();

Change the instantiation of the ImageIcon to sun.gif. What is the difference?

What is the purpose for setDelay(int d)? Where is it called?

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

What applied experiences do you have? (For Applied Programs Only)

Answered: 1 week ago

Question

Understand human resources role in performance appraisals

Answered: 1 week ago