java 将画布内容保存到本地图片(简单画图板)

概述: 一个简单的画图板,实现将画布内容保存为本地图片

1、手绘图形

    可以画任意图形,即在鼠标拖动下实现任意线段的绘制。

    基本原理是扩展swing中的JPanel类(也可以用Java的Canvas类,但两者还是有些区别的) ,同时添加对鼠标的监听。任意的线段绘制,可以看做是一点一点的很小的线段叠加而成,就是点构成线么,即在鼠标拖动监听器里每次采集点,然后跟上一次的点构成线,同时每次都repaint,而在paint方法里就要实现对对所有点的一次绘制。

    在这里用到的鼠标监听事件是鼠标的拖动,要实现 MouseMotionListener 这个接口。

完整代码:

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener{
	private int width = 300;
	private int height = 300;
	private int x = 0;
        private int y = 0;
    
	public ArrayList myList = new ArrayList();
	
	public MyCanvas()
	{
		super();
		this.setSize(width, height);
		this.setBounds(new Rectangle(0,0,450,300));
		this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		this.setBackground(Color.WHITE);
		this.setVisible(true);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
	
	public void paint(Graphics graphics){
        super.paint(graphics);
        Graphics2D g2d = (Graphics2D)graphics;
        g2d.setBackground(Color.WHITE);

        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(10));
        for(int i=0;i

在这里遇到一个小问题,本来是用继承Canvas类来做的,但是画布上写东西的时候会不停地闪烁,而且之后图片也一直保存不下来,一直没找到问题所在,换成JPanel就好了,个人猜测是组件在调用repaint()方法时的问题,在这里不多说。屏幕闪烁可以用双缓冲解决。

2、保存图片到本地

    主要用到awt的image类以及IO流。

    主要步骤:新建一个BufferedImage ,然后利用graphics在其上在画一遍存在于canvas上的内容,然后将BufferedImage保存于本地即可,利用ImageIO里的静态方法write()。

部分代码:

        Dimension imagesize = this.canvas.getSize();
	BufferedImage image = new BufferedImage(imagesize.width,imagesize.height,BufferedImage.TYPE_INT_RGB);	
	Graphics2D graphics = image.createGraphics();
        this.canvas.paint(graphics);
        graphics.dispose();
        File f=new File("src/temp/1.jpg");
	if( !f.exists() )
        {
	    f.createNewFile();
	    System.out.println(123);
	}
	ImageIO.write(myImage, "jpg",f);
	return null;

3、调整保存图片的大小

因为笔者需要用到固定大小的图片,所以对图片进行了调整,之后保存。

完整代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;



public class MainofFrame extends JFrame{
	private int width = 450;
	private int height = 450;
	private MyCanvas canvas = null;
	
	public MainofFrame()
	{
		super();
		this.setTitle("paint");
		this.setSize(width, height);
		this.setResizable(false);
		this.setLocation(400, 180);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		
		this.canvas = new MyCanvas();
		this.add(canvas);
		
		this.save = new JButton();
		this.save.setText("save");
		this.save.setBounds(40, 360, 80, 30);
		this.save.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					savePic();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

		});
		this.add(save);
		this.setVisible(true);
	}
	
	public String savePic() throws IOException{
		Dimension imagesize = this.canvas.getSize();
		BufferedImage image = new BufferedImage(imagesize.width,imagesize.height,BufferedImage.TYPE_INT_RGB);
		
		Graphics2D graphics = image.createGraphics();
                this.canvas.paint(graphics);
                graphics.dispose();

                Image newImage = image.getScaledInstance(28, 28, Image.SCALE_SMOOTH);
                BufferedImage myImage = new BufferedImage(28,28,BufferedImage.TYPE_INT_RGB);
                Graphics graphics1 = myImage.getGraphics();
                graphics1.drawImage(newImage, 0, 0, null);
                graphics1.dispose();//对图片调整大小

                File f=new File("src/temp/1.jpg");
		if( !f.exists() )
		{
			f.createNewFile();
			System.out.println(123);
		}
			
		ImageIO.write(myImage, "jpg",f);
		return null;
	}
}
正在学习中,如果有错误希望帮忙矫正,十分感谢!

    +(在eclipse上复制粘贴过来的,不知道代码为什么排版这么乱,还望谅解。。。)




你可能感兴趣的:(Java)