我是初学者,尝试做简单的手机九宫格图案解锁,要怎么写鼠标拖动事件?谢谢高手解答
我是初学者,尝试做简单的手机九宫格图案解锁,要怎么写鼠标拖动事件?我要让鼠标拖动过程中经过我绘制的图像的范围内就记下该图像的坐标直到我松开为止我所经过的图像的所有坐标放到一个容器或者数组内,请问要如何写才能做到?我的代码:
package com.accp;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MyFrom extends JFrame {
//Point pressed = new Point(), lastTranslate = new Point();
public MyFrom() {
super("滑动解锁");
this.setSize(300, 400);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics arg0) {
super.paint(arg0);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arg0.drawOval(j * 100 + 10, i * 100 + 50, 70, 70);
}
}
}
/*public void mousePressed(MouseEvent e) {
// 鼠标的坐标
Point loc = e.getPoint();
//鼠标的坐标-图像的开始位置的坐标=鼠标的相对坐标
pressed.x = loc.x - lastTranslate.x;
pressed.y = loc.y - lastTranslate.y;
}*/
public void mouseDragged(MouseEvent e){
//Point loc = e.getPoint();
}
/*
public void mouseDragged(MouseEvent e) {
Point loc = e.getPoint();
//拖动后的位置坐标-鼠标的相对坐标,生成相对坐标系
Point translate = new Point(loc.x - pressed.x,
loc.y - pressed.y);
Graphics g = getGraphics();
try {
g.clearRect(0,0,
getSize().width,getSize().height);
//转化相对坐标系
g.translate(translate.x, translate.y);
showStatus("Translating Graphics: " +
translate);
paint(g);
//g.drawOval(arg0.drawOval(j * 100 + 10, i * 100 + 50, 70, 70));
}
finally {
g.dispose();
}
lastTranslate = translate;
}
private void showStatus(String string) {
// TODO Auto-generated method stub
}**/
public static void main(String[] args) {
new MyFrom().setVisible(true);
}
}
我注视了好多,就是不知道实现没有,思路不清晰,谢谢各位大神帮帮忙小弟在这里先谢谢了。 java,鼠标拖动事件,手机九宫格锁屏 鼠标 String 类
补充:Java , Java相关