如何用java编程实现用鼠标拖动画正方形(要有源码哟)
展开全部
基本上是这样子了,有些bug你自己改改吧
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class T0 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new T0().new MyFrame();
}
class MyFrame extends Frame {
private int x, y;
private Graphics g;
public MyFrame() {
this.setBounds(200, 200, 200, 200);
this.setVisible(true);
g = this.getGraphics();
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
});
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, MyFrame.this.getWidth(), MyFrame.this.getHeight());
g.setColor(Color.BLACK);
g.drawRect(x, y, e.getX() - x, e.getY() - y);
}
});
}
}
}
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class T0 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new T0().new MyFrame();
}
class MyFrame extends Frame {
private int x, y;
private Graphics g;
public MyFrame() {
this.setBounds(200, 200, 200, 200);
this.setVisible(true);
g = this.getGraphics();
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
});
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, MyFrame.this.getWidth(), MyFrame.this.getHeight());
g.setColor(Color.BLACK);
g.drawRect(x, y, e.getX() - x, e.getY() - y);
}
});
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |