最原始最简单的方法就是获得(mousedown)坐标(startx,starty),获得(mouseup)坐标(endx,endy),这就是矩形的对角坐标,然后根据公式算出其他两个点的坐标,再把他们连线画出来就行了

解决方案 »

  1.   

    你想要那种应用的,applet或是application
      

  2.   

    里面有线,谢先,圆,把圆该一下
    draw(Rectangle2D.DOUBLE);applet 你继承字japplet酒兴
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class Test extends JFrame{
      private JButton b1,b2,b3;
      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();
            }
          }
        );
        b3 = new JButton("xie");
        b3.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(b3,BorderLayout.WEST);
         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 ;
       Vector write;
       private String ss;
       protected  int x1,x2,y1,y2;
       Line cur,curo;   public Draw()
       {      lines = new Vector();
          ovals = new Vector();
          write = 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();
    //                text = 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();
                }
                else if(ss=="xie")//跟下一个程序对比这里
                {
                    x1=e.getX();
                    y1=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();
                }            else if(ss == "xie")//跟下一个程序对比这里
                {
                    Line text = new Line();
                    text.x0 = x1;
                    text.y0 = y1;
                    text.x1 = e.getX();
                    text.y1 = e.getY();
                    x1=e.getX();
                    y1=e.getY();
                    write.add(text);
                }//。。//            else if(ss=="xie")//跟下一个程序对比这里
    //            {
    //              x2=e.getX();
    //              y2=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));
         for(int i = 0;i<write.size();i++){
           Line tmp = (Line)write.elementAt(i);
            g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
         }
    //        if(x1==-1 || y1==-1)////跟下一个程序对比这里。。
    //      {
    //
    //        x1=x2;
    //        y1=y2;
    //      }
    //      g.drawLine(x1,y1,x2,y2);//跟下一个程序对比这里
    //
    //      x1=x2;
    //      y1=y2;//。
      }
    }
      

  3.   

    把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));
    改成g.drawRect(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1),
            Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));就是矩形
      

  4.   

    JavaBoyCaoJi(老曹) 
    怎么到处都有你!
    还总是UP
      

  5.   

    以前做的作业, 你参考一下http://my.nbip.net/homepage/zhouB403/MyCode/2dDraw.exe