具体编程来处理位图
知识点:
实现逻辑:
- 画板上的图片 new BufferedImage(canvasWidth,canvasHeight,BufferedImage.TYPE_INT_BGR);
- 画板Canvas的paint方法用画笔Graphics g 使用方法drawImage更新图片。
- 使用图片image的getGraphics();方法得到的图片画笔进行在图片上作画。 转化为Graphics2D 画笔
- 鼠标移动的最近两点之间画直线,鼠标移动之间很短,展示效果为曲线。
图片画笔(保留图片本身已画图基础上新增作画):
- 图片颜色填充为白色 imageGraphics.fillRect(0, 0, canvasWidth, canvasHeight);
- 画笔粗细imageGraphics.setStroke(new BasicStroke(5f));
- 画笔颜色imageGraphics.setColor(fontColor);
- 画笔画直线mageGraphics.drawLine(drawX, drawY, e.getX(), e.getY());
菜单组件:
- 右键菜单 popupMenu.show(canvas,e.getX(), e.getY());
鼠标事件应用:
MouseListener鼠标事件
-
- mouseReleased 松开鼠标
- e.isPopupTrigger()右键松开
ActionListener 鼠标左键点击触发
WindowListener监听器windowClosing() 关闭窗口事件触发
demo:
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; /** * @ClassName DrawCanvasTest * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/5/8. */ public class DrawCanvasTest { /** * 窗口 */ private Frame frame = new Frame("手绘程序"); /** * 定义画板 */ private final int canvasWidth = 600; private final int canvasHeight = 400; private Canvas canvas = new DrawCanvas(); /** * 定义图片 */ private BufferedImage image = new BufferedImage(canvasWidth,canvasHeight,BufferedImage.TYPE_INT_BGR); /** * 获取图片对应的画笔,画笔的初始位置,初始颜色 */ private Graphics2D imageGraphics = (Graphics2D) image.getGraphics(); private int drawX = -1; private int drawY = -1; private Color fontColor = new Color(255); /** * 画笔右键菜单 */ PopupMenu popupMenu = new PopupMenu(); MenuItem redItem = new MenuItem("红色"); MenuItem greenItem = new MenuItem("绿色"); MenuItem blueItem = new MenuItem("蓝色"); /** * 定义画板子类,定义画图图片 */ private class DrawCanvas extends Canvas { @Override public void paint(Graphics g) { g.drawImage(image,0, 0, null); } } private void start(){ /** * 画板大小设置 */ canvas.setSize(canvasWidth, canvasHeight); frame.add(canvas); /** * 设置画笔粗细 */ imageGraphics.setStroke(new BasicStroke(5f)); /** * 画笔右键菜单组合。添加到窗口 */ popupMenu.add(redItem); popupMenu.add(greenItem); popupMenu.add(blueItem); frame.add(popupMenu); /** * 画板中图片背景画成白色 */ imageGraphics.fillRect(0, 0, canvasWidth, canvasHeight); /** * 按下鼠标拖动时画图 */ canvas.addMouseMotionListener(new MouseAdapter() { //按下鼠标拖动 @Override public void mouseDragged(MouseEvent e) { /** * 如果位置不在初始位置,开始画图 */ if (drawX != -1 && drawY != -1){ imageGraphics.setColor(fontColor); imageGraphics.drawLine(drawX, drawY, e.getX(), e.getY()); canvas.repaint(); }//移动画笔位置到当前鼠标位置 drawX = e.getX(); drawY = e.getY(); } }); /** * 松开鼠标画笔归位-1;-1; */ canvas.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { drawX = -1; drawY = -1; //如果是右键松开弹出选择颜色的弹框 if (e.isPopupTrigger()){ popupMenu.show(canvas,e.getX(), e.getY()); } } }); /** * 右键菜单绑定监听器,改变画笔颜色 */ ActionListener fontColorListener = actionEvent -> { switch (actionEvent.getActionCommand()){ case "红色": fontColor = new Color(255,0,0); break; case "绿色": fontColor = new Color(0,250,0); break; case "蓝色": fontColor = new Color(0,0,255); break; } }; redItem.addActionListener(fontColorListener); greenItem.addActionListener(fontColorListener); blueItem.addActionListener(fontColorListener); /** * 窗口关闭 */ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("窗口关闭"); System.exit(0); } }); /** * 窗口位置大小 */ frame.setLocation(400,300); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new DrawCanvasTest().start(); } }