[rcpilot-cvs] src/rcpilot/rcgs RcgsMap.java,1.3,1.4
Status: Beta
Brought to you by:
mjpawlowsky
From: Michael P. <mjp...@us...> - 2004-06-24 10:29:42
|
Update of /cvsroot/rcpilot/src/rcpilot/rcgs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14824 Modified Files: RcgsMap.java Log Message: Changes submitted by Chris Smith 1. added loadMap + loadMapImage method to combine common functionality of constructor + changeMap. 2. as a result, rewrote constructor and changeMap. 3. made getMapInfo private. 4. renamed getMapInfo to loadMapInfo. 5. fixed bug in constructor (fix moved to loadMap). 6. removed redundant 'this.' in a few places. 7. added default map filenames to class as private static final Strings. 8. removed Color class data members (black/white) that weren't in use anywhere in the code. 9. rewrote parsing of map .inf files to clean it up a bit and add some error checking on the input. Index: RcgsMap.java =================================================================== RCS file: /cvsroot/rcpilot/src/rcpilot/rcgs/RcgsMap.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RcgsMap.java 9 Mar 2004 00:11:00 -0000 1.3 --- RcgsMap.java 24 Jun 2004 10:29:32 -0000 1.4 *************** *** 1,10 **** /* - * - * - * TODO: Combine the constructor and changeMap.... - * - * - * - * * Class to create a map for tracking the vehicle * --- 1,3 ---- *************** *** 49,127 **** import java.io.*; - public class RcgsMap extends JComponent implements TelemetryDataChangedEventListener{ - - - final Color white = Color.white; - final Color black = Color.black; - private Image mapImg; // Size of the map in pixels ! private long mapHpix; ! private long mapVpix; ! // Size of the map in 1/100th of seconds ! private long mapHsecs; ! private long mapVsecs; ! private long Hscale; ! private long Vscale; ! private long topLatSecs; ! private long topLonSecs; ! private long botLatSecs; ! private long botLonSecs; ! private long scale; ! /* * Create the Map Panel and get the last map. */ public RcgsMap(TelemetryData tdata ) throws FileNotFoundException, IOException { ! ! MediaTracker tracker; ! ! // Register for TelemetryDataChangedEvents ! TelemetryDataChangedEventListener l = this; ! tdata.addTelemetryDataChangedListener(l); ! ! this.setName("Map"); ! ! String mapname = UserPreferences.getMap(); ! ! mapImg = Toolkit.getDefaultToolkit().getImage("res/maps/"+mapname+".gif"); ! ! ! /* ! * THIS DOESN'T WORK.... WHY? ! */ ! if (mapImg.getWidth(null) == 0) { ! System.out.println (" width was " + mapImg.getWidth(null)); ! mapImg = Toolkit.getDefaultToolkit().getImage("res/maps/World.gif"); ! } ! ! // Get the info files as well ! getMapInfo(mapname); ! ! try { ! tracker = new MediaTracker(this); ! tracker.addImage(mapImg, 0); ! tracker.waitForAll(); ! } catch ( Exception e ) {} ! ! mapHpix = mapImg.getWidth(null); ! mapVpix = mapImg.getHeight(null); ! Hscale = mapHsecs / mapHpix; ! Vscale = mapVsecs / mapVpix; ! ! System.out.println ("mapHsecs: " + mapHsecs); ! System.out.println ("mapVsecs: " + mapVsecs); ! System.out.println ("mapHpix: " + mapHpix); ! System.out.println ("mapVpix: " + mapVpix); ! System.out.println ("Hscale: " + Hscale); ! System.out.println ("Vscale: " + Vscale); } /* --- 42,178 ---- import java.io.*; + public class RcgsMap extends JComponent + implements TelemetryDataChangedEventListener { + private static final String defaultMapName = "world"; + private static final String defaultImagePath = "res/maps/"+defaultMapName+".gif"; + private static final String defaultInfoPath = "res/maps/"+defaultMapName+".inf"; private Image mapImg; + // Size of the map in pixels ! private long mapHpix; ! private long mapVpix; ! ! // Size of the map in 1/100th of seconds ! private long mapHsecs; ! private long mapVsecs; ! private long Hscale; ! private long Vscale; ! private long topLatSecs; ! private long topLonSecs; ! private long botLatSecs; ! private long botLonSecs; ! private long scale; /* * Create the Map Panel and get the last map. */ + public RcgsMap(TelemetryData tdata ) throws FileNotFoundException, IOException { ! // register for telemetry events ! tdata.addTelemetryDataChangedListener(this); ! setName("Map"); ! loadMap(UserPreferences.getMap()); } + ////////////////////////////////////////////////////////////// + // + // Load the given mapName. + // If it can't be found, load defaultMapName + // + // returns -- true if it loaded mapName successfully + // returns -- false if anything goes wrong. + // + // There are multiple cases that result in false: + // 1. Found mapName but something wrong with image or info file. + // 2. Couldn't find mapName, default loaded instead. + // 3. Couldn't find mapName, default failed to load. + // + + private boolean loadMap(String mapName) throws FileNotFoundException, IOException { + + boolean rval = true; // assume load will succeed + + String imagePath = "res/maps/"+mapName+".gif"; + String infoPath = "res/maps/"+mapName+".inf"; + + boolean imageExists = (new File(imagePath)).exists(); + boolean infoExists = (new File(infoPath)).exists(); + + // if requested map image or info file doesn't exist, + // prepare to load default image/info. + + if(!imageExists || !infoExists) { + System.err.println("RcgsMap.java: One of " + imagePath + + " or " + infoPath + + " doesn't exist, using defaults."); + + mapName = defaultMapName; + imagePath = defaultImagePath; + infoPath = defaultInfoPath; + + rval = false; // couldn't find part of requested map + } + + // perform actual loading + + rval &= loadMapImage(imagePath); + rval &= loadMapInfo(infoPath); + + return rval; + } + + ////////////////////////////////////////////////////////////// + // + // Load the image portion of the map. + // + // This should only be called from private + // routine loadMap, nowhere else. + // + + private boolean loadMapImage(String imagePath) { + MediaTracker tracker; + + boolean rval = true; // assume load will succeed + boolean imageExists = (new File(imagePath)).exists(); + + if(imageExists) { + mapImg = Toolkit.getDefaultToolkit().getImage(imagePath); + + // use mediatracker to load image for us + + try { + tracker = new MediaTracker(this); + tracker.addImage(mapImg, 0); + 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 " + + tracker.statusID(0,false) + + " loading image " + imagePath); + rval = false; // load failed + } + + } catch ( Exception e ) { + System.err.println("RcgsMap.java: Exception adding mapImg to tracker."); + } + } + else { + JOptionPane.showMessageDialog(Rcgs.frame, "Image file "+imagePath+" for map does not exist."); + rval = false; // load failed + } + + return rval; + } /* *************** *** 129,230 **** * The info should be in a file called mapname.inf * with a format like: 45 45.160, 74 04.550 ! * 45 20.080, 73 14.280 ! */ ! public void getMapInfo(String mapname) throws FileNotFoundException, IOException{ ! ! boolean exists = (new File("res/maps/"+mapname+".inf")).exists(); ! if (exists) { ! BufferedReader in = new BufferedReader(new FileReader("res/maps/"+mapname+".inf")); ! String str, lat, lon; ! String[] tmp; ! int i = 1; ! while ((str = in.readLine()) != null) { ! tmp = str.split(","); ! getCoord(tmp, i); ! i++; ! } ! mapHsecs = botLonSecs - topLonSecs; ! mapVsecs = topLatSecs - botLatSecs; ! } else { ! // Modal dialog with OK button ! JOptionPane.showMessageDialog(Rcgs.frame, "Information file for map does not exists."); ! } ! } ! ! ! public void getCoord(String[] tmp, int line){ ! ! String t; ! String[] s; ! double k, l; ! int lat=0, lon=0; ! ! if (line <= 2){ ! t = tmp[0].trim(); ! s = t.split(" "); ! Debug.dump(s); ! l = Double.parseDouble(s[0]); ! l = l * 60 * 60 * 100; ! k = Double.parseDouble(s[1]); ! k = k * 60 * 100; ! lat = (int)(k + l); ! ! t = tmp[1].trim(); ! s = t.split(" "); ! Debug.dump(s); ! l = Double.parseDouble(s[0]); ! l = l * 60 * 60 * 100; ! k = Double.parseDouble(s[1]); ! k = k * 60 * 100; ! lon = (int)(k + l); ! } ! ! switch (line){ ! case 1: ! topLatSecs = lat; ! topLonSecs = lon; ! case 2: ! botLatSecs = lat; ! botLonSecs = lon; ! break; ! default: ! break; ! } } ! ! public void changeMap(ActionEvent e) throws FileNotFoundException, IOException { - try - { - this.mapImg = Toolkit.getDefaultToolkit().getImage("res/maps/" + e.getActionCommand() + ".gif"); - } catch (Exception ex) { - System.err.println("Error loading map : " + ex); - } - - // Get the info files as well - getMapInfo(e.getActionCommand()); - - try { - MediaTracker tracker = new MediaTracker(this); - tracker.addImage(this.mapImg, 0); - tracker.waitForAll(); - } catch ( Exception me ) { - - } - - // Set the last map in preferences - UserPreferences.setMap(e.getActionCommand()); - - if (this.isVisible()) - { - this.repaint(); - } - } - - protected void paintComponent(Graphics g) { --- 180,254 ---- * The info should be in a file called mapname.inf * with a format like: 45 45.160, 74 04.550 ! * 45 20.080, 73 14.280 ! */ ! private boolean loadMapInfo(String infoPath) throws FileNotFoundException, IOException{ ! boolean rval = true; // assume load will succeed ! boolean infoExists = (new File(infoPath)).exists(); ! ! if (infoExists) { ! BufferedReader in = new BufferedReader(new FileReader(infoPath)); ! String str; ! String[] line1Tokens = null, line2Tokens = null; ! ! // read 2 lines from file into tokens ! ! if((str = in.readLine()) != null) ! line1Tokens = str.trim().replaceAll(",","").split(" "); ! else ! rval = false; // load failed ! ! if(rval && ((str = in.readLine()) != null)) ! line2Tokens = str.trim().replaceAll(",","").split(" "); ! else ! rval = false; // load failed ! ! // parse tokens from each line into lat/long in seconds ! // (as long as everything has gone smoothly so far). ! ! if(rval) { ! ! try { ! topLatSecs = (int) (360000 * Double.parseDouble(line1Tokens[0]) + ! 6000 * Double.parseDouble(line1Tokens[1])); ! ! topLonSecs = (int) (360000 * Double.parseDouble(line1Tokens[2]) + ! 6000 * Double.parseDouble(line1Tokens[3])); ! ! botLatSecs = (int) (360000 * Double.parseDouble(line2Tokens[0]) + ! 6000 * Double.parseDouble(line2Tokens[1])); ! ! botLonSecs = (int) (360000 * Double.parseDouble(line2Tokens[2]) + ! 6000 * Double.parseDouble(line2Tokens[3])); ! ! mapHsecs = botLonSecs - topLonSecs; ! mapVsecs = topLatSecs - botLatSecs; ! } ! catch(NumberFormatException nfe) { ! System.err.println("RcgsMap.java: Map file " + infoPath + ! " contains non-numeric tokens!"); ! rval = false; ! } ! } ! } else { ! JOptionPane.showMessageDialog(Rcgs.frame, "Information file "+infoPath+" for map does not exist."); ! rval = false; // load failed } + + return rval; } ! public void changeMap(ActionEvent e) throws FileNotFoundException, IOException { + // load map named in the action command. + // if it loads correctly, save map name to preferences + + if(loadMap(e.getActionCommand())) + UserPreferences.setMap(e.getActionCommand()); + + if(isVisible()) + repaint(); + } protected void paintComponent(Graphics g) { *************** *** 232,247 **** Graphics2D g2 = (Graphics2D) g; Shape plane; int course = (int)Rcgs.tdata.getCourse(); ! ! ((Graphics2D)g2).setRenderingHint ! (RenderingHints.KEY_ANTIALIASING, ! RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(UserPreferences.getVidColor()); g2.fillRect(0, 0, getWidth(), getHeight()); ! int mch = mapImg.getWidth(this) / 2; ! int mcv = mapImg.getHeight(this) / 2; int ch = getParent().getWidth() / 2; int cv = getParent().getHeight() / 2; --- 256,271 ---- Graphics2D g2 = (Graphics2D) g; Shape plane; + int course = (int)Rcgs.tdata.getCourse(); ! g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, ! RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(UserPreferences.getVidColor()); 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; *************** *** 250,253 **** --- 274,278 ---- plane = Shapes.planePath(ch, cv, course); + g2.setColor(Color.red); g2.fill(plane); *************** *** 256,265 **** g2.dispose(); //clean up - } ! public void telemetryDataChanged(TelemetryDataChangedEvent ev){ ! this.repaint(); ! } ! } --- 281,288 ---- g2.dispose(); //clean up } ! public void telemetryDataChanged(TelemetryDataChangedEvent ev) { ! repaint(); ! } } |