Java开发 一起来画画(源码)

 可执行文件

其中路径自己设置,图片自己找

效果图

package com.mr.draw;

import javax.swing.JFrame;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;


import java.awt.BorderLayout;//边界布局
import javax.swing.ButtonGroup;//按钮组
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;//按钮类
import javax.swing.JToggleButton;//可以显示状态的按钮
import javax.swing.JToolBar;//工具栏


import java.awt.BasicStroke;//基本画笔
import java.awt.event.ActionListener;//动作事件 
import java.awt.event.ActionEvent; //动作监听

import javax.swing.JColorChooser;

import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
//
//import com.mr.util.FrameGetShape;//获取图形的接口
//import com.mr.util.ShapeWindow;//选择图形的界面
//import com.mr.util.Shapes;//表示可以选择的图形

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import java.awt.AlphaComposite;//透明效果
import java.awt.Font;//字体类
import javax.swing.JOptionPane;//小对话框

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Cursor;
import java.awt.Point;

public class DrawPictureFRame extends JFrame{// implements FrameGetShape{
	BufferedImage image = new BufferedImage( 570, 390, BufferedImage.TYPE_INT_BGR);//图片大小 类型
	Graphics gs = image.getGraphics();//获得图像的绘制图像
	Graphics2D g = (Graphics2D) gs;//将绘制图像转换为Graphics2D;
	DrawPictureCanvas canvas = new DrawPictureCanvas();//创建画布对象
	Color forecolor = Color.BLACK;
	Color backgroundColor = Color.WHITE;//背景色
	
	//鼠标绘制点横纵坐标
	int x = -1;
	int y = -1;
	boolean rubber = false;//橡皮
//	boolean drawShape = false; //画图形标识变量
//	Shapes shape;//绘制的图形
	
	private JToolBar toolBar;//工具栏
	private JButton eraseButton;//橡皮类
	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 foregroundItem;
	private JMenuItem backgroundItem;
	private JMenuItem eraseMenuItem;
	private JMenuItem exitMenuItem;
	private JMenuItem savaMenuItem;
	 
	private JMenuItem shuiyinMenuItem;
	private String shuiyin = "";
	
	/*
	 * 构造方法
	 */
	public  DrawPictureFRame() {
		setResizable(false);
		setBounds( 500, 100, 574, 460);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("画图程序 (水印内容:[" + shuiyin + "] )");
		init();
		addListener();
	}
	
