Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project 1: 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: 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 right 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

// FileName.java // by Name // CSCI 1302 // Project 1 // Date

Additional Deliverable:

Dots.java

DotsPanel.java

Here is a demo of what I am looking for. Your submission does not have to be exactly like this but the functionality should be there:

At the 4 second mark I draw dots while dragging with the left mouse button.

At the 7 second mark I group all the dots with the right mouse button.

At the 20 second I adjust the speed of all the dots with the slider.

import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener;

public class Dots2 { public static void main(String[] args) { JFrame frame=new JFrame("Dots"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JSlider slider; slider=new JSlider(); slider.setMaximum(10); slider.setMinimum(1); slider.setMajorTickSpacing(1); slider.setPaintTicks(true); slider.setValue(1); final DotsPanel dotsPanel=new DotsPanel(); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(dotsPanel,BorderLayout.CENTER); frame.getContentPane().add(slider,BorderLayout.PAGE_END); frame.pack(); frame.setVisible(true); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { int value=slider.getValue(); dotsPanel.setSpeedMultiplier(value); } }); } }

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.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; 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 { int speedMultiplier=1; int sizes[]; private int SIZE=6; private ArrayListpointList; Color colors[]; AnimationListener am; SecureRandom randomNum; public DotsPanel() { pointList=new ArrayList(); sizes=new int[1000]; colors=new Color[1000]; randomNum=new SecureRandom(); addMouseMotionListener(new DotsMouseMotionListener()); addMouseWheelListener(new DotsMouseMotionListener()); addMouseListener(new DotsMouseMotionListener()); am=new AnimationListener(); am.timer.start(); setBackground(Color.black); setPreferredSize(new Dimension(300,200)); initializeColors(); } private void initializeColors() { for(int i=0;i=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 void addPoint(Point p) { int currIndex=0; if(pointList.size()<1000) { pointList.add(p); currIndex=pointList.size()-1; sizes[currIndex]=SIZE; repaint(); } } private void bubble(Point p) { for(Point point:pointList) { if(Math.abs(point.x-p.x)<100&&Math.abs(point.y-p.y)<100) { point.x=-p.x; point.y=-p.y; } } } private void group(Point p) { for(Point point:pointList) { if(Math.abs(point.x-p.x)<100&&Math.abs(point.y-p.y)<100) { point.x=p.x; point.y=p.y; } } } private class DotsMouseMotionListener implements MouseMotionListener,MouseWheelListener,MouseListener { public void mouseDragged(MouseEvent event) { addPoint(event.getPoint()); } public void mouseMoved(MouseEvent e) { } public void mouseWheelMoved(MouseWheelEvent mwe) { int hits=mwe.getWheelRotation(); if(hits>0) { if(SIZE>3) { SIZE--; } } else { SIZE++; } } public void mouseClicked(MouseEvent me) { Point type=me.getPoint(); if(me.getButton()==MouseEvent.BUTTON1) { group(type); } else { bubble(type); } } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } } }

I have everything correct but I need to group all of the dots with the right click function. Right now it just groups dots near the mouse and it needs to group all of the dots that are out. The squares also only go to the left and upper edge when the side is adjusted and it needs to go to all edges randomly.

This was the feedback from my professor: Needs to group all the dots. You also need to look at why the squares are going beyond the left and upper edge and not going to the right and bottom edge after I adjust the size of the squares.

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

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

Demonstrates a real belief and passion for change.

Answered: 1 week ago