想在applet里从(0,0)到(x,y)显示一条直线,x,y分别是鼠标每一刻的位置,我画出来的是很多直线,要求是在每一刻都只能有一条直线,而且它随鼠标位置的变化而变化,伸缩自如,请高手指点一下

解决方案 »

  1.   

    /*
     * Test3.java
     *
     * Created on 2006年8月22日, 上午11:45
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    /**
     *
     * @author lbf
     */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Test3 extends JApplet{
        private int x,y;
        /** Creates a new instance of Test3 */
        public Test3() {
            this.addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent me){
                    x=me.getX();
                    y=me.getY();
                    repaint();
                }
            });
        }
        public void paint(Graphics g){
            super.paint(g);
            g.drawLine(0,0,x,y);
        }
        
    }
      

  2.   

    super.paint(g);
    这句就是擦掉以前画的线段的方法
      

  3.   

    在鼠标事件里调用repaint()方法就行了呀
      

  4.   

    这个是图形学里所谓的“橡皮筋技术”
    不断擦除就可以了
    写一个paint()方法
    然后repaint()就可以了
    我有一个swing的画图板程序
    你可以参考
      

  5.   

    public class drawIFrame extends javax.swing.JInternalFrame {
    .............................
      private void formMouseDragged(java.awt.event.MouseEvent evt) {
    .......................................
      repaint();  //重新绘制屏幕
    }
    ......................public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
            for (int i =0; i < shapeList.size(); i++) {
                s = shapeList.get(i);
                s.drawShape(g2d);
            }//画出图形链表中的每个图形
        }}就OK了~
    呵呵~