	private  void addListener() {
		//画板添加鼠标移动事件监听
		canvas.addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseDragged(final MouseEvent e){//拖拽鼠标
				if(x > 0 && y > 0){
					if(rubber){
						g.setColor(backgroundColor);
						g.fillRect(x, y, 10, 10);
					}else{
						g.drawLine(x, y, e.getX(), e.getY());
					}
				}
				x = e.getX();
				y = e.getY();
				canvas.repaint();
			}
		
			public void mouseMoved(final MouseEvent event){//当鼠标移动时
				if(rubber){
					Toolkit toolkit = Toolkit.getDefaultToolkit();//获得系统默认工具包
					
					Image image = toolkit.createImage("src/img/icom/鼠标橡皮.png");//利用工具包获取图像
					
					Cursor cursor = toolkit.createCustomCursor(image, new Point(0, 0), "c");
					setCursor(cursor);					
				}else{
					setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));//十字架
				}
				
			}
		});
	toolBar.addMouseMotionListener(new MouseMotionAdapter() {
	
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));//默认光标
		}
	});
	
	canvas.addMouseListener(new MouseAdapter(){
		public void mouseReleased(final MouseEvent arg0){
			 x = -1;
			 y = -1;
		}
	});
	
	strokeButton1.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			//声明画笔属性 :粗 细(单位像素) 末端无修饰 折线处呈尖角
			BasicStroke bStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
			g.setStroke(bStroke);
			
		}
	});
	
	
		strokeButton2.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			//声明画笔属性 :粗 细(单位像素) 末端无修饰 折线处呈尖角
			BasicStroke bStroke = new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
			g.setStroke(bStroke);
		}
	});

		strokeButton3.addActionListener(new ActionListener() {
	
	@Override
	public void actionPerformed(final ActionEvent e) {
		// TODO Auto-generated method stub
		//声明画笔属性 :粗 细(单位像素) 末端无修饰 折线处呈尖角
		BasicStroke bStroke = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
		g.setStroke(bStroke);
	}
		});
		
		
	backgroundButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			Color bgColor = new JColorChooser().showDialog(DrawPictureFRame.this,"选择颜色", Color.CYAN);
			if(bgColor != null){
				backgroundColor = bgColor;//如果选择颜色非空就把它赋值给背景颜色
			}
			//把按钮也设置为这种颜色
			backgroundButton.setBackground(backgroundColor);
			//设置背景
			g.setColor(backgroundColor);//画笔换为背景色
			g.fillRect(0, 0, 570, 390);//画满画布
			g.setColor(forecolor);//画笔设置为前景色
			canvas.repaint();	
		}
	});	
		
	
	foregroundButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			Color fColor = new JColorChooser().showDialog(DrawPictureFRame.this, "选择颜色", Color.CYAN);
			if(fColor != null){
				forecolor = fColor;
			}
			foregroundButton.setBackground(forecolor);
			g.setColor(forecolor);
		}
	});
	
	
	clearButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			g.setColor(backgroundColor);
			g.fillRect(0, 0, 570, 390);
			g.setColor(forecolor);
			canvas.repaint();
		}
	});
	

	
	eraseButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			if(!rubber){
				
				eraseButton.setIcon(new  ImageIcon("src/img/icom/画笔.png"));
				eraseMenuItem.setText("画笔");
				eraseButton.setToolTipText("画笔");
				rubber = true;
			}else{
				eraseButton.setIcon(new  ImageIcon("src/img/icom/橡皮.png"));
				eraseMenuItem.setText("橡皮");
				eraseButton.setToolTipText("橡皮");
				rubber = false;
				g.setColor(forecolor);
			}
			
		}
	});
	exitMenuItem.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			System.exit(0);
		}
	});
	clearMenuItem.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			g.setColor(backgroundColor);
			g.fillRect(0, 0, 570, 390);
			g.setColor(forecolor);
			canvas.repaint();
		}
	});
	
	foregroundItem.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			Color fColor = new JColorChooser().showDialog(DrawPictureFRame.this, "选择颜色", Color.CYAN);
			if(fColor != null){
				forecolor = fColor;
			}
			foregroundButton.setBackground(forecolor);
			g.setColor(forecolor);
		}
	});
		
	backgroundItem.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			Color bgColor = new JColorChooser().showDialog(DrawPictureFRame.this,"选择颜色", Color.CYAN);
			if(bgColor != null){
				backgroundColor = bgColor;//如果选择颜色非空就把它赋值给背景颜色
			}
			//把按钮也设置为这种颜色
			backgroundButton.setBackground(backgroundColor);
			//设置背景
			g.setColor(backgroundColor);//画笔换为背景色
			g.fillRect(0, 0, 570, 390);//画满画布
			g.setColor(forecolor);//画笔设置为前景色
			canvas.repaint();	
		}
	});	
	strokeMenuItem1.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			//声明画笔属性 :粗 细(单位像素) 末端无修饰 折线处呈尖角
			BasicStroke bStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
			g.setStroke(bStroke);
			strokeButton1.setSelected(true);
			
		}
	});
	strokeMenuItem2.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			//声明画笔属性 :粗 细(单位像素) 末端无修饰 折线处呈尖角
			BasicStroke bStroke = new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
			g.setStroke(bStroke);
			strokeButton2.setSelected(true);
		}
	});
	strokeMenuItem3.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			//声明画笔属性 :粗 细(单位像素) 末端无修饰 折线处呈尖角
			BasicStroke bStroke = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
			g.setStroke(bStroke);
			strokeButton3.setSelected(true);
		}
			});
	eraseMenuItem.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(final ActionEvent e) {
			// TODO Auto-generated method stub
			if((!rubber)){
				eraseButton.setIcon(new  ImageIcon("src/img/icom/画笔.png"));
				eraseMenuItem.setText("画图");
				eraseButton.setToolTipText("画图");
				rubber = true;
			}else{
				eraseButton.setIcon(new  ImageIcon("src/img/icom/橡皮.png"));
				eraseMenuItem.setText("橡皮");
				eraseButton.setToolTipText("橡皮");
				rubber = false;
				g.setColor(forecolor);
			}
			
		}
		
	});
		
	shuiyinMenuItem.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			shuiyin = JOptionPane.showInputDialog(DrawPictureFRame.this, "您想添加什么水印?");
			
			if(null == shuiyin){
				shuiyin = "";
			}else{
				setTitle("画图程序 (水印内容:[" + shuiyin + "] )");
			}
			addWatermark();//添加水印
		}
		
	});

