[rcpilot-cvs] src/rcpilot/rcgs RcgsMap.java,1.4,1.5 Shapes.java,1.2,1.3 Tracker.java,1.8,1.9
Status: Beta
Brought to you by:
mjpawlowsky
|
From: Michael P. <mjp...@us...> - 2004-06-25 12:29:21
|
Update of /cvsroot/rcpilot/src/rcpilot/rcgs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6074 Modified Files: RcgsMap.java Shapes.java Tracker.java Log Message: -- Map resizes correctly with changes in component/window size. This required implementing ComponentListener interface to pickup resize events. -- Map displays 'fast' image first, then switches to antialiased version of image when it's ready (on my machine, the antialiased version took up to 2-3 seconds for large window sizes). This required overriding imageUpdate(Image,int,int,int,int,int); Try resizing the map several times and you'll see the image 'pop' from aliased to antialiased. -- bug with calculation of Hscale/Vscale fixed (needs to be based on actual window size, not .gif size). -- Plane displays at correct GPS coordinates received from TelemetryData class. -- added HOME graphic (small green house with H inside it). -- HOME graphic displays at correct GPS coordinates received from UserPreferences. Try setting your home coords somewhere inside the Montreal map, then swap back and forth between Montreal/World maps. -- added ARROW graphic. (right now just displays near center of map, but can be used for various things on map). -- added displayDebugInfo() method. Shapes.java ----------- -- Rewritten to support arbitrary number of shapes with very little code for each additional shape. -- Added HOME_SHAPE and ARROW_SHAPE -- Adding a new shape is described in the source file. Tracker.java ------------ -- One line altered to reflect new method of accessing Shapes.java. Index: RcgsMap.java =================================================================== RCS file: /cvsroot/rcpilot/src/rcpilot/rcgs/RcgsMap.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RcgsMap.java 24 Jun 2004 10:29:32 -0000 1.4 --- RcgsMap.java 25 Jun 2004 12:29:12 -0000 1.5 *************** *** 40,47 **** import java.awt.event.*; import javax.swing.*; import java.io.*; public class RcgsMap extends JComponent ! implements TelemetryDataChangedEventListener { private static final String defaultMapName = "world"; --- 40,48 ---- import java.awt.event.*; import javax.swing.*; + import java.io.*; public class RcgsMap extends JComponent ! implements ComponentListener, TelemetryDataChangedEventListener { private static final String defaultMapName = "world"; *************** *** 49,53 **** private static final String defaultInfoPath = "res/maps/"+defaultMapName+".inf"; ! private Image mapImg; // Size of the map in pixels --- 50,56 ---- private static final String defaultInfoPath = "res/maps/"+defaultMapName+".inf"; ! private Image mapImg, fastDisplayImg, goodDisplayImg; ! ! private MediaTracker scaledImgTracker = null; // Size of the map in pixels *************** *** 68,72 **** private long scale; ! /* * Create the Map Panel and get the last map. --- 71,78 ---- private long scale; ! ! private boolean imgRescaleRequired = true; ! private boolean goodImgLoaded = false; ! /* * Create the Map Panel and get the last map. *************** *** 76,79 **** --- 82,88 ---- // register for telemetry events tdata.addTelemetryDataChangedListener(this); + // register for resize events + addComponentListener(this); + setName("Map"); loadMap(UserPreferences.getMap()); *************** *** 121,128 **** // perform actual loading ! rval &= loadMapImage(imagePath); ! rval &= loadMapInfo(infoPath); ! ! return rval; } --- 130,149 ---- // perform actual loading ! boolean imageSuccess = loadMapImage(imagePath); ! boolean infoSuccess = loadMapInfo(infoPath); ! ! if(imageSuccess && infoSuccess) { ! // these values shound only be calculated ! // if we managed to load an image and ! // a .inf file ! ! mapHsecs = botLonSecs - topLonSecs; ! mapVsecs = topLatSecs - botLatSecs; ! ! mapHpix = mapImg.getWidth(null); ! mapVpix = mapImg.getHeight(null); ! } ! ! return rval & imageSuccess & infoSuccess; } *************** *** 151,160 **** tracker.waitForAll(); - mapHpix = mapImg.getWidth(null); - mapVpix = mapImg.getHeight(null); - - Hscale = mapHsecs / mapHpix; - Vscale = mapVsecs / mapVpix; - if(tracker.isErrorAny()) { System.err.println("RcgsMap.java: Error code " + --- 172,175 ---- *************** *** 222,228 **** botLonSecs = (int) (360000 * Double.parseDouble(line2Tokens[2]) + 6000 * Double.parseDouble(line2Tokens[3])); - - mapHsecs = botLonSecs - topLonSecs; - mapVsecs = topLatSecs - botLatSecs; } catch(NumberFormatException nfe) { --- 237,240 ---- *************** *** 248,253 **** UserPreferences.setMap(e.getActionCommand()); ! if(isVisible()) repaint(); } --- 260,267 ---- UserPreferences.setMap(e.getActionCommand()); ! if(isVisible()) { ! imgRescaleRequired = true; repaint(); + } } *************** *** 255,259 **** Graphics2D g2 = (Graphics2D) g; ! Shape plane; int course = (int)Rcgs.tdata.getCourse(); --- 269,273 ---- Graphics2D g2 = (Graphics2D) g; ! Shape plane, home, arrow; int course = (int)Rcgs.tdata.getCourse(); *************** *** 265,277 **** g2.fillRect(0, 0, getWidth(), getHeight()); - int mch = mapImg.getWidth(null) / 2; - int mcv = mapImg.getHeight(null) / 2; - int ch = getParent().getWidth() / 2; int cv = getParent().getHeight() / 2; ! ! g2.drawImage(mapImg, ch - mch, cv - mcv, null); ! ! plane = Shapes.planePath(ch, cv, course); g2.setColor(Color.red); --- 279,339 ---- g2.fillRect(0, 0, getWidth(), getHeight()); int ch = getParent().getWidth() / 2; int cv = getParent().getHeight() / 2; ! ! if(imgRescaleRequired) { ! if(scaledImgTracker==null) ! scaledImgTracker = new MediaTracker(this); ! ! fastDisplayImg = mapImg.getScaledInstance(getWidth(), ! getHeight(), ! Image.SCALE_FAST); ! ! goodDisplayImg = mapImg.getScaledInstance(getWidth(), ! getHeight(), ! Image.SCALE_SMOOTH); ! ! try { ! // wait only for fast version of image to load ! // so we don't hang the app. ! // good version will load/display automatically ! // through imageUpdate() method. ! ! scaledImgTracker.addImage(fastDisplayImg, 0); ! scaledImgTracker.waitForID(0); ! ! goodImgLoaded = false; ! imgRescaleRequired = false; ! } ! catch(Exception e) { ! System.err.println("RcgsMap.java: Error loading fast image!"); ! } ! } ! ! if(!goodImgLoaded) ! g2.drawImage(fastDisplayImg, 0,0,null); ! ! g2.drawImage(goodDisplayImg, 0,0,this); ! ! // in seconds/pixel ! Hscale = mapHsecs / getWidth(); ! Vscale = mapVsecs / getHeight(); ! ! // in seconds ! int homeLatSecs = UserPreferences.getHomeLatitude(); ! int homeLonSecs = UserPreferences.getHomeLongitude(); ! ! int planeLatSecs = Rcgs.tdata.getGPSLatitude(); ! int planeLonSecs = Rcgs.tdata.getGPSLongitude(); ! ! // in pixels ! int homeLatPix = (int)((topLatSecs-homeLatSecs)/Vscale); ! int homeLonPix = (int)((homeLonSecs-topLonSecs)/Hscale); ! ! int planeLatPix = (int)((topLatSecs-planeLatSecs)/Vscale); ! int planeLonPix = (int)((planeLonSecs-topLonSecs)/Hscale); ! ! // draw plane ! plane = Shapes.getShapeIcon(Shapes.PLANE_SHAPE, planeLonPix, planeLatPix, course); g2.setColor(Color.red); *************** *** 279,288 **** g2.setColor(Color.black); g2.draw(plane); ! g2.dispose(); //clean up } ! ! public void telemetryDataChanged(TelemetryDataChangedEvent ev) { ! repaint(); ! } } --- 341,420 ---- g2.setColor(Color.black); g2.draw(plane); ! ! // draw home ! home = Shapes.getShapeIcon(Shapes.HOME_SHAPE, homeLonPix, homeLatPix, 0); ! ! g2.setColor(Color.green); ! g2.fill(home); ! g2.setColor(Color.black); ! g2.draw(home); ! ! g2.setFont(new Font("Arial", Font.BOLD, 12)); ! g2.drawString("H",homeLonPix-3,homeLatPix+7); ! ! // draw arrow ! arrow = Shapes.getShapeIcon(Shapes.ARROW_SHAPE, ch-80, cv, 90); ! ! g2.setColor(Color.yellow); ! g2.fill(arrow); ! g2.setColor(Color.black); ! g2.draw(arrow); ! g2.dispose(); //clean up } ! ! public void displayDebugInfo() { ! ! System.out.println("Current img width: " + mapImg.getWidth(null)); ! System.out.println("Current img height: " + mapImg.getHeight(null)); ! ! System.out.println("Current component width: " + getWidth()); ! System.out.println("Current component height: " + getHeight()); ! ! System.out.println("topLatSecs: " + topLatSecs); ! System.out.println("bopLatSecs: " + botLatSecs); ! ! System.out.println("topLonSecs: " + topLonSecs); ! System.out.println("bopLonSecs: " + botLonSecs); ! ! System.out.println("Hscale: " + Hscale); ! System.out.println("Vscale: " + Vscale); ! } ! ! public void telemetryDataChanged(TelemetryDataChangedEvent ev) { ! repaint(); ! } ! ! ! public boolean imageUpdate(Image img, int infoflags, ! int x, int y, int w, int h) { ! if (infoflags != ALLBITS) ! return true; ! else { ! goodImgLoaded = true; ! repaint(); ! return false; ! } ! } ! ! public void componentResized(ComponentEvent e) { ! imgRescaleRequired = true; ! repaint(); ! } ! ! // dont need these 3 below, but ! // we couldn't use the adapter class b/c ! // java doesn't support multiple inheritance! ! ! public void componentHidden(ComponentEvent e) { ! ! } ! ! public void componentMoved(ComponentEvent e) { ! ! } ! ! public void componentShown(ComponentEvent e) { ! ! } } Index: Tracker.java =================================================================== RCS file: /cvsroot/rcpilot/src/rcpilot/rcgs/Tracker.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Tracker.java 5 Apr 2004 17:33:58 -0000 1.8 --- Tracker.java 25 Jun 2004 12:29:12 -0000 1.9 *************** *** 127,132 **** // Create plane shape and rotate it to course. // (course - bearing) because of previous transform) ! plane = Shapes.planePath(xc, yc-dist, (int)(course - b)); ! g2.setColor(Color.red); g2.fill(plane); --- 127,131 ---- // Create plane shape and rotate it to course. // (course - bearing) because of previous transform) ! plane = Shapes.getShapeIcon(Shapes.PLANE_SHAPE, xc, yc-dist, (int)(course - b)); g2.setColor(Color.red); g2.fill(plane); Index: Shapes.java =================================================================== RCS file: /cvsroot/rcpilot/src/rcpilot/rcgs/Shapes.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Shapes.java 3 Mar 2004 18:42:20 -0000 1.2 --- Shapes.java 25 Jun 2004 12:29:12 -0000 1.3 *************** *** 33,63 **** abstract public class Shapes { ! ! ! public static Shape planePath(int xc, int yc, int angle){ ! ! AffineTransform at = new AffineTransform(); ! Shape rplane; ! ! // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ! int txPoints[] = {xc-18, xc-3, xc-3, xc-2, xc+2, xc+3, xc+3, xc+18, xc+20, xc+20, xc+18, xc+14, xc+13, xc+3, xc+3, xc+7, xc+9, xc+7, xc+3, xc+3, xc+1, xc-1, xc-3, xc-3, xc-7, xc-9, xc-7, xc-3, xc-3, xc-13, xc-14, xc-18, xc-20, xc-20}; ! int tyPoints[] = {yc-3, yc-3, yc-10, yc-12, yc-12, yc-10, yc-3, yc-3, yc-2, yc+1, yc+2, yc+2, yc+3, yc+3, yc+15, yc+15, yc+17, yc+19, yc+19, yc+21, yc+22, yc+22, yc+21, yc+19, yc+19, yc+17, yc+15, yc+15, yc+3, yc+3, yc+2, yc+2, yc+1, yc-2 }; ! GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD, txPoints.length); ! gp.moveTo(txPoints[0], tyPoints[0]); ! ! for (int index = 1; index < txPoints.length; index++) { ! gp.lineTo(txPoints[index], tyPoints[index]); ! }; ! ! gp.closePath(); ! ! at.rotate(Math.toRadians(angle), xc, yc); ! rplane = gp.createTransformedShape(at); ! return(rplane); } - - - ! } \ No newline at end of file --- 33,108 ---- abstract public class Shapes { ! ! public static final int PLANE_SHAPE = 0; ! public static final int HOME_SHAPE = 1; ! public static final int ARROW_SHAPE = 2; ! ! static GeneralPath[] paths = null; ! ! /////////////////////////////////////////////////////////////////// ! // shape descriptions ! // ! // to add a new shape, add an entry (above) of the form: ! // public static final int NEW_SHAPE_NAME = #; ! // and describe the shape's x/y coords in the Points arrays below. ! // ! // The shape can then be retrieved using getShapeIcon(NEW_SHAPE_NAME, ...); ! // ! ! static int xPoints[][] = { ! {-18,-3,-3,-2,2,3,3,18,20,20,18,14,13,3,3,7,9,7,3,3,1,-1,-3,-3,-7,-9,-7,-3,-3,-13,-14,-18,-20,-20}, // x coords for plane ! {-11,0,11,7,7,-7,-7}, // x coords for home ! {-6,0,6,3,3,-3,-3} // x coords for arrow ! }; + static int yPoints[][] = { + { -3,-3,-10,-12,-12,-10,-3,-3,-2,1,2,2,3,3,15,15,17,19,19,21,22,22,21,19,19,17,15,15,3,3,2,2,1,-2}, // y coords for plane + {2,-8,2,2,8,8,2}, // y coords for home + {4,-10,4,4,10,10,4} // y coords for arrow + }; + + public static Shape getShapeIcon(int shapeType, int xc, int yc, int angle){ + + // if this is the first time we've ever been called, + // setup the paths array + + if(paths==null) { + paths = new GeneralPath[xPoints.length]; + for(int i = 0; i < xPoints.length; i++) + paths[i] = null; + } + + // create shape if this is the + // first time this method was invoked for + // a particular shapeType + + if(paths[shapeType]==null) { + paths[shapeType] = buildPath(xPoints[shapeType], + yPoints[shapeType]); + } + + AffineTransform at = buildTransform(xc,yc,angle); + + return(paths[shapeType].createTransformedShape(at)); } ! private static AffineTransform buildTransform(int xc, int yc, int angle) { ! AffineTransform at = new AffineTransform(); ! at.translate(xc,yc); ! at.rotate(Math.toRadians(angle)); ! return(at); ! } ! ! private static GeneralPath buildPath(int xP[], int yP[]) { ! GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, ! xP.length); ! path.moveTo(xP[0], yP[0]); ! ! for (int i = 1; i < xP.length; i++) ! path.lineTo(xP[i], yP[i]); ! ! path.closePath(); ! ! return(path); ! } ! } |