2012-11-02 17 views
-2

我有一個功能程序,收集每個鼠標點擊座標,然後使用這些座標繪製一個多邊形。我添加了一個功能,以便在完成繪製多邊形並填充之後,可以擦除屏幕並以新形狀重新開始。我遇到的麻煩是找出重置座標值的方法。Java小程序多邊形陣列

我現在擁有的是在我的action中執行的方法I將我的兩個數組(XCoordinates和YCoordinates)歸零。現在用戶可以用新座標重新開始,但現在第一個座標是(0,0)。每次我畫一個形狀時,它都會從左上角開始哈哈。

我想將數組的值設置爲當我初始化兩個數組時所具有的值。我試圖用actionPerformed重新初始化數組,但它不起作用,我相信這是非常糟糕的編程習慣。

任何想法?

+0

您可能希望顯示一些代碼以符合您的描述。否則,我們怎麼知道你可能做錯了什麼? –

+2

一個「Point」類型的數組,更多的是面向對象的,然後是平行的x-y數組。 – Mordechai

+0

我自己我會使用'ArrayList ',然後調用'clear()'在需要時重置它。 –

回答

2

它很容易直接操作Polygon座標數組字段,但只能通過公共API來實現。特別是,看看這些方法:

  • invalidate(),其中「應座標的任何直接操作之後被調用。」

  • reset(),使多邊形變空。

  • addPoint(),它保持內部一致的狀態。

有一個相關的例子here

2

你則很難通過不提供任何代碼,但這裏的二取的想法...

使用多邊形

這基本上採用的是Polygon和不斷加分,直到按Enter鍵...

public class PolyPainter { 

    public static void main(String[] args) { 
     new PolyPainter(); 
    } 

    public PolyPainter() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new PolyPane()); 
       frame.setSize(400, 400); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    protected class PolyPane extends JPanel { 

     private Polygon poly; 
     private Point lastPoint; 

     public PolyPane() { 

      poly = new Polygon(); 

      InputMap im = getInputMap(); 
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "clear"); 
      ActionMap am = getActionMap(); 
      am.put("clear", new AbstractAction() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        poly = new Polygon(); 
        repaint(); 
       } 
      }); 

      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mousePressed(MouseEvent e) { 
        lastPoint = e.getPoint(); 
        poly.addPoint(e.getX(), e.getY()); 
        repaint(); 
       } 
      }); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.draw(poly); 
      if (lastPoint != null) { 
       g2d.setColor(Color.RED); 
       g2d.fillOval(lastPoint.x - 5, lastPoint.y - 5, 10, 10); 
      } 
      g2d.dispose(); 
     } 
    } 
} 

使用列表中要點

這基本上使用點的列表

public class PolyPainter1 { 

    public static void main(String[] args) { 
     new PolyPainter1(); 
    } 

    public PolyPainter1() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new PolyPane()); 
       frame.setSize(400, 400); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    protected class PolyPane extends JPanel { 

     private List<Point> poly; 
     private Point lastPoint; 

     public PolyPane() { 

      poly = new ArrayList<Point>(25); 

      InputMap im = getInputMap(); 
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "clear"); 
      ActionMap am = getActionMap(); 
      am.put("clear", new AbstractAction() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        poly.clear(); 
        repaint(); 
       } 
      }); 

      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mousePressed(MouseEvent e) { 
        lastPoint = e.getPoint(); 
        poly.add(lastPoint); 
        repaint(); 
       } 
      }); 

     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      Polygon pg = new Polygon(); 
      for (Point p : poly) { 
       pg.addPoint(p.x, p.y); 
      } 
      g2d.draw(pg); 
      if (lastPoint != null) { 
       g2d.setColor(Color.RED); 
       g2d.fillOval(lastPoint.x - 5, lastPoint.y - 5, 10, 10); 
      } 
      g2d.dispose(); 
     } 
    } 
} 

個人,第一個是更有效,因爲它不需要在每次它重繪的時間來建立一個新的Polygon對象。

+0

+1用於對比。 – trashgod