Question
The codes Dots.java and DotsPanel.java is the base code. Edit the code to make them squares instead on circles. Also make each square move at
The codes Dots.java and DotsPanel.java is the base code. Edit the code to make them squares instead on circles. Also make each square move at random speed and have the squares "bounce" appropriately off all four sides of the screen.
Instead of drawing dots you will draw squares so add that change.
Add animation using a Timer so that the squares all move when drawn. Make each square move at random speed (in x and y directions), and have the squares "bounce" appropriately off all four sides of the screen.
HINTS:
Create 2 arrays of size 1000 to hold random values between 1 and 10 for the X movements and Y movements. This will allow your rectangles to have independent movement speeds.
You will need to create a ActionListener class similar to what is used in ReboundPanel class.
Inside the Actionistener class you create you will want to reference the X and Y array indexes rather than the static moveX and moveY used in the ReboundPanel.
Dots.java
import javax.swing.JFrame;
public class Dots { //----------------------------------------------------------------- // Creates and displays the application frame. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Dots"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DotsPanel());
frame.pack(); frame.setVisible(true); } }
DotsPanel.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.security.SecureRandom;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DotsPanel extends JPanel {
private final int SIZE = 6; // radius of each dot
private ArrayList pointList;
// -----------------------------------------------------------------
// Constructor: Sets up this panel to listen for mouse events.
// -----------------------------------------------------------------
public DotsPanel() {
pointList = new ArrayList();
addMouseListener(new DotsListener());
setBackground(Color.black);
setPreferredSize(new Dimension(300, 200));
}
private void setColor(Color color) {
}
// -----------------------------------------------------------------
// Draws all of the dots stored in the list.
// -----------------------------------------------------------------
// Step 2 : Changed the paintComponent to paint.
public void paint(Graphics page) {
super.paintComponent(page);
// Step 3 : created the SecureRandom Generator.
SecureRandom randomNum = new SecureRandom();
Color color = new Color(randomNum.nextInt(256), randomNum.nextInt(256),
randomNum.nextInt(256));
page.setColor(color);
for (Point spot : pointList)
page.fillOval(spot.x - SIZE, spot.y - SIZE, SIZE * 2, SIZE * 2);
page.drawString("Count: " + pointList.size(), 5, 15);
}
// *****************************************************************
// Represents the listener for mouse events.
// *****************************************************************
private class DotsListener implements MouseListener {
// --------------------------------------------------------------
// Adds the current point to the list of points and redraws
// the panel whenever the mouse button is pressed.
// --------------------------------------------------------------
public void mousePressed(MouseEvent event) {
pointList.add(event.getPoint());
repaint();
}
// --------------------------------------------------------------
// Provide empty definitions for unused event methods.
// --------------------------------------------------------------
public void mouseClicked(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent 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