Question: My program doesn't have slider to control speed, no wheel functionality, and the right and left mouse button do the same thing Paintin' & Animatin'

My program doesn't have slider to control speed, no wheel functionality, and the right and left mouse button do the same thing

Paintin' & Animatin'

For Proj 1, you'll build on the Dots.java and DotsPanel.java programs (you may revert to the originals, or use your Project 1 as a foundation):

Add the ability to draw continuously while dragging; Add the ability to change the size of new dots by scrolling the mouse wheel up/down; Add animation using a Timer so that the dots all move when drawn.

Add an array for the colors of each dot, make each color random (for fun, use four ints from 0-255 instead of three, to give dots transparency), make each dot move at a different random speed (in x and y directions), and have the dots "bounce" appropriately off all four sides of the screen.

Add the ability to "group" dots whenever the mouse is dragged (hint: when pressed, remember that dot's speed x/y, then whenever dragging, use that speed x/y for all the 'dragged' dots...).

Add a 10 point slider that will act as a speed multiplier. So when you adjust the slider the current stored speed for the dot will increase by a factor of the current position of the slider.

Have the left click do one thing (e.g. "bubbles") and the right mouse click do another ("group").

Submit just the Java source code files from your project's 'src' folder. The files should contain the following lines of comments (or similar) at the top

Additional Deliverable:

Dots.java

DotsPanel.java

************************************************************

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); } }

******************************************************

import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Random; import javax.swing.JPanel; import javax.swing.Timer; public class DotsPanel extends JPanel { private final int SIZE = 6; // radius of each dot private ArrayList pointList; AnimationListener am; // ----------------------------------------------------------------- // Constructor: Sets up this panel to listen for mouse events. // ----------------------------------------------------------------- public DotsPanel() { pointList = new ArrayList(); // addMouseListener(new DotsListener()); addMouseMotionListener(new DotsMouseMotionListener()); am = new AnimationListener(); am.timer.start(); setBackground(Color.black); setPreferredSize(new Dimension(300, 200)); } private void setColor(Color color) { } public void paint(Graphics page) { super.paintComponent(page); 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.fillRect(spot.x - SIZE, spot.y - SIZE, SIZE * 2, SIZE * 2); page.drawString("Count: " + pointList.size(), 5, 15); } private class AnimationListener implements ActionListener { int xRand[]; int yRand[]; Random rand; public AnimationListener() { rand = new Random(); xRand = new int[1000]; yRand = new int[1000]; for (int i = 0; i < 1000; i++) { xRand[i] = rand.nextInt(20) - 10; yRand[i] = rand.nextInt(20) - 10; } } private Timer timer = new Timer(50, this); public void actionPerformed(ActionEvent e) { Rectangle window = getBounds(); for (int i = 0; i < pointList.size(); i++) { Point spot = pointList.get(i); spot.x += xRand[i]; spot.y += yRand[i]; if (spot.x <= 0) { xRand[i] = Math.abs(xRand[i]); } else if (spot.x >= window.width - (SIZE * 2)) { xRand[i] = -Math.abs(xRand[i]); } if (spot.y <= 0) { yRand[i] = Math.abs(yRand[i]); } else if (spot.y >= window.height - (SIZE * 2)) { yRand[i] = -Math.abs(yRand[i]); } } repaint();

} } private class DotsListener implements MouseListener { public void mousePressed(MouseEvent event) { pointList.add(event.getPoint()); repaint(); } public void mouseClicked(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } private class DotsMouseMotionListener implements MouseMotionListener { public void mouseDragged(MouseEvent event) { System.out.println(event.getPoint().toString()); pointList.add(event.getPoint()); repaint(); } public void mouseMoved(MouseEvent e) { }

}

}

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!