GraphicsThe PlanHardwareCoordinate SystemsSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Built-in ShapesUser-defined ShapesShapesSprites and ShapesMaking a SpriteSlide 17Slide 18Slide 19Slide 20CompSci 45.1GraphicsCompSci 45.2The PlanHardwareCoordinate SystemBuilt-in ShapesUser-defined ShapesSprites and ShapesMaking a SpriteCompSci 45.3HardwareMonitorResolutions (640x480, 800x600, 1280x1024)Bit depth (8, 15, 16, 24, 32)Refresh rate (75-85 Hz)Video CardAssists monitorOptimizes graphicsCompSci 45.4Coordinate SystemsCartesianPolarScreen (Graphics)Java 2D (Graphics2D)CompSci 45.5Coordinate SystemsCartesianRectangularX increases to the rightY increases as you go upOrigin typically at centerReal valuedyx0CompSci 45.6Coordinate SystemsPolarr increases as distance from the origin increasestheta increases in the counterclockwise directiongrid lines make concentric circles and sectorsOrigin typically at centerr is real valuedtheta is from 0 to 2*PIrtheta0.CompSci 45.7Coordinate SystemsConversion between Cartesian and Polar[x, y] = [r*cos(theta), r*sin(theta)]r = sqrt(x*x+y*y)theta = acos(x/r) if y>0theta = - acos(x/r) if y<=0No need to memorize this, but you may see it in the codeCompSci 45.8Coordinate SystemsScreen (Graphics)RectangularX increases to the rightY increases as you go downOrigin at upper leftNon-negative integer valuedxy0CompSci 45.9Coordinate SystemsJava 2D (Graphics2D)RectangularX increases to the rightY increases as you go downOrigin at upper leftReal valued (approximated)xy0CompSci 45.10Coordinate SystemsJava2D to Screen conversionSimple – round the floating point to an integer (or just truncate)Screen to Java2D conversionNone needed because integers are approximated by realsCompSci 45.11Coordinate SystemsWhy use Java2D coordinate system?Smoother motionInteger values often need to be rounded which can lead to more calculation errorSimpler to rotate and expandCompSci 45.12Built-in ShapesIn java.awt.geom packageEllipse2D.DoubleRectangle2D.DoubleRoundRectangle2D.DoubleAll constructed with (x, y, width, height)What about circles and squares?CompSci 45.13User-defined ShapesAlso in java.awt.geomGeneralPathLinesCurvesoQuadraticoCubicCan be transformed via AffineTransformAreaConstructive Area GeometryUseful tool for finding intersectionsCompSci 45.14ShapesAll classes so far are all ShapesCan draw them using a Graphics2DCan get boundary informationCan be used to make a Sprite…CompSci 45.15Sprites and ShapesSprites haveSizeShapeOrientationLocationColorOptionally a TrackerCompSci 45.16Making a SpriteHow to make a Sprite:1. Extend Sprite2. In the constructora. Call super()b. Make any Shapec. Call setShape(yourShape)CompSci 45.17Making a Spritepackage tipgame.game.test.sprite;import java.awt.geom.*;public class SquareSprite extends Sprite{ public SquareSprite() {super();Rectangle2D.Double rectangle;rectangle=new Rectangle2D.Double(0, 0, 1, 1);setShape(rectangle); }}How to make a Sprite:1. Extend Sprite2. In the constructora. Call super()b. Make any Shapec. Call setShape(yourShape)CompSci 45.18Making a SpriteSee the video game engine web site for the source code examples that followhttp://www.cs.duke.edu/~cjj1/professional/tipgame/CompSci 45.19Making a SpriteIn the constructor of LightSprite:super();Area area=new Area();Rectangle2D.Double box=new Rectangle2D.Double(0, 0, 0.2, 0.6);area.add(new Area(box));Ellipse2D.Double circle=new Ellipse2D.Double(0.02, 0.02, 0.16, 0.16);area.subtract(new Area(circle));circle=new Ellipse2D.Double(0.02, 0.22, 0.16, 0.16);area.subtract(new Area(circle));circle=new Ellipse2D.Double(0.02, 0.42, 0.16, 0.16);area.subtract(new Area(circle));setShape(area);What does this look like?CompSci 45.20Making a SpriteIn the constructor of TriangleSprite:super();GeneralPath path=new GeneralPath();path.moveTo(0.0f, 0.0f);path.lineTo(1.0f, (float)Math.sqrt(3));path.lineTo(-1.0f, (float)Math.sqrt(3));path.closePath();setShape(path);How could you make a space ship
View Full Document