用elipse/netbeans/jbuilder编写一个GUI绘图器,用来绘制矩形,三角形,圆形。椭圆形等基本图形
代码怎么写

解决方案 »

  1.   

    写了个简单的例子,供参考import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.Graphics2D;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;public class DrawTest extends JFrame {
    private JPanel drawPanel;
    private JButton drawButton;
    private Graphics2D g2d;

    public DrawTest() {
    this.setSize(400, 300);
    this.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    drawPanel = new JPanel();
    drawButton = new JButton("draw");
    drawButton.addActionListener(actionListener);
    this.getContentPane().add(drawPanel, BorderLayout.CENTER);
    this.getContentPane().add(drawButton, BorderLayout.SOUTH);
    this.setVisible(true);
    }

    public static void main(String[] args) {
    DrawTest dt = new DrawTest();
    }

    ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("draw")) {
    g2d = (Graphics2D)drawPanel.getGraphics();
    g2d.setPaint(Color.RED);
    g2d.drawRect(5, 5, 100, 50);
    g2d.dispose();
    }
    }
    };
    }
      

  2.   

    监听鼠标事件
    重载paintComponent方法即可
      

  3.   

    最小化一下 应该就看出来了
    因为没有重写paint
    所以图形会消失
      

  4.   

    如果你不喜欢Java中提供的直接画图形的方法来画,那你也可以用多个线段来逼近圆,椭圆,曲线等图形,这样你会学到更多的东西,而不只是调用现成的函数等来实现.