import com.mr.util.DrawImageUtil;
import com.mr.util.FrameGetShape;
import com.mr.util.ShapeWindow;
import com.mr.util.Shapes;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
/**
* @author WayLon
* @create 2021-07-28 14:18
* 备注:画图主窗体
*/
public class DrawPictureFrame extends JFrame implements FrameGetShape {
BufferedImage image = new BufferedImage(570, 390, BufferedImage.TYPE_INT_BGR);// 创建一个8位BGR颜色分量的图像
Graphics gs = image.getGraphics();// 获得图像的绘图对象
Graphics2D g = (Graphics2D) gs;// 将绘图图像转换为Graphics2D类型
DrawPictureCanvas canvas = new DrawPictureCanvas();//创建画布对象
Color forecolor = Color.BLACK;// 定义前景色,在这里可以把前景色理解为画笔颜色
Color bacgroundColor = Color.WHITE;// 定义背景色
int x = -1, y = -1;// 上一次鼠标绘制点的横、纵坐标
boolean rubber = false;// 橡皮标识变量
boolean drawShape = false;// 画图形标识变量
Shapes shapes;// 绘画的图形
private JToolBar toolBar;// 工具栏
private JButton eraserButton;// 橡皮按钮
private JToggleButton strokeButton1;// 细线按钮
private JToggleButton strokeButton2;// 粗线按钮
private JToggleButton strokeButton3;// 较粗按钮
private JButton backgroundButton;// 背景色按钮
private JButton foregroundButton;// 前景色按钮
private JButton clearButton;// 清除按钮
private JButton saveButton;// 保存按钮
private JButton shapeButton;// 图形按钮
private JMenuItem strokeMenuItem1;// 细线菜单
private JMenuItem strokeMenuItem2;// 粗线菜单
private JMenuItem strokeMenuItem3;// 较粗菜单
private JMenuItem clearMenuItem;// 清除菜单
private JMenuItem helpMenuItem;// 帮助菜单
private JMenuItem foregroundMenuItem;// 前景色菜单
private JMenuItem backgroundMenuItem;// 背景色菜单
private JMenuItem eraserMenuItem;// 橡皮擦菜单
private JMenuItem exitMenuItem;// 退出菜单
private JMenuItem saveMenuItem;// 保存菜单
private JMenuItem shuiyinMenuItem;// 水印菜单
private String shuiyin = "";// 水印字符内容
private PictureWindow pictureWindow;// 简笔画展示窗体
private JButton showPicButton;// 展开简笔画的按钮
/**
* 构造方法
*/
public DrawPictureFrame() {
ImageIcon imageIcon = new ImageIcon("src/img/themeicon.png");
Image image = imageIcon.getImage();
setIconImage(image);// 设置窗体图标
setResizable(false);// 窗体不能改变大小
setTitle("画图(水印内容:[" + shuiyin + "])");// 设置标题,添加水印内容提示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(700, 300, 574, 460);
init();// 组件初始化
addListener();// 添加组件监听
}
/**
* 启动程序
*/
public static void main(String[] args) {
DrawPictureFrame frame = new DrawPictureFrame();
frame.setVisible(true);
}
/**
* 为组件添加动作监听
*/
private void addListener() {
// 画板添加鼠标移动事件监听
canvas.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(final MouseEvent e) {// 当鼠标拖拽时
if (x > 0 && y > 0) {// 如果X和Y存在·鼠标记录
if (rubber) {// 如果橡皮标识为true,表示使用橡皮
g.setColor(bacgroundColor);
g.fillRect(x, y, 10, 10);// 在鼠标滑过的位置画填充的正方形
} else {// 如果橡皮标识为false,表示用画笔画图
g.drawLine(x, y, e.getX(), e.getY());// 在鼠标滑过的位置画直线
}
}
x = e.getX();
y = e.getY();
canvas.repaint();// 更新画布
}
@Override
public void mouseMoved(MouseEvent e) {
if (rubber) {
// 设置鼠标指针的形状为图片
Toolkit kit = Toolkit.getDefaultToolkit();// 获得系统默认的组件工具包
// 利用工具包获取图片
Image img = kit.createImage("src/img/鼠标橡皮.png");
// 利用工具包创建一个自定义的光标对象
// 参数为图片、光标热点和光标描述字符串
Cursor c = kit.createCustomCursor(img, new Point(0, 0), "clear");
setCursor(c);// 使用自定义的光标
} else {
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
}
});
canvas.addMouseListener(new MouseAdapter() {
// 当按键松开时
@Override
public void mouseReleased(MouseEvent e) {
// 将记录上一次鼠标绘制点的横纵坐标恢复成-1
x = -1;
y = -1;
}
// 当按键按下时
@Override
public void mousePressed(MouseEvent e) {
if (drawShape) {// 如果此时鼠标画的是图形
switch (shapes.getType()) {// 判断图形的种类
case Shapes.YUAN:// 如果是圆形
// 计算坐标,让鼠标处于图形中的中心位置
int yuanX = e.getX() - shapes.getWidth() / 2;
int yuanY = e.getY() - shapes.getHeigth() / 2;
// 创建圆形图形并指定坐标和宽高
Ellipse2D yuan = new Ellipse2D.Double(yuanX, yuanY, shapes.getWidth(), shapes.getHeigth());
g.draw(yuan);//画图工具画此圆形
break;
case Shapes.FANG:
int fangX = e.getX() - shapes.getWidth() / 2;
int fangY = e.getY() - shapes.getHeigth() / 2;
// 创建方形图形并指定坐标和宽高
Rectangle2D fang = new Rectangle2D.Double(fangX, fangY, shapes.getWidth(), shapes.getHeigth());
g.draw(fang);// 画图工具画此方形
break;
}
canvas.repaint();
// 画图形标识变量为false,说明现在鼠标画的是图形而不是线条
drawShape = false;
}
}
});
// 工具栏添加鼠标移动监听
toolBar.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
// 设置鼠标指针的形状为默认光标
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});
// 细线按钮添加动作监听
strokeButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_stroke1_actionPerformed();
}
});
// 粗线按钮添加动作监听
strokeButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_stroke2_actionPerformed();
}
});
// 较粗线按钮添加动作监听
strokeButt
没有合适的资源?快使用搜索试试~ 我知道了~
210729仿Windows自带“画图”软件.zip
共54个文件
class:29个
png:14个
xml:5个
需积分: 50 22 下载量 10 浏览量
2021-07-29
14:19:12
上传
评论 1
收藏 604KB ZIP 举报
温馨提示
210729仿Windows自带“画图”软件.zip
资源推荐
资源详情
资源评论
收起资源包目录
210729仿Windows自带“画图”软件.zip (54个子文件)
210729仿Windows自带“画图”软件
src
DrawPictureCanvas.java 753B
DrawPictureFrame.java 25KB
PictureWindow.java 3KB
img
themeicon.png 2KB
picture
鸭子.png 68KB
公主.png 59KB
Logo.png 31KB
汽车.png 56KB
蜡笔小新.png 34KB
鼠标橡皮.png 18KB
out
production
210729仿Windows自带“画图”软件
PictureWindow$1.class 803B
DrawPictureFrame$11.class 1KB
DrawPictureFrame$6.class 724B
PictureWindow$2.class 855B
DrawPictureFrame$9.class 1KB
DrawPictureFrame$18.class 726B
PictureWindow.class 2KB
DrawPictureFrame$3.class 783B
DrawPictureFrame$1.class 2KB
DrawPictureFrame.class 10KB
DrawPictureFrame$2.class 2KB
DrawPictureFrame$15.class 1KB
DrawPictureFrame$22.class 1KB
DrawPictureFrame$14.class 1KB
DrawPictureFrame$24.class 1KB
DrawPictureFrame$21.class 723B
DrawPictureFrame$10.class 1KB
DrawPictureFrame$4.class 724B
DrawPictureFrame$5.class 724B
DrawPictureFrame$7.class 727B
DrawPictureFrame$8.class 727B
DrawPictureFrame$13.class 730B
DrawPictureCanvas.class 774B
DrawPictureFrame$23.class 2KB
DrawPictureFrame$16.class 726B
DrawPictureFrame$17.class 726B
DrawPictureFrame$20.class 729B
img
themeicon.png 2KB
picture
鸭子.png 68KB
公主.png 59KB
Logo.png 31KB
汽车.png 56KB
蜡笔小新.png 34KB
鼠标橡皮.png 18KB
DrawPictureFrame$19.class 729B
DrawPictureFrame$12.class 723B
.idea
.gitignore 188B
misc.xml 284B
libraries
DrawUtil_mr.xml 301B
workspace.xml 4KB
dataSources.local.xml 148B
modules.xml 327B
lib
DrawUtil.mr.jar 64KB
210729仿Windows自带“画图”软件.iml 503B
共 54 条
- 1
资源评论
Waylon1024
- 粉丝: 263
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功