public  void mouseClicked(MouseEvent e1){
                  if(! e1.getSource() instanceof Component)
                      return;
         Graphics g = ((Component)e1.getSource()).getGraphics();
......................
...............................
}

解决方案 »

  1.   

    public  void mouseClicked(MouseEvent e1){
                      
             Graphics g=((Component)e1.getSource()).getGraphics();

                  .........................
      

  2.   

    public void mousePressed(MouseEvent e){
      i=e.getX();
      j=e.getY();
    }public void mouseDragged(MouseEvent e){
      m=e.getX();
      n=e.getY();
    }
    repaint();
      

  3.   

    import java.awt.*;
    import java.awt.event.*;public class line extends Frame{ static TheCanvas cv=new TheCanvas();
    static TextField tf1=new TextField(10);
    static TextField tf2=new TextField(10);
    static TextField tf3=new TextField(10);
    static TextField tf4=new TextField(10);
    static Panel p3=new Panel(); public static void main(String args[]){

    line li=new line("canva");
    li.setcalculator();
    li.add(BorderLayout.CENTER,li.cv);
    li.go();
    }
    line(String str){
    super(str);
    }
    public void setcalculator()
    {
    Panel p1=new Panel();
    Label first=new Label("opr1");
    Label second=new Label("opr2");
    Label third=new Label("opr3");
    Label fourth=new Label("opr4");
    p1.add(first);
    tf1.setEditable(true);
    tf1.resize(tf1.preferredSize());
    p1.add(tf1);

    p1.add(second);
    tf2.setEditable(true);
    tf2.resize(tf2.preferredSize());
    p1.add(tf2);

    p1.add(third);
    tf3.setEditable(true);
    tf3.resize(tf3.preferredSize());
    p1.add(tf3);

    p1.add(fourth);
    tf4.setEditable(true);
    tf4.resize(tf4.preferredSize());
    p1.add(tf4);

    Panel p2=new Panel();
    Button drawline1=new Button("line");
    drawline1.addMouseListener(new TheCanvas());
    p2.add(drawline1);

    //Panel p3=new Panel();

    add("Center",p3);
    add("South",p2);
    add("North",p1);
    }

    public void go(){
    TheAdapterTest tat=new TheAdapterTest();
    addWindowListener(tat);
    setSize(600,400);
    show();
    }
    }
    class TheAdapterTest extends WindowAdapter{
    public void windowClosing(WindowEvent e){
    System.exit(1);
    }
    } class TheCanvas extends Canvas implements MouseListener
    {

      int m;
      int n;
      int i;
      int j;
     public  void mouseClicked(MouseEvent e1){
    String str1=line.tf1.getText();
    String str2=line.tf2.getText();
    String str3=line.tf3.getText();
    String str4=line.tf4.getText();

    i=Integer.parseInt(str1);
    j=Integer.parseInt(str2);
    m=Integer.parseInt(str3);
    n=Integer.parseInt(str4);

        Graphics g=p2.getGraphics();
       g.drawLine(i,j,m,n);
        //Graphics g=((Component)e1.getSource()).getGraphics();
        //line.cv.getGraphics()   g.drawLine(i,j,m,n);
    }

    public void mousePressed(MouseEvent e1){}
    public void mouseReleased(MouseEvent e1){}
    public void mouseEntered(MouseEvent e1){}
    public void mouseExited(MouseEvent e1){}

     //public void paint(Graphics g){
    // super.paint(g);
    // draw(g);
    // }
    // public void draw(Graphics g){
    // g.setColor(Color.pink);
    // g.drawLine(i,j,m,n);
    // }
    }
      

  4.   

    import java.awt.*;
    import java.awt.event.*;public class line extends Frame{ static TheCanvas cv;
    static TextField tf1=new TextField(10);
    static TextField tf2=new TextField(10);
    static TextField tf3=new TextField(10);
    static TextField tf4=new TextField(10);
    static Panel p3=new Panel(); public static void main(String args[]){

    line li=new line("canva");
    li.setcalculator();
    //li.add(BorderLayout.CENTER,li.cv=new TheCanvas(p3));
    li.go();
    }
    line(String str){
    super(str);
    }
    public void setcalculator()
    {
    Panel p1=new Panel();
    Label first=new Label("opr1");
    Label second=new Label("opr2");
    Label third=new Label("opr3");
    Label fourth=new Label("opr4");
    p1.add(first);
    tf1.setEditable(true);
    tf1.resize(tf1.preferredSize());
    p1.add(tf1);

    p1.add(second);
    tf2.setEditable(true);
    tf2.resize(tf2.preferredSize());
    p1.add(tf2);

    p1.add(third);
    tf3.setEditable(true);
    tf3.resize(tf3.preferredSize());
    p1.add(tf3);

    p1.add(fourth);
    tf4.setEditable(true);
    tf4.resize(tf4.preferredSize());
    p1.add(tf4);

    Panel p2=new Panel();
    Button drawline1=new Button("line");
    drawline1.addMouseListener(new TheCanvas(p3));
    p2.add(drawline1);

    //Panel p3=new Panel();
    //
    add("Center",p3);
    add("South",p2);
    add("North",p1);
    }

    public void go(){
    TheAdapterTest tat=new TheAdapterTest();
    addWindowListener(tat);
    setSize(600,400);
    show();
    }
    }
    class TheAdapterTest extends WindowAdapter{
    public void windowClosing(WindowEvent e){
    System.exit(1);
    }
    } class TheCanvas extends Canvas implements MouseListener
    {

      int m;
      int n;
      int i;
      int j;
      Panel panel;
     public TheCanvas(Panel panel) {
        this.panel = panel;
     }
     public  void mouseClicked(MouseEvent e1){
    String str1=line.tf1.getText();
    String str2=line.tf2.getText();
    String str3=line.tf3.getText();
    String str4=line.tf4.getText();

    i=Integer.parseInt(str1);
    j=Integer.parseInt(str2);
    m=Integer.parseInt(str3);
    n=Integer.parseInt(str4);

        Graphics g=panel.getGraphics();
       g.drawLine(i,j,m,n);
        //Graphics g=((Component)e1.getSource()).getGraphics();
        //line.cv.getGraphics()   g.drawLine(i,j,m,n);
    }

    public void mousePressed(MouseEvent e1){}
    public void mouseReleased(MouseEvent e1){}
    public void mouseEntered(MouseEvent e1){}
    public void mouseExited(MouseEvent e1){}

     //public void paint(Graphics g){
    // super.paint(g);
    // draw(g);
    // }
    // public void draw(Graphics g){
    // g.setColor(Color.pink);
    // g.drawLine(i,j,m,n);
    // }
    }