package class1_2_DuckStrategies_v0; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Point; import java.awt.image.BufferedImage; import java.io.File; import java.util.Random; import javax.imageio.ImageIO; import javax.swing.JComponent; /** * This class represents a virtual Ball. Ball extends JComponent * so that it can paint itself in a window. * @author hornick */ public class Duck extends JComponent { private static final int IMAGESIZE = 100; // jpg height/width in pixels private String name; // name of this Ball private Image image; // jpg image of this duck private Point currentPos; // the duck's current position private Point increment; // the duck's incremental displacement each time it moves private Random random; // a random number generator used for swimming private WavPlayer quacker; // used to voice duck sounds public Duck(String name) { this.name = name; String imageFile = "duck.jpg"; try{ // try to load the specified image file image = ImageIO.read( new File(imageFile));// load raw file image = image.getScaledInstance(IMAGESIZE, IMAGESIZE, BufferedImage.SCALE_SMOOTH); // scale to fixed size } catch( Exception e ) { // Aw, crud. Can't load the image file. // The following code demonstrates how to dynamically generate // an image in memory as a BufferedImage, to be displayed later. image = new BufferedImage(IMAGESIZE, IMAGESIZE, BufferedImage.TYPE_3BYTE_BGR); Graphics g = image.getGraphics(); Font f = new Font("Courier New", Font.ITALIC, 10); g.setFont(f); g.setColor(Color.RED); g.drawString(imageFile, 20, 20); g.drawString("not found", 20, 30); // note we can also draw lines etc in the BufferedImage // - maybe to create an outline of a generic duck - // here, we're just displaying a simple message. } setSize(IMAGESIZE, IMAGESIZE); // set the size of this duck's image random = new Random(); // create the random number generator // initial position of this duck - random x,y coordinates currentPos = new Point(random.nextInt(DuckPond.PONDSIZE - IMAGESIZE), random.nextInt(DuckPond.PONDSIZE - IMAGESIZE) ); setLocation(currentPos); // set the duck's initial position in the pond // this value defines how far the duck moves each update period - s random value between 5 and 10. increment = new Point(random.nextInt(5)+5, random.nextInt(5)+5 ); quacker = new WavPlayer("quack.wav"); } @Override // Override of JComponent.paintComponent method, which is called automatically // by the JFrame containing this component whenever it needs to be painted. // Here, we're repainting by drawing the JPG image we loaded earlier. public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, IMAGESIZE, IMAGESIZE, null); } /** * This method implements a duck's quacking behavior */ public void quack() { System.out.println(name + ": quack!"); quacker.play(); } /** * This method implements a duck's swimming behavior. * Every time this method is called, the duck's position on the screen moves * a little bit according to the algorithm we implement here. */ public void swim() { if( (currentPos.x > (DuckPond.PONDSIZE-image.getWidth(null))) // leaving the window's right edge || (currentPos.x < 0) ) { // ...or left edge increment.x = -increment.x; // reverse direction quack(); // quack each time we reverse direction } if( (currentPos.y > (DuckPond.PONDSIZE-image.getHeight(null))) // leaving the window's top edge || (currentPos.y < 0) ) {//... or bottom edge increment.y = -increment.y; // reverse direction } currentPos.x += increment.x; // increment the position of the duck currentPos.y += increment.y; setLocation(currentPos); // ...and update its location in the container } }