2014-12-28 32 views
3

我需要製作一個繪製三角形的代碼。那麼用戶將點擊三角形內部或外部。如果是的話,會顯示一個對話框「點擊三角形內部」。如何檢查鼠標點擊二維圖形java。

我有代碼繪製三角形,這裏是代碼,現在該怎麼做。我不知道。如果有人幫助,然後請。

我試過軸基,但我沒有拿出所需的結果。

public void paintComponent(Graphics g) 
     { 
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw(new Line2D.Double (150, 200, 200, 100)); 
      g2.draw(new Line2D.Double (100, 100, 150, 200));   
      g2.draw(new Line2D.Double (100, 100, 200, 100)); 
     } 

產量在這裏。 enter image description here

+0

這應有助於:http://stackoverflow.com/questions/2049582/how-to-determine-a-point-in-a-triangle – lwi

回答

1

Shape類是你想要使用的。不需要手工繪製三角形(每條線有單獨的draw語句),請創建一個代表三角形的對象Shape。一個Polygon就足以創建三角形。

而不是改變JFrame的繪畫,它是一個更好的主意,有一個自定義繪畫面板,並將其添加到框架。

public class YourFrame extends JFrame { //Replace with your class name, obviously 

    public YourFrame(){ 
     //Create and add trianglePanel 
     setLayout(new BorderLayout()); 
     add(new TrianglePanel(), BorderLayout.CENTER); 
     pack(); 
     repaint(); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    class TrianglePanel extends JPanel implements MouseListener{ 
     private Polygon triangle; 

     public TrianglePanel(){ 
      //Create triangle 
      triangle = new Polygon(); 
      triangle.addPoint(150, 200); 
      triangle.addPoint(100, 100); 
      triangle.addPoint(200, 100); 

      //Add mouse Listener 
      addMouseListener(this); 

      //Set size to make sure that the whole triangle is shown 
      setPreferredSize(new Dimension(300, 300)); 
     } 

     /** Draws the triangle as this frame's painting */ 
     public void paintComponent(Graphics g){ 
      Graphics2D g2d = (Graphics2D)g; 
      g2d.draw(triangle); 
     } 

     //Required methods for MouseListener, though the only one you care about is click 
     public void mousePressed(MouseEvent e) {} 
     public void mouseReleased(MouseEvent e) {} 
     public void mouseEntered(MouseEvent e) {} 
     public void mouseExited(MouseEvent e) {} 

     /** Called whenever the mouse clicks. 
      * Could be replaced with setting the value of a JLabel, etc. */ 
     public void mouseClicked(MouseEvent e) { 
      Point p = e.getPoint(); 
      if(triangle.contains(p)) System.out.println("Triangle contains point"); 
      else System.out.println("Triangle Doesn't contain point"); 
     } 
    } 
}   
+0

我沒有得到它,但讓我試試。那麼我會問我是否有麻煩。謝謝 –

+0

更改爲繪製三角形作爲面板。這就是我如何編寫它的原因。 – Mshnik

+0

這些行有錯誤沒有addPoint函數.... triangle.addPoint(150,200); triangle.addPoint(100,100); triangle.addPoint(200,100); –

-1

這應該做的工作。這不是最好的選擇,也不是最好的選擇,但它的工作原理。

// The main class which makes the frame and adds the necessary stuff 
class Mainclass { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setSize(400,400); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     CPanel panel = new CPanel(); 

     // I like colours 
     panel.setBackground(new Color(255,255,255,255)); 
     frame.add(panel); 

     frame.addMouseListener(new Mouse(panel)); 

     frame.setVisible(true); 
    } 
} 

// The MouseListener which checks if the mouse is clicked 
class Mouse implements MouseListener { 

    CPanel panel; 

    public Mouse(CPanel panel) { 
     this.panel=panel; 
    } 


    @Override 
    public void mouseClicked(MouseEvent e) { 
     int x=e.getX(); 
     int y=e.getY(); 

     boolean inside=false; 

     ArrayList<Integer> Ys = panel.coordinates.get(x); 
     if(Ys != null) { 
      if(panel.coordinates.get(x).contains(y)) { 
       inside=true; 
      } 
     } 

     if(inside) { 
      System.out.println("You clicked in the triangle"); 
     } else { 
      System.out.println("You clicked out of the triangle"); 
     } 

    } 

