import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.awt.image.FilteredImageSource; import java.awt.image.ImageFilter; import java.awt.image.ImageProducer; import java.awt.image.RGBImageFilter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.JComponent; import javax.swing.JFrame; /** * */ /** * @author Donald Yessick * */ public class PiDarts extends JComponent implements MouseListener { final int MAX_NUMBER_OF_DARTS = 100000; private static BufferedImage dartBoard, dart; //images private static URL urlDartBoard, urlDart; //urls for images public PiDarts(){ try { urlDartBoard = new URL( "http://dyessick.com/Coastal/Spring2008/csci140lab/images/dartboard.PNG"); urlDart = new URL( "http://dyessick.com/Coastal/Spring2008/csci140lab/images/dart.PNG"); dartBoard = makeColorTransparent(ImageIO.read(urlDartBoard), new Color(0, 0, 0)); dart = makeColorTransparent(ImageIO.read(urlDart), new Color(255, 255, 255)); } catch (MalformedURLException badUrl) { badUrl.printStackTrace(); System.out.println("ABORTING: " + badUrl.toString()); System.exit(0); } catch (IOException ioError) { ioError.printStackTrace(); System.out.println("ABORTING: " + ioError.toString()); System.exit(0); } catch (Exception otherError) {// this alone is sufficient otherError.printStackTrace(); System.out.println("ABORTING: " + otherError.toString()); System.exit(0); } } public static void main(String[] args) { PiDarts piDart = new PiDarts(); JFrame frame = piDart.getWindow(); frame.setVisible(true); piDart.againAndAgain(); } JFrame getWindow() { // this is the lab8 object JFrame window = new JFrame(this.toString()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int dim = Math.min(this.getToolkit().getScreenSize().width, this.getToolkit().getScreenSize().height); window.setSize(dim-30,dim); window.setContentPane(this); window.addMouseListener(this); return window; } public void paint(Graphics graphics){ Graphics2D g = (Graphics2D)(graphics); g.drawImage( //the image dartBoard, //top left coordinate to place image 0,0, ////bottom right coordinate to scale image getWidth(), getHeight(), //where to start in the image --you can choose part of the image 0, 0, //where to end in the image --you can choose part of the image dartBoard.getWidth(),dartBoard.getHeight(), //transparent fill color, e.g. Color.YELLOW, leave as null, or omit to leave transparent null, //ImageObserver -- null will do -- can be used to alert when null ); } public void againAndAgain(){ while (true) { int count = 4; while (count < MAX_NUMBER_OF_DARTS) { throwDarts(count); try { Thread.sleep(3000); //wait 3 seconds } catch (InterruptedException exception) { exception.printStackTrace(); } repaint(); try { Thread.sleep(1000); //wait 1 seconds (this allows screen to paint) } catch (InterruptedException exception) { exception.printStackTrace(); } count *= 4; //let count grow } } } public void throwDarts(int numThrows){ Graphics graphics = this.getGraphics(); Graphics2D g = (Graphics2D)(graphics); int throwsLeft = numThrows; while (throwsLeft > 0){ double dx = Math.random(); double dy = Math.random(); int x = (int)(getWidth()*dx); int y = (int)(getHeight()*dy); int scale = (int) (Math.max(1, .5*Math.log(numThrows))); x-=5; y-=10; //tip is not at top of pic g.drawImage( //the image dart, //top left coordinate to place image x,y, ////bottom right coordinate to scale image x+dart.getWidth()/scale, y+dart.getHeight()/scale, //where to start in the image --you can choose part of the image 0, 0, //where to end in the image --you can choose part of the image dart.getWidth(),dart.getHeight(), //transparent fill color, e.g. Color.YELLOW, leave as null, or omit to leave transparent null, //ImageObserver -- null will do -- can be used to alert when null ); throwsLeft--; try { Thread.sleep(7000/numThrows); } catch (InterruptedException exception) { exception.printStackTrace(); } } } public String toString(){ return "Pi by Darts"; } public void wait(int time){ try { Thread.sleep(time); } catch (InterruptedException exception) { exception.printStackTrace(); } } public void throwDart(int x, int y){ Graphics graphics = this.getGraphics(); Graphics2D g = (Graphics2D)(graphics); y-=5; //tip is not at top of pic g.drawImage( //the image dart, //top left coordinate to place image x,y, ////bottom right coordinate to scale image x+2*dart.getWidth(), y+2*dart.getHeight(), //where to start in the image --you can choose part of the image 0, 0, //where to end in the image --you can choose part of the image dart.getWidth(),dart.getHeight(), //transparent fill color, e.g. Color.YELLOW, leave as null, or omit to leave transparent null, //ImageObserver -- null will do -- can be used to alert when null ); } public static BufferedImage makeColorTransparent(BufferedImage bim, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; public final int filterRGB(int x, int y, int rgb) { if ((rgb | 0xFF000000) == markerRGB) {// Mark the alpha bits // as zero - transparent return 0x00FFFFFF & rgb; } else {// nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(bim.getSource(), filter); Image im = Toolkit.getDefaultToolkit().createImage(ip); return convert(im); } static public BufferedImage convert(Image im) { BufferedImage bi = new BufferedImage(im.getWidth(null), im .getHeight(null), BufferedImage.TYPE_4BYTE_ABGR); Graphics bg = bi.getGraphics(); bg.drawImage(im, 0, 0, null); bg.dispose(); return bi; } /* required for for mouse listener */ public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mousePressed(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0) {} public void mouseClicked(MouseEvent mouseEvent) {} }