Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Change the animation of the square so that once its leading edge hits the right edge of the frame the animation reverses and it moves

Change the animation of the square so that once its leading edge hits the right edge of the frame the animation reverses and it moves back across the frame. Once its leading edge touches the left edge of the frame it once again rebounds and starts moving back across the frame again. This continues indefinitely. b. Change the animation of the circle so that once the frame is entirely filled with the circle, the animation reverses and the circle shrinks back to its original size. Once it has reached its original size it starts growing again, until it once again fills the frame. This continues indefinitely.

Below is my code

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

public class AnimatedGUI { private JFrame frame; private int square; private int circle; private int diameter; private boolean animateShapes; private boolean shapeAnimate; private JButton startStopAnimationButton; private JButton startStopAnimateButton; private ShapesDrawPanel drawPanel;

public AnimatedGUI(){ square = 1; circle = 1; diameter=50; animateShapes = true; shapeAnimate = true; startStopAnimationButton = new JButton("Red Square Button"); startStopAnimateButton = new JButton("Blue Circle Button"); drawPanel = new ShapesDrawPanel(); }

public static void main (String[] args){ AnimatedGUI gui = new AnimatedGUI(); gui.go(); }

public void go(){ frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(startStopAnimationButton, BorderLayout.PAGE_START); frame.getContentPane().add(startStopAnimateButton, BorderLayout.PAGE_END); frame.getContentPane().add(drawPanel, BorderLayout.CENTER); startStopAnimationButton.addActionListener(new SRButtonListener()); startStopAnimateButton.addActionListener(new CBButtonListener());

frame.setSize(500,500); frame.setVisible(true);

}

class SRButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { animateShapes=!animateShapes; drawPanel.repaint(); } };

class CBButtonListener implements ActionListener {

public void actionPerformed (ActionEvent e) { shapeAnimate=!shapeAnimate; drawPanel.repaint(); } };

class ShapesDrawPanel extends JPanel{ public void paintComponent (Graphics g){ Graphics2D g2=(Graphics2D)g; g2.setColor(Color.yellow); g2.fillRect(0,0,this.getWidth(), this.getHeight()); g2.setColor(Color.blue); g2.fillOval((this.getWidth()-(diameter+circle))/2,(this.getHeight()-(diameter+circle))/2,diameter+circle,diameter+circle); g2.setColor(Color.red); g2.fillRect(square,0,50,50);

if (animateShapes) animateShapes(); if (shapeAnimate) shapeAnimate();

}

public void animateShapes(){ try{ Thread.sleep(5); } catch (Exception ex){} square++; repaint(); }

public void shapeAnimate(){ try{ Thread.sleep(5); } catch (Exception ex){} circle++; repaint(); }

} }

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

More Books

Students also viewed these Databases questions

Question

9. Make sure goals are internalized and accepted by the athlete.

Answered: 1 week ago

Question

What has been your desire for leadership in CVS Health?

Answered: 1 week ago