[rcpilot-cvs] src/rcpilot/rcgs RcgsMap.java,1.9,1.10
Status: Beta
Brought to you by:
mjpawlowsky
|
From: Chris S. <cm...@us...> - 2004-06-28 04:53:29
|
Update of /cvsroot/rcpilot/src/rcpilot/rcgs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19321/rcpilot/rcgs Modified Files: RcgsMap.java Log Message: 1. Zoom/Scroll buttons implemented and working. 2. Hscale/Vscale converted to double instead of long (the precision lost in calculations was significant) 3. Renamed bot/top longitude variables to left/right. (was confusing) 4. Changed color of range rings from black to red. 5. Arrow shape removed (was just there for testing) Index: RcgsMap.java =================================================================== RCS file: /cvsroot/rcpilot/src/rcpilot/rcgs/RcgsMap.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RcgsMap.java 27 Jun 2004 20:14:05 -0000 1.9 --- RcgsMap.java 28 Jun 2004 04:53:13 -0000 1.10 *************** *** 42,50 **** import java.awt.event.*; import javax.swing.*; - //import javax.swing.plaf.basic.*; // for BasicArrowButton import java.io.*; public class RcgsMap extends JComponent ! implements ComponentListener, TelemetryDataChangedEventListener { private static final String defaultMapName = "world"; --- 42,51 ---- import java.awt.event.*; import javax.swing.*; import java.io.*; public class RcgsMap extends JComponent ! implements ComponentListener, ! TelemetryDataChangedEventListener, ! ActionListener { private static final String defaultMapName = "world"; *************** *** 70,89 **** private long mapVsecs; ! private long Hscale; ! private long Vscale; // GPS stats for image loaded from disk private long srcTopLatSecs; ! private long srcTopLonSecs; private long srcBotLatSecs; ! private long srcBotLonSecs; // GPS stats for currently displayed subimage private long displayTopLatSecs; ! private long displayTopLonSecs; private long displayBotLatSecs; ! private long displayBotLonSecs; ! private long scale; --- 71,107 ---- private long mapVsecs; ! // seconds/pixel ! private double Hscale; ! private double Vscale; // GPS stats for image loaded from disk private long srcTopLatSecs; ! private long srcLeftLonSecs; private long srcBotLatSecs; ! private long srcRightLonSecs; // GPS stats for currently displayed subimage private long displayTopLatSecs; ! private long displayLeftLonSecs; private long displayBotLatSecs; ! private long displayRightLonSecs; ! ! // Used to track currently displayed portion ! // of loaded map (needed for zoom/scrolling). ! // ! // - displayX is the x pixel coordinate (in map space) ! // of the current top left corner of the display. ! // - displayY is the y pixel coordinate (in map space) ! // of the current top left corner of the display. ! // ! ! private int displayX; ! private int displayY; ! private int displayWidth; ! private int displayHeight; ! ! private int zoomLevel; ! private long scale; *************** *** 113,122 **** setLayout(null); zoomPlusButton = new JButton(new ImageIcon(ZOOM_PLUS_IMAGE_PATH)); zoomMinusButton = new JButton(new ImageIcon(ZOOM_MINUS_IMAGE_PATH)); - - add(zoomPlusButton); - add(zoomMinusButton); - scrollUpButton = new JButton(new ImageIcon(SCROLL_UP_IMAGE_PATH)); scrollRightButton = new JButton(new ImageIcon(SCROLL_RIGHT_IMAGE_PATH)); --- 131,137 ---- setLayout(null); + // create buttons zoomPlusButton = new JButton(new ImageIcon(ZOOM_PLUS_IMAGE_PATH)); zoomMinusButton = new JButton(new ImageIcon(ZOOM_MINUS_IMAGE_PATH)); scrollUpButton = new JButton(new ImageIcon(SCROLL_UP_IMAGE_PATH)); scrollRightButton = new JButton(new ImageIcon(SCROLL_RIGHT_IMAGE_PATH)); *************** *** 124,127 **** --- 139,162 ---- scrollLeftButton = new JButton(new ImageIcon(SCROLL_LEFT_IMAGE_PATH)); + // set action commands + zoomPlusButton.setActionCommand("ZOOM_PLUS"); + zoomMinusButton.setActionCommand("ZOOM_MINUS"); + scrollUpButton.setActionCommand("SCROLL_UP"); + scrollRightButton.setActionCommand("SCROLL_RIGHT"); + scrollDownButton.setActionCommand("SCROLL_DOWN"); + scrollLeftButton.setActionCommand("SCROLL_LEFT"); + + // add action commands + + zoomPlusButton.addActionListener(this); + zoomMinusButton.addActionListener(this); + scrollUpButton.addActionListener(this); + scrollRightButton.addActionListener(this); + scrollDownButton.addActionListener(this); + scrollLeftButton.addActionListener(this); + + // add buttons + add(zoomPlusButton); + add(zoomMinusButton); add(scrollUpButton); add(scrollRightButton); *************** *** 179,183 **** // a .inf file ! mapHsecs = srcBotLonSecs - srcTopLonSecs; mapVsecs = srcTopLatSecs - srcBotLatSecs; --- 214,218 ---- // a .inf file ! mapHsecs = srcRightLonSecs - srcLeftLonSecs; mapVsecs = srcTopLatSecs - srcBotLatSecs; *************** *** 185,188 **** --- 220,230 ---- mapVpix = mapImg.getHeight(null); + // reset these whenever a new map loads + displayX = 0; + displayY = 0; + zoomLevel = 1; + + Hscale = ((double)mapHsecs / mapImg.getWidth(null) ); + Vscale = ((double)mapVsecs / mapImg.getHeight(null) ); } *************** *** 271,282 **** 6000 * Double.parseDouble(line1Tokens[1])); ! srcTopLonSecs = (int) (360000 * Double.parseDouble(line1Tokens[2]) + ! 6000 * Double.parseDouble(line1Tokens[3])); srcBotLatSecs = (int) (360000 * Double.parseDouble(line2Tokens[0]) + 6000 * Double.parseDouble(line2Tokens[1])); ! srcBotLonSecs = (int) (360000 * Double.parseDouble(line2Tokens[2]) + ! 6000 * Double.parseDouble(line2Tokens[3])); } catch(NumberFormatException nfe) { --- 313,324 ---- 6000 * Double.parseDouble(line1Tokens[1])); ! srcLeftLonSecs = (int) (360000 * Double.parseDouble(line1Tokens[2]) + ! 6000 * Double.parseDouble(line1Tokens[3])); srcBotLatSecs = (int) (360000 * Double.parseDouble(line2Tokens[0]) + 6000 * Double.parseDouble(line2Tokens[1])); ! srcRightLonSecs = (int) (360000 * Double.parseDouble(line2Tokens[2]) + ! 6000 * Double.parseDouble(line2Tokens[3])); } catch(NumberFormatException nfe) { *************** *** 294,297 **** --- 336,344 ---- } + ////////////////////////////////////////////////////////////// + // + // Called externally if user chooses a new map + // from the dropdown menu, etc. + public void changeMap(ActionEvent e) throws FileNotFoundException, IOException { *************** *** 299,311 **** // if it loads correctly, save map name to preferences ! if(loadMap(e.getActionCommand())) UserPreferences.setMap(e.getActionCommand()); ! ! if(isVisible()) { imgUpdateRequired = true; - repaint(); } } private void layoutButtons() { // fix: cleaner way to --- 346,370 ---- // if it loads correctly, save map name to preferences ! if(loadMap(e.getActionCommand())) { UserPreferences.setMap(e.getActionCommand()); ! displayWidth = getWidth()/zoomLevel; ! displayHeight = getHeight()/zoomLevel; imgUpdateRequired = true; } + + if(isVisible()) + repaint(); } + ////////////////////////////////////////////////////////////// + // + // Since we're not using a layout manager, this method + // is called to position the buttons. This is necessary + // if we want the buttons to overlay on the map. + // It must only be called *after* the component is sized/visible. + // Currently it is called from within the paintComponent() method. + // + // Don't call me from anywhere else. + private void layoutButtons() { // fix: cleaner way to *************** *** 324,328 **** Graphics2D g2 = (Graphics2D) g; ! Shape plane, home, arrow; int course = (int)Rcgs.tdata.getCourse(); --- 383,387 ---- Graphics2D g2 = (Graphics2D) g; ! Shape plane, home; int course = (int)Rcgs.tdata.getCourse(); *************** *** 330,361 **** g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - - g2.setColor(UserPreferences.getVidColor()); - g2.fillRect(0, 0, getWidth(), getHeight()); ! if(imgUpdateRequired) { ! int cropWidth = Math.min(mapImg.getWidth(null), ! getWidth()); ! int cropHeight = Math.min(mapImg.getHeight(null), ! getHeight()); ! layoutButtons(); ! // in hundredths of a second/pixel ! Hscale = mapHsecs / mapImg.getWidth(null); ! Vscale = mapVsecs / mapImg.getHeight(null); // figure out top/bot/left/right GPS coords // of cropped image. ! displayTopLatSecs = srcTopLatSecs; ! displayTopLonSecs = srcTopLonSecs; ! displayBotLatSecs = srcTopLatSecs - Vscale*cropHeight; ! displayBotLonSecs = srcTopLonSecs - Hscale*cropWidth; ! displayImg = createImage(new FilteredImageSource(mapImg.getSource(), ! new CropImageFilter(0, 0, cropWidth, cropHeight))); imgUpdateRequired = false; --- 389,422 ---- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ! g2.setColor(UserPreferences.getVidColor()); ! g2.fillRect(0,0,getWidth(),getHeight()); ! // in hundredths of a second/pixel ! double displayHscale = Hscale / zoomLevel; ! double displayVscale = Vscale / zoomLevel; ! if(imgUpdateRequired) { ! layoutButtons(); // figure out top/bot/left/right GPS coords // of cropped image. ! displayTopLatSecs = (int)(srcTopLatSecs - Vscale*displayY); ! displayLeftLonSecs = (int)(srcLeftLonSecs + Hscale*displayX); ! displayBotLatSecs = (int)(displayTopLatSecs - displayVscale*displayHeight); ! displayRightLonSecs = (int)(displayLeftLonSecs + displayHscale*displayWidth); ! ! // perform image cropping ! ! CropImageFilter cif = new CropImageFilter(displayX, displayY, ! displayWidth, displayHeight); ! ! FilteredImageSource fis = new FilteredImageSource(mapImg.getSource(), cif); ! ! displayImg = createImage(fis).getScaledInstance(getWidth(),getHeight(),Image.SCALE_FAST); imgUpdateRequired = false; *************** *** 380,388 **** // in pixels ! int homeLatPix = (int)((displayTopLatSecs-homeLatSecs)/Vscale); ! int homeLonPix = (int)((homeLonSecs-displayTopLonSecs)/Hscale); ! int planeLatPix = (int)((displayTopLatSecs-planeLatSecs)/Vscale); ! int planeLonPix = (int)((planeLonSecs-displayTopLonSecs)/Hscale); // draw plane --- 441,449 ---- // in pixels ! int homeLatPix = (int)((displayTopLatSecs-homeLatSecs)/displayVscale); ! int homeLonPix = (int)((homeLonSecs-displayLeftLonSecs)/displayHscale); ! int planeLatPix = (int)((displayTopLatSecs-planeLatSecs)/displayVscale); ! int planeLonPix = (int)((planeLonSecs-displayLeftLonSecs)/displayHscale); // draw plane *************** *** 408,413 **** // corresponding to one mile. ! double oneMileArcVRadius = asecVPerMile * (1/(double)Vscale); ! double oneMileArcHRadius = asecHPerMile * (1/(double)Hscale); home = Shapes.getShapeIcon(Shapes.HOME_SHAPE, homeLonPix, homeLatPix, 0); --- 469,474 ---- // corresponding to one mile. ! double oneMileArcVRadius = asecVPerMile * (1/(double)displayVscale); ! double oneMileArcHRadius = asecHPerMile * (1/(double)displayHscale); home = Shapes.getShapeIcon(Shapes.HOME_SHAPE, homeLonPix, homeLatPix, 0); *************** *** 423,427 **** // draw arcs around home ! g2.setColor(Color.black); for(int i = 0; i < 36; i++) { --- 484,489 ---- // draw arcs around home ! g2.setStroke(new BasicStroke(2)); ! g2.setColor(Color.red); for(int i = 0; i < 36; i++) { *************** *** 442,456 **** i*10,5,Arc2D.OPEN)); } ! // draw arrow ! arrow = Shapes.getShapeIcon(Shapes.ARROW_SHAPE, 200, 200, 90); ! g2.setColor(Color.yellow); ! g2.fill(arrow); ! g2.setColor(Color.black); ! g2.draw(arrow); } public void displayDebugInfo() { --- 504,683 ---- i*10,5,Arc2D.OPEN)); } + } ! private void showEdgeOfMapDialog() { ! JOptionPane.showMessageDialog(Rcgs.frame, "Edge of map reached."); ! } ! private void updateZoomDependantVariables() { } + private boolean increaseZoom() { + boolean zoomPerformed = false; + + if(zoomLevel<6) { + zoomLevel++; + + // perform centered zoom in + displayX += displayWidth/(zoomLevel*2); + displayY += displayHeight/(zoomLevel*2); + + displayWidth = getWidth()/zoomLevel; + displayHeight = getHeight()/zoomLevel; + + zoomPerformed = true; + } + else + JOptionPane.showMessageDialog(Rcgs.frame, "Maximum zoom level reached."); + + return zoomPerformed; + } + + private boolean decreaseZoom() { + boolean zoomPerformed = false; + + if(zoomLevel>1) { + zoomLevel--; + + // perform centered zoom out + + displayWidth = getWidth()/zoomLevel; + displayHeight = getHeight()/zoomLevel; + + displayX -= displayWidth/((zoomLevel+1)*2); + displayY -= displayHeight/((zoomLevel+1)*2); + + // bounds checking + + if(displayX < 0) displayX = 0; + if(displayY < 0) displayY = 0; + + int rightSide = displayX + displayWidth; + int bottomSide = displayY + displayHeight; + + if(rightSide > mapImg.getWidth(null)) + displayX -= rightSide - mapImg.getWidth(null); + + if(bottomSide > mapImg.getHeight(null)) + displayY -= bottomSide - mapImg.getHeight(null); + + zoomPerformed = true; + } + else + JOptionPane.showMessageDialog(Rcgs.frame, "Minimum zoom level reached."); + + return zoomPerformed; + } + + private boolean scrollUp() { + + boolean scrollPerformed = false; + + // Figure out how far we should scroll up. + // This is the minimum of 2 values: + // field1: 1/4 the current displayHeight (preferred) + // field2: As far as we can go without running out of image + + int delta = Math.min(displayHeight/4, + displayY); + + if(delta > 0) { + displayY -= delta; + scrollPerformed = true; + } + else + showEdgeOfMapDialog(); + + return scrollPerformed; + } + + private boolean scrollRight() { + + boolean scrollPerformed = false; + + // Figure out how far we should scroll to the right. + // This is the minimum of 2 values: + // field1: 1/4 the current displayWidth (preferred) + // field2: As far as we can go without running out of image + + int delta = Math.min(displayWidth/4, + mapImg.getWidth(null)-displayWidth-displayX); + + if(delta > 0) { + displayX += delta; + scrollPerformed = true; + } + else + showEdgeOfMapDialog(); + + return scrollPerformed; + } + + private boolean scrollDown() { + + boolean scrollPerformed = false; + + // Figure out how far we should scroll down. + // This is the minimum of 2 values: + // field1: 1/4 the current displayHeight (preferred) + // field2: As far as we can go without running out of image + + int delta = Math.min(displayHeight/4, + mapImg.getHeight(null)-displayHeight-displayY); + + if(delta > 0) { + displayY += delta; + scrollPerformed = true; + } + else + showEdgeOfMapDialog(); + + return scrollPerformed; + } + + private boolean scrollLeft() { + + boolean scrollPerformed = false; + + // Figure out how far we should scroll to the left. + // This is the minimum of 2 values: + // field1: 1/4 the current displayWidth (preferred) + // field2: As far as we can go without running out of image + + int delta = Math.min(displayWidth/4, + displayX); + if(delta > 0) { + displayX -= delta; + scrollPerformed = true; + } + else + showEdgeOfMapDialog(); + + return scrollPerformed; + } + + public void actionPerformed(ActionEvent e) { + boolean shouldRepaint = false; + + if("ZOOM_PLUS".equals(e.getActionCommand())) + shouldRepaint = increaseZoom(); + else if("ZOOM_MINUS".equals(e.getActionCommand())) + shouldRepaint = decreaseZoom(); + else if("SCROLL_UP".equals(e.getActionCommand())) + shouldRepaint = scrollUp(); + else if("SCROLL_RIGHT".equals(e.getActionCommand())) + shouldRepaint = scrollRight(); + else if("SCROLL_DOWN".equals(e.getActionCommand())) + shouldRepaint = scrollDown(); + else if("SCROLL_LEFT".equals(e.getActionCommand())) + shouldRepaint = scrollLeft(); + + if(shouldRepaint) { + imgUpdateRequired = true; + repaint(); + } + } + public void displayDebugInfo() { *************** *** 464,469 **** System.out.println("bopLatSecs: " + srcBotLatSecs); ! System.out.println("topLonSecs: " + srcTopLonSecs); ! System.out.println("bopLonSecs: " + srcBotLonSecs); System.out.println("mapHSecs: " + mapHsecs); --- 691,696 ---- System.out.println("bopLatSecs: " + srcBotLatSecs); ! System.out.println("topLonSecs: " + srcLeftLonSecs); ! System.out.println("bopLonSecs: " + srcRightLonSecs); System.out.println("mapHSecs: " + mapHsecs); *************** *** 491,494 **** --- 718,725 ---- public void componentResized(ComponentEvent e) { imgUpdateRequired = true; + + displayWidth = getWidth()/zoomLevel; + displayHeight = getHeight()/zoomLevel; + repaint(); } |