一、实现一个简易画图工具
(一)组成部分
1.窗体(JFrame类)
2.按钮(JButton类)
3.监听器(ActionListener,MouseListener)
4.画笔(Graphics)
(二)需要导入的包
1.java.awt
2.Javax.swing
(三)要实现的功能
1.颜色选择:黑色,红色,蓝色
2.形状的选择:直线,矩形,椭圆,等腰三角形,三角形,多边形
二、代码实现
package com.mj1125;
import javax.swing.*;
import java.awt.*;
/**
* 画图工具:需要导入库中类(java.awt javax.swing)
* 所需组件:
* 窗体(Login):显示画图界面
* 按钮
* 画笔:显示自定义内容
*/
public class DrawUI {
// 显示画图界面的方法
public void showUI(){
// 创建对象
JFrame jf = new JFrame();
// 设置大小
jf.setSize(900,900);
// 设置标题
jf.setTitle("画图Tool");
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
// 设置可视化
jf.setVisible(true);
// 流式布局管理器
FlowLayout flow = new FlowLayout();
jf.setLayout(flow);
// 标签
JLabel jLabel= new JLabel("形状");
jf.add(jLabel);
// 功能模块
JButton line = new JButton("直线");
jf.add(line);
JButton rect = new JButton("矩形");
jf.add(rect);
JButton oval = new JButton("椭圆");
jf.add(oval);
JButton btn = new JButton("三角形");
jf.add(btn);
JButton etg = new JButton("等腰三角形");
jf.add(etg);
JButton plg = new JButton("多边形");
jf.add(plg);
JLabel jLabel2 = new JLabel("颜色");
jf.add(jLabel2);
// 颜色
JButton color1 = new JButton("黑色");
jf.add(color1);
JButton color2 = new JButton("红色");
jf.add(color2);
JButton color3 = new JButton("蓝色");
jf.add(color3);
// 画笔:图形显示在哪个组件上,画笔就从该组件上获取
// 注:从窗体上获取画笔对象一定要在窗体显示可见之后
Graphics g = jf.getGraphics();
/** 监听器
* 1.事件源:当前动作所发生的组件(swing)
* 2.监听器:鼠标监听器方法:addMouseListener()
* 3.绑定事件处理类
* 数据类型
* 1.基本类型(8个):整型(byte/short/int/long) 浮点型(float/double) 字符(char) 布尔型(Boolean)
* 2.引用类型/自定义类型:类(class) 接口(interface) 数组
*/
// 创建对象,放在调用方法之前
DrawMouse mouse = new DrawMouse();
// 给按钮添加动作监听器的方法
line.addActionListener(mouse);
rect.addActionListener(mouse);
oval.addActionListener(mouse);
btn.addActionListener(mouse);
etg.addActionListener(mouse);
plg.addActionListener(mouse);
color1.addActionListener(mouse);
color2.addActionListener(mouse);
color3.addActionListener(mouse);
// 给窗体添加鼠标监听器的方法
jf.addMouseListener(mouse);
// 把画笔对象传递给DrawMouse类
mouse.gr = g;
}
public static void main(String[] args) {
DrawUI ui = new DrawUI();
ui.showUI();
}
}
继承接口代码
package com.mj1125;
//继承接口:implements
//继承接口一定要重写接口中所有的抽象方法
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 DrawMouse implements MouseListener, ActionListener {
// 引用传递(初始化属性)
public Graphics gr;//保存传递过来的画笔对象
// 全局变量(属性)
public int x1,y1,x2,y2,x3,y3;
public String name;
public int flag = 1;//标记位 防止画三角形时在获取x1,y1,x2,y2后再次重复获取而略过获取x3,y3的值
// 重写抽象方法
public void actionPerformed(ActionEvent e){
// 获取按钮内容
name = e.getActionCommand();
System.out.println("name = "+name);
// 根据鼠标内容实现颜色选择
if("黑色".equals(name)){
gr.setColor(Color.BLACK);
}
if ("红色".equals(name)) {
gr.setColor(Color.RED);
}
if ("蓝色".equals(name)) {
gr.setColor(Color.BLUE);
}
}
public void mouseClicked(MouseEvent e){
System.out.println("点击");
x3 = e.getX();
y3 = e.getY();
if ("三角形".equals(name)) {
gr.drawLine(x1,y1,x3,y3);
gr.drawLine(x2,y2,x3,y3);
flag = 1;
}
if ("多边形".equals(name)) {
gr.drawLine(x2,y2,x3,y3);
x2 = x3;
y2 = y3;
}
System.out.println("x3 = "+x3+",y3 = "+y3);
if (e.getClickCount()==2) {
gr.drawLine(x1,y1,x3,y3);
flag = 1;
}
}
public void mousePressed(MouseEvent e){
System.out.println("按下");
// 获取坐标值
// 局部变量:1.在当前方法(作用域)中定义的变量 2.方法的参数
if(flag==1){
x1=e.getX();
y1=e.getY();
}
}
public void mouseReleased(MouseEvent e){
System.out.println("释放");
if(flag==1){
x2=e.getX();
y2=e.getY();
}
// 绝对值
int w = Math.abs(x2-x1);
int h = Math.abs(y2-y1);
// 最小值
int x = Math.min(x1,x2);
int y = Math.min(y1,y2);
// 绘制图形
if ("直线".equals(name)) {
gr.drawLine(x1,y1,x2,y2);
}
if ("矩形".equals(name)) {
gr.drawRect(x,y,w,h);
}
if("椭圆".equals(name)){
gr.drawOval(x,y,w,h);
}
if ("三角形".equals(name)&&flag==1) {
gr.drawLine(x1,y1,x2,y2);
flag++;
}
if("多边形".equals(name) && flag == 1){
gr.drawLine(x1,y1,x2,y2);
flag++;
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
运行效果