    @Override public void mousePressed (MouseEvent e) {} 
    @Override public void mouseReleased (MouseEvent e) {} 
    @Override public void mouseEntered (MouseEvent e) {} 
    @Override public void mouseExited (MouseEvent e) {} 
} 

// The panel 
class CPanel extends JPanel { 

    public int minX=100; 
    public int maxX=200; 
    public int minY=100; 
    public int maxY=200; 

    // All the pixels in the triangle 
    HashMap<Integer, ArrayList<Integer>> coordinates = new HashMap<Integer, ArrayList<Integer>>(); 


    public void paintComponent(Graphics G) { 
     super.paintComponent(G); 

     /** For that one downvoter: I made it G2D, even though it makes no difference **/ 
     Graphics2D g = (Graphics2D) G; 

     // Drawing a centered triangle 
     int xCen=(int)Math.round((minX+maxX)/2.0); 

     // I like colours 
     g.setColor(new Color(0,0,255,128)); 

     for(int y=0; y<=maxY-minY; y++) { 

      int x0=xCen-y; 
      int x1=xCen+y; 

      int y0=y+minY; 

      for(int x=x0; x<=x1; x++) { 
       // Adding all pixels in this row to 'coordinates' 
       ArrayList<Integer> Ys = coordinates.get(x); 

       if(Ys==null) { 
        coordinates.put(x, new ArrayList<Integer>()); 
        Ys = coordinates.get(x); 
       } 

       Ys.add(y0); 
       coordinates.put(x, Ys); 
      } 

      // Draw the row 
      g.drawLine(x0,y0,x1,y0); 
     } 

     // Output the coordinates for debugging purposes 
     System.out.println(coordinates); 
    } 
} 

再一次,如果你想要更好的性能,請不要使用它。

+0

三角形繪製代碼,它是如何工作的? 我需要製作大約27個三角形(謝爾平斯基三角形)。循環將不是一個很好的選擇... –

+0

@AdnanAli這就是我說過兩次,它不是最好的表現,但你知道如何做到這一點。 – Charlie

0

最終代碼@Mshink的幫助

public class Triangle_shape extends JFrame { 
     /** 
     * @param args the command line arguments 
     */ 
     public Triangle_shape(){ 
      //Create and add trianglePanel 

      // setVisible(true); 
     // setDefaultCloseOperation(EXIT_ON_CLOSE); 
     } 
     public static void main(String[] args) { 

      // TODO code application logic here 
      TrianglePanel t= new TrianglePanel(); 
     JFrame frame = new JFrame(); 
     final int FRAME_WIDTH = 500; 
     final int FRAME_HEIGHT = 500; 

     frame.setSize (FRAME_WIDTH, FRAME_HEIGHT);   

     frame.setLayout(new BorderLayout()); 
      frame.add(t); 
     // frame.add(new TrianglePanel(), BorderLayout.CENTER); 
      frame.pack(); 
      frame.repaint(); 
      frame.setTitle("A Test Frame"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

     } 
     public static class TrianglePanel extends JPanel implements MouseListener{ 
      private Polygon triangle; 

      public TrianglePanel(){ 
       //Create triangle 
       triangle = new Polygon(); 
       triangle.addPoint(150, 200); 
       triangle.addPoint(100, 100); 
       triangle.addPoint(200, 100); 

       //Add mouse Listener 
       addMouseListener(this); 

       //Set size to make sure that the whole triangle is shown 
       setPreferredSize(new Dimension(300, 300)); 
      } 

      /** Draws the triangle as this frame's painting */ 
      public void paintComponent(Graphics g){ 
       Graphics2D g2d = (Graphics2D)g; 
       g2d.draw(triangle); 
      } 

      //Required methods for MouseListener, though the only one you care about is click 
      public void mousePressed(MouseEvent e) {} 
      public void mouseReleased(MouseEvent e) {} 
      public void mouseEntered(MouseEvent e) {} 
      public void mouseExited(MouseEvent e) {} 

      /** Called whenever the mouse clicks. 
       * Could be replaced with setting the value of a JLabel, etc. */ 
      public void mouseClicked(MouseEvent e) { 
       Point p = e.getPoint(); 
       if(triangle.contains(p)) System.out.println("Triangle contains point"); 
       else System.out.println("Triangle Doesn't contain point"); 
      } 




     } 
    }