import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import javax.swing.JFrame; /** * */ /** * @author Donald Yessick * */ public class Lab14 extends PiDarts { //throwDart(int x, int y); public static void main(String[] args) { Lab14 piDart = new Lab14(); JFrame frame = piDart.getWindow(); frame.setVisible(true); for (int i = 0; i < 10; i++){ piDart.wait(15000); piDart.randomDemo(); piDart.wait(15000); piDart.repaint(); } } public void mouseClicked(MouseEvent mouseEvent) { throwExampleDarts(mouseEvent.getX(),mouseEvent.getY()); } void throwExampleDarts(int x, int y){ //place a dart at the mouse x -= 5; //offset in dart picture y -= 55; //title bar + offset in dart picture throwDart(x,y); wait(1000); //introduce a small delay //place a dart randomly x = (int)(Math.random() * this.getWidth()); y = (int)(Math.random() * this.getHeight()); throwDart(x,y); wait(1000); //introduce a small delay //place a dart near the bulls-eye double range = 300; double min = this.getHeight()/2-150; x = (int)Math.rint((Math.random() * range + min)); y = (int)Math.rint((Math.random() * range + min)); throwDart(x,y); } void randomDemo(){ int red, green, blue; int alpha; //opaqueness/transparency level int x, y; int w,h; int count = (int)(Math.random() * 900)+100; //between 100 and 1000 double probabilityCircle = .25; //25% Graphics g = super.getGraphics(); //our parent class is a JComponent int screenWidth = super.getWidth(); int screenHeight = super.getHeight(); for (int i = 0; i < count; i++){ red = (int)(Math.random()*256); green = (int)(Math.random()*256); blue = (int)(Math.random()*256); alpha = (int)(Math.random()*256); Color c = new Color(red, green,blue,alpha); // a random color w = (int)(Math.random() * screenWidth/2); h = (int)(Math.random() * screenHeight/2); x = (int)(Math.random() * screenWidth-w); y = (int)(Math.random() * screenHeight-h); g.setColor(c); if (Math.random() < probabilityCircle){ g.fillOval(x, y, w, h); } else{ g.fillRect(x, y, w, h); } } } }