Question: Speed Control The files SpeedControl and SpeedControlPanel contain a program (and its associated panel) with a circle that moves on the panel and rebounds from

Speed Control

The files SpeedControl and SpeedControlPanel contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle. Save the program to your directory and run it to see how it works. (WATCH OUT!! There may be one or two errors you need to resolve for the program to compile correctly). In this lab exercise you will add to the panel a slider that controls the speed of the animation. Study the code in SlideColorPanel (Listing 10.16 in the text) to help understand the steps below.

Set up a JSlider object. You need to

Declare it.

Instantiate it to be a JSlider that is horizontal with values ranging from 0 to 200, initially set to 30.

Set the major tick spacing to 40 and the minor tick spacing to 10.

Set paint ticks and paint labels to true and the X alignment to left.

Set up the change listener for the slider. A skeleton of a class named SlideListener is already in

SpeedControlPanel.java. You need to

Complete the body of the statedChanged function. This function must determine the value of the slider, then set the timer delay to that value. The timer delay can be set with the method setDelay (int delay) in the Timer class.

Add the change listener to the JSlider object.

Create a label (Timer Delay) for the slider and align it to the left.

Create a JPanel object and add the label and slider to it then add your panel to the SOUTH of the main

panel (note that it has already been set up to use a border layout).

Compile and run the program. Make sure the speed is changing when the slider is moved. (NOTE: Larger

delay means slower!)

You should have noticed one problem with the program. The ball (circle) goes down behind the panel the

slider is on. To fix this problem do the following:

In actionPerformed, declare a variable slidePanelHt (type int). Use the getSize() method to get the size (which is a Dimension object) of the panel you put the slider on. Assign slidePanelHt to be the height

of the Dimension object. For example, if your panel is named slidePanel the following assignment statement is what you need:

slidePanelHt = slidePanel.getSize().height;

Now use this height to adjust the condition that tests to see if the ball hits the bottom of the panel.

Test your program to make sure it is correct.

// ******************************************************************** // SpeedControl.java // // Demonstrates animation -- balls bouncing off the sides of a panel - // with speed controlled by a slider. // ******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SpeedControl { // ------------------------------------ // Sets up the frame for the animation. // ------------------------------------ public void static main (String[] args) { JFrame frame = new JFrame ("Bouncing Balls"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane.add(new SpeedControlPanel ()); frame.pack(); frame.setVisible(true); } }

// ****************************************************************** // SpeedControlPanel.java // // The panel for the bouncing ball. Similar to // ReboundPanel.java in Listing 8.16 in the text, except a circle // rather than a happy face is rebounding off the edges of the // window. // ****************************************************************** import java.awt.*; import java.awt.event.*; import javax. swing. *; import javax.swing.event.*; public class SpeedControlPanel extends JPanel { private final int WIDTH = 600; private final int HEIGHT = 400; private final int BALL_SIZE = 50; private Circle bouncingBall; // the object that moves private Timer timer; private int moveX, moveY; // increment to move each time // -------------------------------------------- // Sets up the panel, including the timer // for the animation // --------------------------------------------

//******************************************************************** // SlideColorPanel.java Authors: Lewis/Loftus // // Represents the slider control panel for the SlideColor program. //********************************************************************

import java.awt.*; import javax.swing.*; import javax.swing.event.*;

public class SlideColorPanel extends JPanel { private JPanel controls, colorPanel; private JSlider rSlider, gSlider, bSlider; private JLabel rLabel, gLabel, bLabel;

//----------------------------------------------------------------- // Sets up the sliders and their labels, aligning them along // their left edge using a box layout. //----------------------------------------------------------------- public SlideColorPanel() { rSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); rSlider.setMajorTickSpacing(50); rSlider.setMinorTickSpacing(10); rSlider.setPaintTicks(true); rSlider.setPaintLabels(true); rSlider.setAlignmentX(Component.LEFT_ALIGNMENT);

gSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); gSlider.setMajorTickSpacing(50); gSlider.setMinorTickSpacing(10); gSlider.setPaintTicks(true); gSlider.setPaintLabels(true); gSlider.setAlignmentX(Component.LEFT_ALIGNMENT);

bSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); bSlider.setMajorTickSpacing(50); bSlider.setMinorTickSpacing(10); bSlider.setPaintTicks(true); bSlider.setPaintLabels(true); bSlider.setAlignmentX(Component.LEFT_ALIGNMENT);

SliderListener listener = new SliderListener(); rSlider.addChangeListener(listener); gSlider.addChangeListener(listener); bSlider.addChangeListener(listener);

rLabel = new JLabel("Red: 0"); rLabel.setAlignmentX(Component.LEFT_ALIGNMENT); gLabel = new JLabel("Green: 0"); gLabel.setAlignmentX(Component.LEFT_ALIGNMENT); bLabel = new JLabel("Blue: 0"); bLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

controls = new JPanel(); BoxLayout layout = new BoxLayout(controls, BoxLayout.Y_AXIS); controls.setLayout(layout); controls.add(rLabel); controls.add(rSlider); controls.add(Box.createRigidArea(new Dimension(0, 20))); controls.add(gLabel); controls.add(gSlider); controls.add(Box.createRigidArea(new Dimension(0, 20))); controls.add(bLabel); controls.add(bSlider);

colorPanel = new JPanel(); colorPanel.setPreferredSize(new Dimension(100, 100)); colorPanel.setBackground(new Color(0, 0, 0)); add(controls); add(colorPanel); }

//***************************************************************** // Represents the listener for all three sliders. //***************************************************************** private class SliderListener implements ChangeListener { private int red, green, blue;

//-------------------------------------------------------------- // Gets the value of each slider, then updates the labels and // the color panel. //-------------------------------------------------------------- public void stateChanged(ChangeEvent event) { red = rSlider.getValue(); green = gSlider.getValue(); blue = bSlider.getValue();

rLabel.setText("Red: " + red); gLabel.setText("Green: " + green); bLabel.setText("Blue: " + blue);

colorPanel.setBackground(new Color(red, green, blue)); } } }

/******************************************************************** // SlideColor.java Authors: Lewis/Loftus // // Demonstrates the use slider components. //********************************************************************

import java.awt.*; import javax.swing.*;

public class SlideColor { //----------------------------------------------------------------- // Presents up a frame with a control panel and a panel that // changes color as the sliders are adjusted. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Slide Colors"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

frame.pack(); frame.setVisible(true); } }

// **************************************************************** // FILE: Circle.java // // Purpose: Define a Circle class with methods to create and draw // a circle of random size, color, and location. // **************************************************************** import java.awt.*; import java.util.Random; public class Circle { private int x, y; // coordinates of the corner private int radius; // radius of the circle private Color color; // color of the circle static Random generator = new Random(); //--------------------------------------------------------- // Creates a random circle with properties in ranges given: // -- radius 25..74 // -- color RGB value 0..16777215 (24-bit) // -- x-coord of upper left-hand corner 0..599 // -- y-coord of upper left-hand corner 0..399 //--------------------------------------------------------- public Circle() { radius = Math.abs(generator.nextInt())%50 + 25; color = new Color(Math.abs(generator.nextInt())% 16777216); x = Math.abs(generator.nextInt())%600; y = Math.abs(generator.nextInt())%400; } //--------------------------------------------------------- // Creates a circle of a given size (diameter). Other // attributes are random (as described above) //--------------------------------------------------------- public Circle(int size) { radius = Math.abs(size/2); color = new Color(Math.abs(generator.nextInt())% 16777216); x = Math.abs(generator.nextInt())%600; y = Math.abs(generator.nextInt())%400; } //--------------------------------------------------------- // Draws circle on graphics object given //--------------------------------------------------------- public void draw(Graphics page) { page. setColor (color) ; page.fillOval(x,y,radius*2,radius*2); } //--------------------------------------------------------- // Shifts the circle's position -- "over" is the number of // pixels to move horizontally (positive is to the right // negative to the left); "down" is the number of pixels // to move vertically (positive is down; negative is up) //--------------------------------------------------------- public void move (int over, int down) { x = x + over; y = y + down; } //---------------------------------------------- // Return the x coordinate of the circle corner //---------------------------------------------- public int getX() { return x; } //---------------------------------------------- // Return the y coordinate of the circle corner //---------------------------------------------- public int getY() { return y; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!