Question
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
- 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.
rewrite this code to implement the above^
// ****************************************************************** // 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 // -------------------------------------------- public SpeedControlPanel () { timer = new Timer(30, new ReboundListener()); this.setLayout (new BorderLayout()); bouncingBall = new Circle(BALL_SIZE); moveX = moveY = 5; // Set up a slider object here setPreferredSize (new Dimension (WIDTH, HEIGHT)); setBackground(Color.black); timer.start(); } // --------------------- // Draw the ball // --------------------- public void paintComponent (Graphics page) { super.paintComponent (page); bouncingBall.draw(page); } // *************************************************** // An action listener for the timer // *************************************************** public class ReboundListener implements ActionListener { // ---------------------------------------------------- // actionPerformed is called by the timer -- it updates // the position of the bouncing ball // ---------------------------------------------------- public void actionPerformed(ActionEvent action) { bouncingBall.move(moveX, moveY); // change direction if ball hits a side int x = bouncingBall.getX(); int y = bouncingBall.getY(); if (x < 0 || x >= WIDTH - BALL_SIZE) moveX = moveX * -1; if (y <= 0 || y >= HEIGHT - BALL_SIZE) moveY = moveY * -1; repaint(); } } // *************************************************** // A change listener for the slider. // *************************************************** private class SlideListener implements ChangeListener { // ------------------------------------------------ // Called when the state of the slider has changed; // resets the delay on the timer. // ------------------------------------------------ public void stateChanged (ChangeEvent event) { } } }
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