import java.awt.Color; 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 CannonBall extends JComponent implements MouseListener { public static void main(String[] args) { CannonBall cannonBall = new CannonBall(); JFrame frame = cannonBall.getWindow(); frame.setVisible(true); cannonBall.shoot(); } /* 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) {} JFrame getWindow() { // this is the lab8 object JFrame window = new JFrame(this.toString()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(this.getToolkit().getScreenSize().width,this.getToolkit().getScreenSize().height/2); window.setContentPane(this); window.addMouseListener(this); return window; } 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; } private static BufferedImage cannon, cannonBall; //images private static URL urlcannon, urlcannonBall; //urls for images public CannonBall(){ try { urlcannonBall = new URL( "http://dyessick.com/Coastal/Spring2008/csci140lab/images/cannonball.PNG"); urlcannon = new URL( "http://dyessick.com/Coastal/Spring2008/csci140lab/images/cannon.PNG"); cannonBall = makeColorTransparent(ImageIO.read(urlcannonBall), new Color(255, 255, 255)); cannon = makeColorTransparent(ImageIO.read(urlcannon), 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); } } int xCannonBall=0; int yCannonBall=0; boolean fireCannon = false; public void shoot(){ int balls = 9; while (balls -- > 0) { xCannonBall=0; yCannonBall=0; fireCannon = true; int time = 0; while (time < 110) { xCannonBall = getXCannonBall(time); yCannonBall = getYCannonBall(time); //drawCannonBall(xCannonBall, yCannonBall); try { Thread.sleep(25); //wait 1/3 seconds } catch (InterruptedException exception) { exception.printStackTrace(); } repaint(); time++; } repaint(); try { Thread.sleep(2000); //wait 3 seconds } catch (InterruptedException exception) { exception.printStackTrace(); } } } public int getXCannonBall(int time){ return (getWidth()-350)*time/100+250; } public int getYCannonBall(int time){ return (((time-50)*(time-50))/10)+100; } public void drawCannonBall(int x, int y){ Graphics graphics = this.getGraphics(); Graphics2D g = (Graphics2D)(graphics); drawCannonBall(g,xCannonBall, yCannonBall); } public void drawCannonBall(Graphics2D g, int x, int y){ g.drawImage( //the image cannonBall, //top left coordinate to place image x,y, ////bottom right coordinate to scale image x+50, y+50, //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 cannonBall.getWidth(),cannonBall.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 String toString(){ return "CannonBall"; } public void paintComponent(Graphics graphics){ Graphics2D g = (Graphics2D)(graphics); if (fireCannon){ drawCannonBall(g,xCannonBall, yCannonBall); } g.drawImage( //the image cannon, //top left coordinate to place image 0,getHeight()-300, ////bottom right coordinate to scale image 300, 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 cannon.getWidth(),cannon.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 ); } }