我正试图开一条 rec子,但我不敢肯定会怎样做,我知道这是与变幻觉(MouseEvent e)”有关的事情,但不知道如何使用。 这是我迄今为止制定的法典:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MovRect extends Applet implements MouseMotionListener, MouseListener {
Color color = Color.green;
int x=30,y=30,w=150,l=150;
String MouseMotion ="";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(color);
g.drawRect(x, y, w, l);
}
public void mouseClicked(MouseEvent e)
{
String clickDesc;
if (e.getClickCount() == 2)
clickDesc = "double";
else
clickDesc = "single";
System.out.println("Mouse was " + clickDesc + "-clicked at location (" +
e.getX() + ", " + e.getY() + ")");
int mouseX = e.getX();
int mouseY = e.getY();
if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l )
{
}
else
{
}
this.repaint();
}
public void mouseDragged(MouseEvent e)
{
System.out.println("mouse is being dragged at location (" + e.getX() + ", " + e.getY() + ")");
MouseMotion ="mouseDragged";
repaint();
}
public void mouseMoved(MouseEvent e)
{
System.out.println("mouse is being moved at location (" + e.getX() + ", " + e.getY() + ")");
MouseMotion ="mouseMoved";
repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}