//	public void mousePressed(MouseEvent e){
//		if(drawShape){
//			switch (shape.getType()) {
//			case Shapes.YUAN:
//				int yuanX = e.getX() - shape.getWidth()/2;
//				int yuanY = e.getY() - shape.getHeigth()/2;
//				Ellipse2D yuan new Ellipse2D.Double(yuanX, yuanY, shape.getWidth(), shape.getHeigth());
//			    g.draw(yuan);
//				break;
//
//			default:
//				break;
//			}
//		}
//	}
	
	
	
	}
	
	
	
	public void init(){
		g.setColor(backgroundColor);//设置画笔颜色
		g.fillRect(0, 0, 570, 390);//用画笔填充
		g.setColor(forecolor);//设置画笔颜色
		canvas.setImage(image);//设置画布颜色
		Container s = getContentPane();
		s.add(canvas);
		
		toolBar = new JToolBar();
		getContentPane().add(toolBar, BorderLayout.NORTH);
		
		
		
		toolBar.addSeparator();
		
		saveButton = new JButton();
		saveButton.setToolTipText("保存");
		saveButton.setIcon(new ImageIcon("src/img/icom/保存.png"));
		toolBar.add(saveButton);
		
		strokeButton1 = new JToggleButton();
		strokeButton1.setToolTipText("细线");//悬停提示
		strokeButton1.setIcon(new ImageIcon("src/img/icom/1像素线条.png"));
		toolBar.add(strokeButton1);
		strokeButton2 = new JToggleButton();
	    strokeButton2.setToolTipText("粗线");
	    strokeButton2.setIcon(new ImageIcon("src/img/icom/2像素线条.png"));
		toolBar.add(strokeButton2);
		strokeButton3 = new JToggleButton();
	    strokeButton3.setToolTipText("较粗");
	    strokeButton3.setIcon(new ImageIcon("src/img/icom/4像素线条.png"));
		toolBar.add(strokeButton3);
		
		//按钮组 确保只有一个被选中
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(strokeButton1);
		buttonGroup.add(strokeButton2);
		buttonGroup.add(strokeButton3);
		
		toolBar.addSeparator();
		
		backgroundButton = new JButton();
		backgroundButton.setToolTipText("背景色");
		backgroundButton.setIcon(new ImageIcon("src/img/icom/背景色.png"));
		toolBar.add(backgroundButton);
		foregroundButton = new JButton();
		foregroundButton.setToolTipText("前景色");
		foregroundButton.setIcon(new ImageIcon("src/img/icom/前景色.png"));
		toolBar.add(foregroundButton);
		
		toolBar.addSeparator();
		clearButton = new JButton();
		clearButton.setToolTipText("清除");
		clearButton.setIcon(new ImageIcon("src/img/icom/清除.png"));
		toolBar.add(clearButton);
		
		
		eraseButton = new JButton();
		eraseButton.setToolTipText("橡皮");
		eraseButton.setIcon(new ImageIcon("src/img/icom/橡皮.png"));
		toolBar.add(eraseButton);
		
		shapeButton = new JButton();
		shapeButton.setToolTipText("图形");
		shapeButton.setIcon(new ImageIcon("src/img/icom/形状.png"));
		toolBar.add(shapeButton);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);//窗体载入菜单(set)
		
		JMenu systemMenu = new JMenu("系统");
		menuBar.add(systemMenu);
		shuiyinMenuItem = new JMenuItem("水印");
		systemMenu.add(shuiyinMenuItem);
		systemMenu.addSeparator();
		savaMenuItem = new JMenuItem("保存");
		systemMenu.add(savaMenuItem);
		systemMenu.addSeparator();
		exitMenuItem = new JMenuItem("退出");
		systemMenu.add(exitMenuItem);
		
		JMenu menu = new JMenu("线型");
		menuBar.add(menu);
		strokeMenuItem1 = new JMenuItem("细线");
		menu.add(strokeMenuItem1);
		menu.addSeparator();
		strokeMenuItem2 = new JMenuItem("粗线");
		menu.add(strokeMenuItem2);
		menu.addSeparator();
		strokeMenuItem3 = new JMenuItem("较粗");
		menu.add(strokeMenuItem3);
	
		JMenu menu2 = new JMenu("颜色");
		menuBar.add(menu2);
		foregroundItem = new JMenuItem("前景颜色");
		menu2.add(foregroundItem);
		menu2.addSeparator();
		backgroundItem = new JMenuItem("背景颜色");
		menu2.add(backgroundItem);
		
		JMenu menu3 = new JMenu("编辑");
		menuBar.add(menu3);
		clearMenuItem = new JMenuItem("清除");
		menu3.add(clearMenuItem);
		menu3.addSeparator();
		eraseMenuItem = new JMenuItem("橡皮");
		menu3.add(eraseMenuItem);
	}
	
	private void addWatermark(){
		if(!"".equals(shuiyin.trim()))//shuiyin.trim()去除空格 如果水印字段不是空字符
		{
			g.rotate(Math.toRadians(-30));//顺时针旋转30°
			Font font =  new Font("楷体", Font.BOLD, 72);//设置字体
			g.setFont(font);//载入字体
			g.setColor(Color.GRAY);//灰色
			AlphaComposite alphaComposite = AlphaComposite.SrcOver.derive(0.4f);//设置透明效果
			g.setComposite(alphaComposite);
			g.drawString(shuiyin, 150, 500);
			
			canvas.paint(g);
			
			g.rotate(30);//旋转回去
			alphaComposite = AlphaComposite.SrcOver.derive(1f);//设置不透明效果
			g.setComposite(alphaComposite);//使用
			g.setColor(forecolor);//设置画笔颜色
		}
			
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new DrawPictureFRame().setVisible(true);
		
		
//		public void getShape(Shapes shape){ 
//			this.shape = shape;//将返回的图形对象付给类的全局变量
//			drawShape = true;//画图形标识变量为true,说明鼠标画的是图形,而不是线
//		}
	}

}
package com.mr.draw;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;

public class DrawPictureCanvas extends Canvas{

	private  Image image = null;
	/*
	 *  设置画板中的图片
	 *  image //画板展示的对象
	 */
	public void setImage(Image image){
		this.image = image;	
	}
	/*
	 * 重写paint方法
	 */
	public void paint(Graphics g){
		g.drawImage(image, 0, 0, null);
		
	}
	
	public void update(Graphics g){
		paint(g);//调用paint方法
	}
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券