From FANG
Compiled in 0.091 seconds.
001 package examples.mapexplore; 002 003 import java.awt.Color; 004 import java.awt.geom.Point2D; 005 import java.awt.geom.Rectangle2D; 006 007 import fang.*; 008 009 010 /**Demonstrates click and drag scrolling. 011 * @author Jam Jenkins*/ 012 public class MapExplore extends GameLoop 013 { 014 /** place clicked*/ 015 private Point2D.Double clicked; 016 /** what part of the sprite is 017 * currently visible on screen*/ 018 Point2D.Double visibleLocation; 019 /** a background sprite larger 020 * than the viewport*/ 021 private ImageSprite background; 022 /**position of the sprite*/ 023 private StringSprite coordinates; 024 025 /** makes and adds the Sprites*/ 026 public void startGame() 027 { 028 makeSprites(); 029 addSprites(); 030 setHelp("resources/MapExploreHelp.txt"); 031 } 032 033 /** makes the image sprites for the background 034 * and sets a vertical scrolling tracker on them*/ 035 private void makeSprites() 036 { 037 background=new ImageSprite("resources/road.jpg", false); 038 background.setScale(3); 039 background.setLocation(0.5, 0.5); 040 Rectangle2D bounds=background.getBounds2D(); 041 visibleLocation=new Point2D.Double( 042 bounds.getWidth()/2, 043 bounds.getHeight()/2); 044 coordinates=new StringSprite(); 045 coordinates.setText("(0, 0)"); 046 updateCoordinates(); 047 coordinates.setColor(new Color(1.0f, 1.0f, 1.0f, 0.75f)); 048 coordinates.leftJustify(); 049 coordinates.topJustify(); 050 coordinates.setLocation(0, 0); 051 coordinates.setScale(0.5); 052 } 053 054 /**updates the text for 055 * the coordinates sprite*/ 056 private void updateCoordinates() 057 { 058 Point2D.Double spriteLocation= 059 background.getLocation(); 060 coordinates.setText( 061 "Sprite Location:\n"+ 062 "("+truncateDecimal(spriteLocation.x, 2)+ 063 ", "+truncateDecimal(spriteLocation.y, 2)+")\n"+ 064 "Visible Location:\n"+ 065 "("+truncateDecimal(visibleLocation.x, 2)+ 066 ", "+truncateDecimal(visibleLocation.y, 2)+")"); 067 } 068 069 private String truncateDecimal(double x, int places) 070 { 071 String toReturn=""+x; 072 return toReturn.substring(0, 073 Math.min(toReturn.length(), toReturn.indexOf('.')+places+1)); 074 } 075 076 /**adds the sprites to the canvas*/ 077 private void addSprites() 078 { 079 canvas.addSprite(background); 080 canvas.addSprite(coordinates); 081 } 082 083 /**makes the background move when the 084 * mouse is dragged*/ 085 public void advanceFrame(double timeAdvanced) 086 { 087 /**gets where the mouse is first pressed down*/ 088 if(clicked==null && getPlayer().getMouse().buttonPressed()) 089 { 090 clicked=getPlayer().getMouse().getLocation(); 091 } 092 /**clears clicked when the mouse is no longer down*/ 093 if(clicked!=null && !getPlayer().getMouse().buttonPressed()) 094 { 095 clicked=null; 096 }
097 /**mouse is being dragged*/ 098 if(clicked!=null && getPlayer().getMouse().pressed()) 100 Point2D.Double position=getPlayer().getMouse().getLocation(); 101 Point2D.Double translate=new Point2D.Double( 102 position.x-clicked.x, 103 position.y-clicked.y); 104 Rectangle2D bounds=background.getBounds2D(); 105 106 /**below if statements keep the sprite on screen*/ 107 if(bounds.getMinX()+translate.x>0) 108 translate.x=-bounds.getMinX(); 109 if(bounds.getMaxX()+translate.x<1) 110 translate.x=1-bounds.getMaxX(); 111 if(bounds.getMinY()+translate.y>0) 112 translate.y=-bounds.getMinY(); 113 if(bounds.getMaxY()+translate.y<1) 114 translate.y=1-bounds.getMaxY(); 115 background.translate( 116 translate.x, 117 translate.y); 118 119 clicked=position; 120 visibleLocation.x-=translate.x; 121 visibleLocation.y-=translate.y; 122 updateCoordinates(); 123 } 124 } 125 126 public static void main(String[] argv) 127 { 128 new MapExplore().runAsApplication(); 129 } 130 } |
Compiler Errors:
----------
1. ERROR in examples/mapexplore/MapExplore.java (at line 98)
if(clicked!=null && getPlayer().getMouse().pressed())
^^^^^^^
The method pressed() is undefined for the type Mouse
----------
1 problem (1 error)
Download/View examples/mapexplore/MapExplore.java