我有一个现成的,传给你好了。

解决方案 »

  1.   

    我已经给你修改好了,你自己对比一下吧。import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class Test extends JFrame{
      private JButton b1,b2;
      private   Draw dr;
      public Test ()
      {
          dr =new Draw();
         b1 = new JButton("line");
         b1.addActionListener(
          new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
              dr.setDraw(e.getActionCommand());
              dr.repaint();
            }
          }
        );
         b2 = new JButton("oval");
         b2.addActionListener(
          new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
               dr.setDraw(e.getActionCommand());
               dr.repaint();
            }
          }
        );
         Container c = getContentPane();
         c.add(b1,BorderLayout.SOUTH);
         c.add(b2,BorderLayout.NORTH);
         c.add(dr,BorderLayout.CENTER);
         validate();
         show();
         setSize(200,200);
         repaint();
      }
      public static  void main(String args[])
      {
         Test t =new Test();
         t.addWindowListener(
           new WindowAdapter(){
             public void windowClosing(WindowEvent e)
             {
               System.exit(0);
             }
           }
        );
      }
    }class Line {
       public int x0,y0,x1,y1;
    }class Draw extends JPanel{
       Vector lines;
       Vector ovals ;
       private String ss;
       Line cur,curo;   public Draw()
       {      lines = new Vector();
          ovals = new Vector();
          setPreferredSize(new Dimension(150,100));
          setBackground(Color.white);
          addMouseListener( new MouseAdapter() {
             public void mousePressed(MouseEvent e) {
                if((cur == null)||(curo==null))
                {
                       cur = new Line();
                    curo = new Line();
                }
                if(ss=="line")
                {
                cur.x0 = e.getX();
                cur.y0 = e.getY();
                }
                else if(ss=="oval")
                {
                curo.x0 = e.getX();
                curo.y0 = e.getY();
                }
             }
             public void mouseReleased(MouseEvent e) {            if(ss=="line")
                {
                  cur.x1 = e.getX();
                  cur.y1 = e.getY();
                  lines.add(cur);
                  cur =null;
                }
                else if (ss=="oval")
               {
                curo.x1 = e.getX();
                curo.y1 = e.getY();
                ovals.add(curo);
                curo = null;
               }         }
          });      addMouseMotionListener( new MouseMotionAdapter(){
             public void mouseDragged(MouseEvent e){
                if(ss =="line")
                {
                cur.x1 = e.getX();
                cur.y1 = e.getY();
                }
                else if(ss=="oval")
                {
                curo.x1 = e.getX();
                curo.y1 = e.getY();
                }
                repaint();
              }
          });
      }
      public void setDraw(String s)
      {
         ss=s;
      }
      public void paintComponent(Graphics g) {
         super.paintComponent(g);     for(int i = 0;i<lines.size();i++){
            Line tmp = (Line)lines.elementAt(i);
            g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
         }
         if(cur != null )
           g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1);
          for(int i = 0;i<ovals.size();i++){
            Line tmpo = (Line)ovals.elementAt(i);
             g.drawOval(Math.min(tmpo.x0,tmpo.x1),Math.min(tmpo.y0,tmpo.y1),
            Math.abs(tmpo.x0-tmpo.x1),Math.abs(tmpo.y0-tmpo.y1));
         }
         if(curo != null )
           g.drawOval(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1),
            Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));
      }
    }