package class2_1_DuckStrategies_v7_Factory.ducks; import class2_1_DuckStrategies_v7_Factory.*; import javax.imageio.ImageIO; import javax.swing.JComponent; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.Random; import java.util.Scanner; /** * This class represents a virtual Duck. Duck extends JComponent * so that it can paint itself in a window. * @author hornick */ public class Duck extends JComponent { protected static final int IMAGESIZE = 100; // jpg height/width in pixels protected SwimBehavior swimmingBehavior; protected QuackBehavior quackingBehavior; protected String name; // name of this Duck protected Image image; // jpg image of this duck protected Point currentPos; // the duck's current position protected Point increment; // the duck's incremental movement protected Random random; // a random number generator used for swimming protected WavPlayer quacker; // used to voice duck sounds /* package */ Duck(String name, String imageFile) { this.name = name; loadDuckImage(imageFile); swimmingBehavior = new StandardSwimming(this); quackingBehavior = new NoQuacking(); 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 quacker = new WavPlayer("quack.wav"); } public void setBehaviors( SwimBehavior sb, QuackBehavior qb) { swimmingBehavior = sb; quackingBehavior = qb; } /** * This method reads a specified file containing an image of * a duck and loads the content into an Image that can be displayed * @param imageFile the file containing the duck's image * @note if the file can't be loaded, an Image will be automatically * be generated containing an error message. */ protected void loadDuckImage(String imageFile) { 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 if( image == null ) throw new RuntimeException("image problem"); } 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. } } @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); } public void performSwimming() { swimmingBehavior.swim(); } public void performQuacking() { quackingBehavior.quack(); } public static Duck createDuck(Scanner in){ System.out.println("Please enter the id of duck you want: "); int id = in.nextInt(); System.out.println("Please enter the duck's name: "); String name = in.next(); Duck res = null; if(id == 1) { res = new Redhead(name); } else { System.out.println("Warning: No duck created!"); } return res; } }