Java Swing - 在循环绘制的组件之间绘制线

时间:2017-05-09 10:11:21

标签: java swing arraylist graphics

我正在完成任务并且我差不多完成了,然而,有一个子弹告诉我在相邻组件之间画一条线以近似它们之间的距离。我到处搜索答案,但都没有奏效。 这是代码,我删除了不重要的事情,提前谢谢!

//IMPORTS
    public class AssignLIRRBranches implements Runnable
    {
      static final String title = "LIRR Map";
      static final String RK = "Ronkonkoma";
      static final String MONTAUK = "Montauk";
      static final String PJ = "Port_Jefferson";
      static int width = 1200;
      static int height = 600;
      static int mapwidth = width-100;
      static int mapheight = height-50;
      double scalingFactorx;
      double scalingFactory;
      double scalingFactor;
      double drawingX; 
      double drawingY; 
      double drawingY2;
      MapPanel mapPanel = new MapPanel(new Font("Arial", Font.ITALIC, 12), false, Color.black, "kek", 12, 12, 12, 12);
      JFrame application;
      BoxLayout boxLayout;
      Station station;
      List<Station> stations = new ArrayList<Station>();
      String[] larray;
      JComboBox jbc;
    public void run()
    {
      /* build the GUI */
        application = new JFrame();
        jbc = new JComboBox();
        jbc.addItem(RK);
        jbc.addItem(MONTAUK);
        jbc.addItem(PJ);
        boxLayout = new BoxLayout(application.getContentPane(), BoxLayout.Y_AXIS);
        jbc.setMaximumSize(new Dimension(150, 15));
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setTitle(title);
        application.setSize(width, height);
        application.setLayout(boxLayout);
        mapPanel.setPreferredSize(new Dimension(mapwidth, mapheight));
        application.add(mapPanel);
        application.add(jbc);
        application.setMinimumSize(application.getSize());
        application.pack();
        application.setMinimumSize(null);
        application.setVisible(true);




    }
      void readStations(String path) throws FileNotFoundException, IOException {

      /* read all stations */
        try{ BufferedReader br = new BufferedReader(new FileReader(path));
         String l;
         while((l = br.readLine()) != null) {
            larray = l.split(" ");
            if(larray[2].equals(RK) || larray[2].equals(MONTAUK) || larray[2].equals(PJ)) {
            /* construct and save Station instances */            
            station = new Station(Integer.parseInt(larray[0]), larray[1], larray[2], Double.parseDouble(larray[4]), Double.parseDouble(larray[5]));
            stations.add(station);
            }


         }
        } catch(NumberFormatException exc) {
            System.out.println(exc);
       }



      }
      public static void main(String[] args) throws FileNotFoundException, IOException {
        String path = "lirr_sta_alpha.txt";
        AssignLIRRBranches ars = new AssignLIRRBranches();
        ars.readStations(path);
        SwingUtilities.invokeLater(ars);
      }
      class Station {
        int id = 0;
        String NAME = " ";
        String BRANCH = " ";
        double latitude;
        double longitude;
        Station(int id, String NAME, String BRANCH, double longitude, double latitude) {
          this.longitude = longitude;
          this.latitude = latitude;
          this.NAME = NAME;
          this.BRANCH = BRANCH;
          this.id = id;
        }
       //GETTERS AND SETTERS

      }
      class MapPanel extends JPanel implements ComponentListener {
        Font mapFont;
        boolean resized = false;
        Color mapColor = Color.black;
        String currentBranch;
        double minLong, minLat, maxLong, maxLat;
        MapPanel(Font mapFont, boolean resized, Color mapColor, String currentBranch, double minLong, double minLat, double maxLong, double maxLat) {
          /* construct the MapPanel */
          this.mapFont = mapFont;
          this.resized = resized;
          this.mapColor = mapColor;
          this.currentBranch = currentBranch;
          this.minLong = minLong;
          this.minLat = minLat;
          this.maxLong = maxLong;
          this.maxLat = maxLat;

        }
     void extents() {
     minLong = Double.MAX_VALUE;
     minLat = Double.MAX_VALUE;
     maxLong = -Double.MIN_VALUE;
     maxLat = -Double.MIN_VALUE;

    for(Station station : stations){
     /* calculate minimum and maximum longitude and latitude for staions of the current branch */
      if(station.getBranch().equals(jbc.getSelectedItem())) {
        if(station.getLatitude()>maxLat){
            maxLat = station.getLatitude();
            maxLong = station.getLongitude();
        }
        if(station.getLatitude()<minLat){
            minLat = station.getLatitude();
            minLong = station.getLongitude();
        }

    }
    }

    }
        public void paint(Graphics gr) {
          Graphics2D g2 = (Graphics2D)gr;
          if (null == mapFont) {
          }
            /* get the desired font and calulate margin values */
          else
            /* set the current font */
          if (resized) {
            /* rescale, taking into account margins */
          }

          /* set the color */
          gr.setColor(mapColor);
          /* iterate through the stations */
                for (int i = 0; i < stations.size(); i++) {
                    if(stations.get(i).getBranch().equals(jbc.getSelectedItem())) {
                        //System.out.println(jbc.getSelectedItem());
               /* calculate the corrdinates and draw the station name */

        extents();
        scalingFactorx = mapwidth/(maxLat - minLat);
        scalingFactory = mapheight/(maxLong - minLong);
        scalingFactor = Math.min(scalingFactorx, scalingFactory);

        drawingX = (stations.get(i).getLatitude() - minLat)   * scalingFactorx/1.1;
        drawingY = (stations.get(i).getLongitude() - minLong)  * scalingFactory/1.1;
        drawingY2 = mapheight - drawingY;
        gr.drawString(stations.get(i).getName(), (int)drawingX, (int)drawingY2-25);
//THIS IS WHERE I WANT TO DRAW A LINE
//gr.drawLine(I already tried using a ListIterator, it didn't work, tried getting the next element and it's values, didn't work, what can I do?);



          }
       }

       mapPanel.addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent e) {
            mapwidth = application.getWidth()-150;
            mapheight = application.getHeight()-75;
            application.repaint();
        }

       });

        jbc.addActionListener (new ActionListener () {
        public void actionPerformed(ActionEvent e) {
        application.repaint();
        }
    }); 
        }




      }
    }

0 个答案:

没有答案