import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class J_Draw extends JFrame
{
int x1=0,x2=0,y1=0,y2=0;
int width=0,height=0;
public J_Draw()
{
super("Example of mouse event handling");
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
repaint();
}
}
);
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
width=x2-x1;
height=y2-y1;
repaint();
}
});
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g)
{
g.drawLine(x1,y1,width,height);
}
public static void main(String args[])
{
JFrame app=new J_Draw();
Container cp=app.getContentPane();
app.setBackground(Color.white);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
本程序是要实现一个画矩形功能的,怎么实现不了啊