Dot Demo
<< Quicksort | LabTrailIndex | Day of Week >>
See a working example here
See a video introduction here (you'll need Quicktime installed or a mpeg4 player like VLC)
You need to make a Dot class to make this work
DotTester.java
import java.applet.Applet; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; public class DotTester extends Applet implements MouseListener { public static final int SIZE=10; /** * Declaring a ArrayList of Dots to * keep track of the Dots */ private ArrayList<Dot> dots; /** * the init method is automatically * called when we start an Applet */ public void init() { dots=new ArrayList<Dot>(); this.addMouseListener(this); } /** * the paint method is automatically * called when the screen needs to be updated */ public void paint(Graphics g) { for(Dot dot:dots) dot.draw(g); } /** * the following methods need to be implemented * in order to deal with the MouseListener interface * we will only use the mouseReleased method */ @Override public void mouseClicked(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) { // add a new dot dots.add(new Dot(e.getX(), e.getY(), SIZE )); repaint(); } }
Dot.java
import java.awt.Color; import java.awt.Graphics; public class Dot { /** * declare attributes */ // your code here /** * declare constructors */ //your code here public void draw(Graphics g) { //your code here } }
Now you try
- Make a overload constructor so you can construct a new Dot with a specified Color
- Make all the Dots red dots
- Make each Dot its own randomly chosen color