在Click base上绘制2D图形对象

时间:2014-12-30 08:14:37

标签: java netbeans graphics awt

以下是我的代码。它在执行时绘制三角形。我想对它做一点改变。 改变是

执行时会有1个三角形。但如果我在三角形1内部单击,则绘制其他三角形。否则不画画。

我尝试了g2d.draw(triangle2);

中的更改

但它有问题。它只是不显示三角形,而是将其绘制为隐藏。

public class Triangle_shape extends JFrame implements ActionListener {
    public static int x=0;
    public static  JButton btnSubmit = new JButton("Submit");
    public static  JButton change = new JButton("Change");
    public Triangle_shape(){
    }
  public static void main(String[] args) {

    TrianglePanel t= new TrianglePanel();
    ClickListener cl= new ClickListener();
    JFrame frame = new JFrame ();  
    final int FRAME_WIDTH = 500;  
    final int FRAME_HEIGHT = 500;
    btnSubmit.addActionListener(cl);
    frame.setSize (FRAME_WIDTH, FRAME_HEIGHT);           
    frame.setLayout(new BorderLayout());
    frame.add(new TrianglePanel(), BorderLayout.CENTER);
    frame.add(btnSubmit, BorderLayout.PAGE_END);
    frame.add(change, BorderLayout.LINE_END);
    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,triangle2;

        public TrianglePanel(){
            //Create triangle
            System.out.println("From Draw "+x);
            triangle = new Polygon();
            triangle.addPoint(150, 200);
            triangle.addPoint(100, 100);
            triangle.addPoint(200, 100);
            triangle2 = new Polygon();
            triangle2.addPoint(200, 300);
            triangle2.addPoint(200, 200);
            triangle2.addPoint(300, 200);
            //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;
            System.out.println("From Graphics "+x);
            g2d.draw(triangle);
            g2d.draw(triangle2);
        }

        //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. */
        public void mouseClicked(MouseEvent e) {
            x++;
            Point p = e.getPoint();
            if(triangle.contains(p)  ) 
                 System.out.println("1");
            else if  (triangle2.contains(p))
            {       System.out.println("2");
             }
            else
            {
                System.out.println("Trianglhhhhhhhhhhhhhhhhhhpoint");
            x--;}
        }          
   }
    private static class ClickListener implements ActionListener {
    private int clickCount = 0;
    @Override
    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == btnSubmit) {
        clickCount++;
        if (clickCount == 1)
          btnSubmit.setText("clicked!");
        else
        btnSubmit.setText("Inside Triangle " + x + " times!");
      } 
        else {
          //JOptionPane.showMessageDialog(MainClass.this, "You must click at least once!",
           btnSubmit.setText("Error " + clickCount + " times!");          
        }

        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试以下代码,它是根据您自己的代码创建的示例。

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.MouseEvent;
   import java.awt.event.MouseListener;
   public class MyTriangle{
   static  JFrame frame = new JFrame();
   public static void main(String[] args)
    {
    frame.setSize(1000, 1500);
    frame.setTitle("Triangle Draw");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //add panel to frame and make it visible
    Polygon triangle1 = new Polygon();
    triangle1.addPoint(100, 500); // first
    triangle1.addPoint(600, 500);//last
    triangle1.addPoint(350, 300);//middel
    addTriangle(new Triangle(triangle1));
    frame.setVisible(true);
}

public static void addTriangle(Triangle triangle1)
{
    frame.add(triangle1);
}
static class Triangle extends JPanel implements MouseListener{
    private Polygon triangle;

    public Triangle(Polygon triangle)
    {
        this.triangle = triangle;

        addMouseListener(this);
    }

    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.draw(triangle);

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Point p = e.getPoint();
        if(triangle.contains(p))
        {
            Polygon triangle2 = new Polygon();
            triangle2.addPoint(200, 300);
            triangle2.addPoint(200, 200);
            triangle2.addPoint(300, 200);
            MyTriangle.addTriangle(new Triangle(triangle2));
            Graphics2D g2d = (Graphics2D)this.getGraphics();
            g2d.draw(triangle2);
        }
    }


}

}