这篇文章主要讲在界面中实现鼠标监听器,并进行画图。
1.进行界面的开发
import java.awt.*;
import javax.swing.*;
public class DrawPad {
public void showUI(){
JFrame jf = new JFrame();
jf.setTitle("画图");
jf.setSize(400,350);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setLocationRelativeTo(null);
}
public static void main(String [] args){
DrawPad drawPad = new DrawPad();
drawPad.showUI();
}
}
2.
创建一个DrawListener类并实行鼠标监听MouseListener,注意要引用鼠标监听器内部的方法
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
3.
在DrawPad类中创建鼠标监听器对象,并添加鼠标监听器,引用画图工具
DrawListener drawListener = new DrawListener();
jf.addMouseListener(drawListener);
Graphics g = jf.getGraphics();
4.在DrawListener类中创建图像对象g2,并把DrawPad类中的g传递给g2
drawListener.g2=g;
Graphics g2;
5.在鼠标按下和释放的方法中获取坐标,并把坐标赋值给外部类(这样内部类中的变量的值就可以被引用了)获取坐标的语法 int x = e.getX();
int x1, y1, x2, y2;
System.out.println("按下");
int x = e.getX();
int y = e.getY();
x1 = x;
y1 = y;
System.out.println("x1 = " + x1 + " y1 = " + y1);
System.out.println("释放");
int x = e.getX();
int y = e.getY();
x2 = x;
y2 = y;
System.out.println("x2 = " + x2 + " y2 = " + y2);
6.
在释放的方法中进行画图(直线,矩形,等腰三角形,圆形)(注意此画法是从左上角原点处开始画。
/绘制直线
//g2.drawLine(x1,y1,x2,y2);
/* 绘制矩形
1.
g2.drawLine(x1,y1,x2,y1);
g2.drawLine(x2,y1,x2,y2);
g2.drawLine(x2,y2,x1,y2);
g2.drawLine(x1,y2,x1,y1);
*/
/*
int X=Math.min(x1,x2);
int Y=Math.min(y1,y2);
int W=Math.abs(x1-x2);
int H=Math.abs(y1-y2);
g2.drawRect(X,Y,W,H);
*/
//绘制等腰三角形
g2.drawLine((x1+x2)/2,y1,x2,y2);
g2.drawLine(x2,y2,x1,y2);
g2.drawLine(x1,y2,(x1+x2)/2,y1);
7.
以下是全部代码
import java.awt.*;
import javax.swing.*;
public class DrawPad {
public void showUI(){
JFrame jf = new JFrame();
jf.setTitle("画图");
jf.setSize(400,350);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setLocationRelativeTo(null);//窗体基本组件完成后直接加入可视化,如果不加会出现错误;
//以下代码必须出现在可视化后面;
DrawListener drawListener = new DrawListener();
jf.addMouseListener(drawListener);
Graphics g = jf.getGraphics();
drawListener.g2=g;
}
public static void main(String [] args){
DrawPad drawPad = new DrawPad();
drawPad.showUI();
}
}
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawListener implements MouseListener {
// 注意不同内部类的变量是独立的,只属于所在类中,不能被其他内部类使用。而外部类的变量可以在内部类被使用(非常重要)
Graphics g2;
int x1, y1, x2, y2;
public void mouseClicked(MouseEvent e) {
System.out.println("点击");
}
public void mousePressed(MouseEvent e) {
System.out.println("按下");
int x = e.getX();
int y = e.getY();
x1 = x;
y1 = y;
System.out.println("x1 = " + x1 + " y1 = " + y1);
}
public void mouseReleased(MouseEvent e) {
System.out.println("释放");
int x = e.getX();
int y = e.getY();
x2 = x;
y2 = y;
System.out.println("x2 = " + x2 + " y2 = " + y2);
//绘制直线
//g2.drawLine(x1,y1,x2,y2);
/* 绘制矩形
1.
g2.drawLine(x1,y1,x2,y1);
g2.drawLine(x2,y1,x2,y2);
g2.drawLine(x2,y2,x1,y2);
g2.drawLine(x1,y2,x1,y1);
*/
/*
int X=Math.min(x1,x2);
int Y=Math.min(y1,y2);
int W=Math.abs(x1-x2);
int H=Math.abs(y1-y2);
g2.drawRect(X,Y,W,H);
*/
//绘制等腰三角形
g2.drawLine((x1+x2)/2,y1,x2,y2);
g2.drawLine(x2,y2,x1,y2);
g2.drawLine(x1,y2,(x1+x2)/2,y1);
}
public void mouseEntered(MouseEvent e) {
System.out.println("进入");
}
public void mouseExited(MouseEvent e) {
System.out.println("退出");
}
}
这是圆形的画法
g2.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-
y1))