package class2_2_PinballStrategy_start; 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 Ball 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 Ball(String name) { this.name = name; String imageFile = "3dball.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(TableTop.TABLESIZE - IMAGESIZE), random.nextInt(TableTop.TABLESIZE - IMAGESIZE) ); Ball.this.updateOnMap(); // 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"); } /** * 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 move() { // TODO: Write strategies and edit this method // to use the strategies. // In addition to the falling down strategy, // write a falling to the right strategy. // // If you have time, also write a strategy to fall toward the image center. if (isOffBottom() || isOffTop()) { increment.y = -increment.y; // bounce off bottom } if (isOffLeft() || isOffRight()) { increment.x = -increment.x; // bounce off side } currentPos.x += increment.x; // increment the position of the duck currentPos.y += increment.y; increment.y++; // Accelerate downward setLocation(currentPos); // ...and update its location in the container } private void updateOnMap() { setLocation(currentPos); // ...and update its location in the container } @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 boolean isOffTop() { return currentPos.y < 0 && increment.y<0; } public boolean isOffBottom() { return currentPos.y > (TableTop.TABLESIZE -image.getHeight(null)) && increment.y > 0; } public boolean isOffLeft() { return currentPos.x < 0; } public boolean isOffRight() { return currentPos.x > (TableTop.TABLESIZE -image.getWidth(null)); } }