鼠标监听器:MouseListenser在java.awt.event中
步骤:
1.创建一个新的类DrawListener
2.实现鼠标监听
public class DrawListener implement MouseListener{}
3.重写接口的抽象的方法:
@Override public void mouseClicked(MouseEvent e) { //为点击 注:点击后移动鼠标为按住 } @Override public void mousePressed(MouseEvent e) { //为按住 } @Override public void mouseReleased(MouseEvent e) { //为释放 } @Override public void mouseEntered(MouseEvent e) { //为进来 } @Override public void mouseExited(MouseEvent e) { //为退出 }
4.在方法中添加鼠标动作后需执行的代码
5.在需要使用鼠标监听的类中创建对象,使用addMouseListener()添加注册这个对象。
获取鼠标坐标啊:
MouseEvent e:鼠标事件
在我们使用方法会自动传入当前时间的对象 里面包含了鼠标的坐标用x,y来表示
我们需要用e.getX() 以及e.getY()来获取它。
画板为例:
在创建画板窗体中需要使用鼠标监听的接口。
在类中创建一个Graphics的对象g,可以成为画笔。并且在画板的类中获取其中的画笔,并传入DrawListener类中。
在我们画一条直线时 需要获取坐标,那么分别获取按住与释放的坐标,在释放的方法中使用g.drawLine(x1,y1,x2,y2);来画出。
练习:
package lcr0330.v2;
import java.awt.*;
import javax.swing.*;
public class DrawBoard {
public void initUI(){
JFrame jf = new JFrame();
jf.setTitle("创意画板");
jf.setSize(400,400);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
DrawListener dl = new DrawListener();
jf.addMouseListener(dl);
Graphics gp = jf.getGraphics();
dl.g = gp;
}
public static void main(String[] args) {
DrawBoard db = new DrawBoard();
db.initUI();
}
}
package lcr0330.v2;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawListener implements MouseListener {
Graphics g;
int x1,y1,x2,y2;
@Override
public void mouseClicked(MouseEvent e) {//点击
}
@Override
public void mousePressed(MouseEvent e) {//按下
int x = e.getX();
int y = e.getY();
System.out.println("x:"+x+"y:"+y);
x1 = x;
y1 = y;
}
@Override
public void mouseReleased(MouseEvent e) {//释放
int x = e.getX();
int y = e.getY();
System.out.println("x:"+x+"y:"+y);
x2 = x;
y2 = y;
//g.drawLine(x1,y1,x2,y2);
//矩形
/*g.drawLine(x1,y1,x2,y1);
g.drawLine(x2,y1,x2,y2);
g.drawLine(x2,y2,x1,y2);
g.drawLine(x1,y2,x1,y1);*/
//等腰三角形
//int x3 = (x1+x2)/2;
//int y3 = y1;
/*int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
// 计算向量
int vectorX = x2 - x1;
int vectorY = y2 - y1;
// 旋转向量
int rotatedVectorX = -vectorY;
int rotatedVectorY = vectorX;
// 计算另一个顶点坐标
int x3 = midX + rotatedVectorX;
int y3 = midY + rotatedVectorY;*/
/*g.drawLine(x1,y1,x2,y2);
g.drawLine(x1,y1,x3,y3);
g.drawLine(x3,y3,x2,y2);*/
//圆
int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
int d = (int)Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y2-y1,2));
int x3 = (x1+x2)/2 - d/2;
int y3 = (y1+y2)/2 - d/2;
int x4 = (x1+x2)/2 + d/2;
int y4 = (y1+y2)/2 + d/2;
for( int i = x3;i < x4;i++){
for (int j = y3;j < y4;j++){
Double h = Math.sqrt(Math.pow(i-midX,2)+Math.pow(j-midY,2));
if(h < d/2+2 && h > d/2-2){
g.drawLine(i,j,i,j);
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {//进入
}
@Override
public void mouseExited(MouseEvent e) {//离开
}
}
通过与上个按钮监听器实现画图的切换:
package lcr0330.v3;
import java.awt.*;
import javax.swing.*;
public class DrawBoardR {
public void drawUI(){
JFrame jf = new JFrame();
jf.setSize(400,400);
jf.setTitle("画板");
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
FlowLayout fl = new FlowLayout();
jf.setLayout(fl);
JButton jb1 = new JButton("直线");
JButton jb2 = new JButton("矩形");
JButton jb3 = new JButton("圆");
JButton jb4 = new JButton("三角形");
DrawListener dl = new DrawListener();
jb1.addActionListener(dl);
jb2.addActionListener(dl);
jb3.addActionListener(dl);
jb4.addActionListener(dl);
jf.add(jb1);
jf.add(jb2);
jf.add(jb3);
jf.add(jb4);
jf.setVisible(true);
jf.addMouseListener(dl);
Graphics g = jf.getGraphics();
dl.g = g;
}
public static void main(String[] args) {
DrawBoardR ab = new DrawBoardR();
ab.drawUI();
}
}
package lcr0330.v3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawListener implements MouseListener , ActionListener {
int i = 0;
Graphics g;
int x1,y1,x2,y2;
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
x1 = x;
y1 = y;
}
@Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
x2 = x;
y2 = y;
if(i == 1){
g.drawLine(x1,y1,x2,y2);
} else if (i == 2) {
g.drawLine(x1,y1,x2,y1);
g.drawLine(x2,y1,x2,y2);
g.drawLine(x2,y2,x1,y2);
g.drawLine(x1,y2,x1,y1);
} else if (i == 3) {
int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
// 计算向量
int vectorX = x2 - x1;
int vectorY = y2 - y1;
// 旋转向量
int rotatedVectorX = -vectorY;
int rotatedVectorY = vectorX;
// 计算另一个顶点坐标
int x3 = midX + rotatedVectorX;
int y3 = midY + rotatedVectorY;
g.drawLine(x1,y1,x2,y2);
g.drawLine(x1,y1,x3,y3);
g.drawLine(x3,y3,x2,y2);
} else if (i == 4) {
int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
int d = (int)Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y2-y1,2));
int x3 = (x1+x2)/2 - d/2;
int y3 = (y1+y2)/2 - d/2;
int x4 = (x1+x2)/2 + d/2;
int y4 = (y1+y2)/2 + d/2;
for( int i = x3;i < x4;i++){
for (int j = y3;j < y4;j++){
Double h = Math.sqrt(Math.pow(i-midX,2)+Math.pow(j-midY,2));
if(h < d/2+2 && h > d/2-2){
g.drawLine(i,j,i,j);
}
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
String ae = e.getActionCommand();
if(ae.equals("直线")){
i = 1;
} else if (ae.equals("矩形")) {
i = 2;
}else if(ae.equals("三角形")){
i = 3;
} else if (ae.equals("圆")) {
i = 4;
}else{
System.out.println("null");
}
}
}