CSE 131 (Fall 2008)
Display

  1. Based on your observation of the game, what are the graphics objects that need to be deployed to implement the game?
  2. In terms of objects we have developed during the semester thus far, which of those objects could be recruited to implement the graphics objects?
  3. As a group, consider the design of an Arrrow object:
    1. How would you represent the arrow in terms of simple YOPS gestures? What are its "has-a" properties?
    2. The arrow in flight is described by a parabola (another group is working on the physics supporting the parabola). Discuss how to represent the parabola's equation.
    3. Given a location of the arrow's center, how do you compute the equation that describes the line of the arrow?
  4. Consider modifying the Eyeball of Lab 5 to be the shooter for the game. Below is is an implementation of Eyeball, with mouse activity removed. Modify it as a group to become a MyopicEyeball so that it loses its focus if asked to look at a point too far away. Run the code as a Java Application, and add code to the main method to make various Eyeballs try to look at various points. You will need an implementation of lab4 to make this work.
    package lab5;
    /**
     * Name:
     * Lab Section: 
     * Date:
     * Eyeball.java
     * CSE 131 Lab 5
     */
    
    import java.awt.Color;
    import yops.*;
    import lab4.*;
    
    public class Eyeball {
    
    	private static int EyeSize = 40;
    	private static int PupilSize = EyeSize*1/4;
    	private Ellipse eye, pupil;
    	protected Point location;
    	
    	public Eyeball(GraphicsPanel p, Point location) {
    		int x = (int)location.getX();
    		int y = (int)location.getY();
    		eye = new Ellipse(x-EyeSize/2,y-EyeSize/2,EyeSize, EyeSize);
    		eye.setFillColor(Color.WHITE);
    		pupil = new Ellipse(x-PupilSize/2, y-PupilSize/2, PupilSize, PupilSize);
    		pupil.setFillColor(Color.BLACK);
    		p.add(pupil);
    		p.add(eye);
    		moveTo(location);
    	}
    
    	private Vector lookAtV;
    	
    	public Vector getLookAtVector() {
    		return lookAtV;
    	}
    	public void lookAt(int x, int y) {
    		
    		lookAtV = new Point(x,y).minus(location);
    		if (lookAtV.magnitude() > EyeSize/2-PupilSize/2)
    			lookAtV = lookAtV.rescale(EyeSize/2-PupilSize/2);
    		Point pupilAt=location.plus(lookAtV);
    		pupil.setCenter((int)pupilAt.getX(), (int)pupilAt.getY());
    		
    	}
    	
    	public void moveTo(Point location) {
    		this.location = location;
    		int x = (int) location.getX();
    		int y = (int) location.getY();
    		eye.setCenter(x,y);
    		pupil.setCenter(x,y);
    	}
    	
    	public static void main(String[] args) {
    		GraphicsPanel panel = new YOPS(500,500).getGraphicsPanel(0);
    		Eyeball eye = new Eyeball(panel, new Point(100, 100));
    		eye.lookAt(200, 50);
    		// put down more Eyeballs if you like.
    		// They could all look at the same point, but some
    		// will be close enough to see the point and some not.
    	}
    
    }
    

  5. The Trajectory group will need to be able to paint images on a GraphicsPanel. The class below is useful because it never tries to paint a pixel out of bounds, and it captures the color and width of the paint brush in the constructor. The method for setting a pixel returns a value that lets the caller know if the brush tried to paint a pixel out-of-bounds.

    Copy and paste the ImagePainter class below into eclipse into a yopsx package, and then read further.


    package yopsx;
    
    import yops.*;
    import java.awt.Color;
    import lab4.Point;
    import lab4.Vector;
    
    /**
     * Set up an image to be painted with a given color and brush radius.
     * The brush can be square or round, your choice.
     *
     */
    
    public class ImagePainter {
    
    	private final Image image;
    	private final Color color;
    	private final int brush;
    
    	/**
    	 * 
    	 * @param image The yops image to be painted
    	 * @param color The color to be used
    	 * @param brush The brush radius (1/2 the width of the brush box)
    	 */
    
    	public ImagePainter(Image image, Color color, int brush) {
    		this.image = image;
    		this.color = color;
    		this.brush = brush;
    	}
    
    	/**
    	 * Paint the specified pixel with brushWidth on all sides.
    	 * All pixels in bounds of the point +/- the brush width are painted
    	 * by calling setOnePixel(Point Pixel).
    	 * @param pixel Point to be painted at the center of the brush
    	 * @return true iff the brush did not wander out of bounds
    	 */
    
    	public boolean setPixel(Point pixel) {
    		boolean ans =  true;
    		for (int i=-brush; i < brush; ++i) {
    			for (int j=-brush; j < brush; ++j) {
    				ans &= setOnePixel(pixel.plus(new Vector(i,j)));
    			}
    		}
    		return ans;
    	}
    
    	/**
    	 * Request repaint of the contained image.
    	 */
    
    	public void repaint() {
    		image.repaint();
    	}
    
    	/**
    	 * Set exactly one pixel.  This could be a helper method but might be useful
    	 * on its own as well.
    	 * @param pixel Specified pixel to set
    	 * @return true iff the pixel is not out of bounds
    	 */
    
    	public boolean setOnePixel(Point pixel) {
    		int x = (int) pixel.getX();
    		int y = (int) pixel.getY();
    		if (
    				   0 <= x  && x < image.getWidth()
    				&& 0 <= y  && y < image.getHeight()
    		) {
    			image.setPixel(x, y, color);
    			return true;
    		}
    		return false;
    	}
    
    }
    

    Find somebody who implemented drawLine as Lab 2 extension, and get that person to help your group adapt drawLine from Lab 2 to use the ImagePainter instead of setting pixels directly.

    The new drawLine should have the signature

      public static boolean drawLine(Point p1, Point p2, ImagePainter painter)
    
    where Point is from Lab 4. The method should return true if and only if all points on the line between p1 and p2 are in bounds (as determined by ImagePainter's setPixel method).


Last modified 07:35:03 CST 04 November 2008 by Ron K. Cytron