想实现一个能够用鼠标画出矩形的JAVA程序,生成CLASS文件,但是无法执行。麻烦各位帮忙看看!import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawTest extends JFrame
{
protected DPanel pan=new DPanel();
protected Drawing[] drawing;
protected int index=0;

public DrawTest()
{
super();
setSize(300,300);
setLayout(new GridLayout(2,1));
add(pan);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}



class DPanel extends JPanel//画板类DPanel
{
public DPanel()
{
super();
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);

for(int i=0;i<=index;i++)
{drawing[i].draw(g);}
}
}

class MouseHandler extends MouseAdapter
{

public void mousePressed(MouseEvent e)
{
drawing[index]=new Rect();
drawing[index].s.x=drawing[index].f.x=e.getX();drawing[index].s.y=drawing[index].f.y=e.getY();
}

public void mouseReleased(MouseEvent e)
{
index++;
}
}

class MouseMotionHandler extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
drawing[index].f.x=e.getX();drawing[index].f.y=e.getY();
repaint();

}

public static void main(String[] args)
{
DrawTest Frame=new DrawTest();
}
}class Drawing//Rect的基类
{
protected Point s,f;
public Drawing()
{
s=f=new Point(0,0); 
}
public void draw(Graphics g){}
}

class Rect extends Drawing//Rect类,用于绘制方框
{
public Rect()
{
super();
}
public void draw(Graphics g)
{
g.drawRect(Math.min(s.x,f.x),Math.min(s.y,f.y),Math.abs(f.x-s.x),Math.abs(f.y-s.y));
}
}

解决方案 »

  1.   

    mousePressed 方法中 出的错误
      

  2.   

    先没管拖动,只实现起止。import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class DrawTest extends JFrame {
    private static final long serialVersionUID = 1L;
    protected DPanel pan = new DPanel();
        //protected Drawing[] drawing;
        
        //protected int index=0;
        
        public static void main(String[] args){
           new DrawTest();
        } public DrawTest(){
            super();
            setSize(300,300);
            setLayout(new GridLayout(2,1));
            add(pan);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
          
        class DPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public DPanel(){
                super();
                this.addMouseListener(new MouseHandler());
                //this.addMouseMotionListener(new MouseMotionHandler());
            }
            
            public void paintComponent(Graphics g){
                super.paintComponent(g);           
                //for(int i=0;i<index;i++)
                //{drawing[i].draw(g);}
                //g.setColor(Color.BLUE);
            }
        }
        
        class MouseHandler extends MouseAdapter{
         int x,y,h,l;
         double h1,l1;
            
            public void mousePressed(MouseEvent e){
                //drawing[index] = new Rect();
                //drawing[index].s.x=drawing[index].f.x=e.getX();
                //drawing[index].s.y=drawing[index].f.y=e.getY();
            
             x = e.getX();
             y = e.getY();
            }
            
            public void mouseReleased(MouseEvent e){
                //index++;
                //Graphics g = getGraphics();
                //g.setColor(Color.red);
             h1=Math.abs(x-e.getX());
            l1=Math.abs(y-e.getY());
            h=(int)h1;
            l=(int)l1;
            Graphics g = getGraphics();
            g.setColor(Color.red);
            g.drawRect(x,y,h,l);
            }
        }
        
        /*class MouseMotionHandler extends MouseMotionAdapter{
            public void mouseDragged(MouseEvent e){
            
                drawing[index].f.x=e.getX();
                drawing[index].f.y=e.getY();
                DrawTest fr = (DrawTest)e.getSource();
                fr.repaint();
            } 
        }*/  
    } /*class Drawing {
            protected Point s,f;
            public Drawing(){
                s=new Point(0,0); 
                f=new Point(0,0);
            }
            public void draw(Graphics g){       
            }
        }
        
        class Rect extends Drawing {
            public Rect(){
                super();    
            }
            public void draw(Graphics g){
                g.drawRect(Math.min(s.x,f.x),Math.min(s.y,f.y),Math.abs(f.x-s.x),Math.abs(f.y-s.y));
            }    
        }*/
      

  3.   

    贴个不错的
     import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class DrawRectangle extends JFrame {

    private static final long serialVersionUID = 1L;
    private int startX, startY, endX, endY;
    private Graphics og;
    private Image oimg; public DrawRectangle() {
    this.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent me) {
    endX = me.getX();// 鼠标拖动时的坐标
    endY = me.getY();
    repaint();// 图形重绘
    }
    });
    this.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent me) {
    startX = me.getX();// 鼠标按下时的坐标
    startY = me.getY();
    } public void mouseReleased(MouseEvent arg0) {
    og.drawRect(startX, startY, endX - startX, endY - startY);// 加一条直线至内存Graphics
    }
    });
    this.setTitle("画矩形");
    this.setBounds(0, 0, 800, 570);// 确定窗体在屏幕上的大小
    this.setResizable(true);// 窗体大小框显示(true)
    this.setVisible(true);// 窗体显示(true)
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭窗体
    this.setLocation(0, 0);// 窗体在屏幕上的位置为(0,0)
    oimg = createImage(800, 600);
    og = oimg.getGraphics();
    } public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(oimg, 0, 0, this);// 先把之前保存在内存的Image画上
    g.drawRect(startX, startY, endX - startX, endY - startY);// 进行绘图
    } public static void main(String[] args) {
    new DrawRectangle();
    }
    }