java画板小demo

用java写的一个画板demo 是Java课程的一个作业,比较简单 直接贴代码,有不完善的地方,但是因为对swing编程没什么兴趣 所以暂时不想改了

MyPanel.java

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Vector;

public class MyPanel extends Panel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	enum DrawType
	{
		FreeDom,
		Line,
		Ellipse,
		Rectangle
	}
	
	private Vector<Vector<Point>> FreedomDatas = new Vector<Vector<Point>>(); //freeDom时使用
	private Vector<Vector<Point>> lineDatas = new Vector<Vector<Point>>(); //Line时使用
	private Vector<Vector<Point>> ellipseDatas = new Vector<Vector<Point>>();
	private Vector<Vector<Point>> rectangleDatas = new Vector<Vector<Point>>();
	
	private Color lineColor = Color.blue;
	private int lineWidth = 5;
	private DrawType drawType = DrawType.FreeDom;
	
	private Vector<Vector<Vector<Point>>> allWriteDatas = new Vector<Vector<Vector<Point>>>();
	private Vector<Vector<Vector<Point>>> allReadDatas = new Vector<Vector<Vector<Point>>>();
	
	public MyPanel()
	{
		addMouseListener(new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				switch(drawType)
				{
					case FreeDom:
						Point p = new Point(e.getX(),e.getY());
						Vector<Point> newLine = new Vector<Point>();
						newLine.add(p);
						FreedomDatas.add(newLine); 
						break;
					case Line:
						//画直线
						Point prePoint = new Point(e.getX(),e.getY());
						Vector<Point> newl = new Vector<Point>();
						newl.add(prePoint);
						lineDatas.add(newl);
						break;
					case Ellipse:
						//画椭圆
						Point newPoint = new Point(e.getX(),e.getY());
						Vector<Point> newp = new Vector<Point>();
						newp.add(newPoint);
						ellipseDatas.add(newp);
						break;
					case Rectangle:
						//画矩形
						Point nPoint = new Point(e.getX(),e.getY());
						Vector<Point> newn = new Vector<Point>();
						newn.add(nPoint);
						rectangleDatas.add(newn);
						break;
				}

			}
			
			public void mouseReleased(MouseEvent e)
			{
				switch(drawType)
				{
				case FreeDom:
					break;
				case Line:
					Point endPoint = new Point(e.getX(),e.getY());
					int n = lineDatas.size() - 1;
					Vector<Point> newl = lineDatas.get(n);
					newl.add(endPoint);
					break;
				case Ellipse:
					Point endPoint2 = new Point(e.getX(),e.getY());
					int n2 = ellipseDatas.size() - 1;
					Vector<Point> newl2 = ellipseDatas.get(n2);
					newl2.add(endPoint2);
					break;
				case Rectangle:
					Point endPoint3 = new Point(e.getX(),e.getY());
					int n3 = rectangleDatas.size() - 1;
					Vector<Point> newl3 = rectangleDatas.get(n3);
					newl3.add(endPoint3);
					break;
				}
				repaint(); //刷新画面
			}
		});
		
		addMouseMotionListener(new MouseMotionAdapter()
		{
			public void mouseDragged(MouseEvent e)
			{
				switch(drawType)
				{
				case FreeDom:
					Point p = new Point(e.getX(),e.getY());
					int n = FreedomDatas.size()-1; //拿到最后一条线的位置
					Vector<Point> lastLine = FreedomDatas.get(n);
					lastLine.add(p);
					break;
				case Line:
					//直线
					break;
				case Ellipse:
					//椭圆
					break;
				case Rectangle:
					//矩形
					break;
				}

				//repaint(); //刷新画面
			}
		});
	}
	
	@Override
	public void paint(Graphics g)
	{
		g.clearRect(0, 0, getWidth(), getHeight());
		g.setColor(lineColor);
		
		Graphics2D g_2D = (Graphics2D)g;
		BasicStroke stroke = new BasicStroke(lineWidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
		g_2D.setStroke(stroke);
		
		switch(drawType)
		{
		case FreeDom:
			Vector<Point> v;
			Point s,e;
			int i,j,m;
			int n = FreedomDatas.size();
			for(i=0;i<n;i++)
			{
				v = FreedomDatas.get(i);
				m = v.size()-1;
				for(j=0;j<m;j++)
				{
					s = (Point)v.get(j);
					e = (Point)v.get(j+1);
					g.drawLine(s.x, s.y, e.x, e.y);
				}
			}
			break;
		case Line:
			//直线
			int ls = lineDatas.size();
			for(int li=0;li<ls;li++)
			{
				Vector<Point> twoPoints = lineDatas.get(li);
				Point startPoint = twoPoints.get(0);
				Point endPoint = twoPoints.get(1);
				g.drawLine(startPoint.x,startPoint.y,endPoint.x,endPoint.y);
			}
			break;
		case Ellipse:
			//椭圆
			int es = ellipseDatas.size();
			for(int ei=0;ei<es;ei++)
			{
				Vector<Point> twoPoints = ellipseDatas.get(ei);
				Point startPoint = twoPoints.get(0);
				Point endPoint = twoPoints.get(1);
				int width = Math.abs(endPoint.x - startPoint.x);
				int height = Math.abs(endPoint.y - startPoint.y);
				g.drawOval(startPoint.x,startPoint.y,width,height);
			}
			break;
		case Rectangle:
			//矩形
			int rs = rectangleDatas.size();
			for(int ri=0;ri<rs;ri++)
			{
				Vector<Point> twoPoints = rectangleDatas.get(ri);
				Point startPoint = twoPoints.get(0);
				Point endPoint = twoPoints.get(1);
				int width = Math.abs(endPoint.x - startPoint.x);
				int height = Math.abs(endPoint.y - startPoint.y);
				g.drawRect(startPoint.x,startPoint.y,width,height);
			}
			break;
		}

	}
	
	public void setDrawType(int i)
	{
		switch(i)
		{
		case 0:
			drawType = DrawType.FreeDom;
			break;
		case 1:
			drawType = DrawType.Line;
			break;
		case 2:
			drawType = DrawType.Ellipse;
			break;
		case 3:
			drawType = DrawType.Rectangle;
			break;
		}
		repaint();
	}
	
	public void setLineWigth(int i)
	{
		lineWidth = i;
		repaint();
	}
	
	public void setLineColor(int i)
	{
		switch(i)
		{
		case 0:
			lineColor = Color.red;
			break;
		case 1:
			lineColor = Color.yellow;
			break;
		case 2:
			lineColor = Color.green;
			break;
		case 3:
			lineColor = Color.gray;
			break;
		case 4:
			lineColor = Color.black;
			break;
		case 5:
			lineColor = Color.blue;
			break;
		}
		repaint();
	}
	
	public void cleanAll()
	{
		FreedomDatas.clear();
		lineDatas.clear();
		ellipseDatas.clear();
		rectangleDatas.clear();
		repaint();
	}
	
	public void saveToFile()
	{
		allWriteDatas.clear();
		allWriteDatas.add(FreedomDatas);
		allWriteDatas.add(lineDatas);
		allWriteDatas.add(ellipseDatas);
		allWriteDatas.add(rectangleDatas);
		
		try
		{
			BufferedWriter output = new BufferedWriter(new FileWriter("out.dat"));
			
			for(int s=0;s<allWriteDatas.size();s++)
			{
				Vector<Vector<Point>> datas = allWriteDatas.get(s);
				int ss = datas.size();
				output.write(new Integer(ss).toString());
				output.newLine();
				for(int i=0;i<datas.size();i++)
				{
					Vector<Point> ps = datas.get(i);
					int pp = ps.size();
					output.write(new Integer(pp).toString());
					output.newLine();
					
					for(int j=0;j<ps.size();j++)
					{
						Point p = ps.get(j);
						int x = (int)p.getX();
						int y = (int)p.getY();
						output.write(new Integer(x).toString());
						output.newLine();
						output.write(new Integer(y).toString());
						output.newLine();
					}
				}
			}
			output.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void readFromFile()
	{
		FreedomDatas.clear();
		lineDatas.clear();
		ellipseDatas.clear();
		rectangleDatas.clear();
		try
		{
			BufferedReader input = new BufferedReader(new FileReader("out.dat"));
			for(int i=0;i<4;i++)
			{
				Vector<Vector<Point>> datas = new Vector<Vector<Point>>();
				String str = input.readLine();
				int size = Integer.parseInt(str);
				for(int j=0;j<size;j++)
				{
					Vector<Point> ps = new Vector<Point>();
					String st = input.readLine();
					int pp = Integer.parseInt(st);
					
					for(int k=0;k<pp;k++)
					{
						String sx = input.readLine();
						int x = Integer.parseInt(sx);
						String sy = input.readLine();
						int y = Integer.parseInt(sy);
						Point p = new Point(x,y);
						ps.add(p);
					}
					datas.add(ps);
				}
				allReadDatas.add(datas);
			}

			Vector<Vector<Point>> clone1 = (Vector<Vector<Point>>) allReadDatas.get(0).clone();
			FreedomDatas = clone1;
			Vector<Vector<Point>> clone2 = (Vector<Vector<Point>>) allReadDatas.get(1).clone();
			lineDatas = clone2;
			Vector<Vector<Point>> clone3 = (Vector<Vector<Point>>) allReadDatas.get(2).clone();
			ellipseDatas = clone3;
			Vector<Vector<Point>> clone4 = (Vector<Vector<Point>>) allReadDatas.get(3).clone();
			rectangleDatas = clone4;
			
			repaint();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
 	}
}

drawFrame.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class DrawFrame extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public DrawFrame()
	{
		super("20102100227 王嘉铠");
		
		//菜单栏
		JMenuBar myBar = new JMenuBar();
		setJMenuBar(myBar);
		
		JMenu []m = {new JMenu("文件"), new JMenu("图形"), new JMenu("线条宽度"), new JMenu("颜色")};
		
		JMenuItem [][]mI = {
					{new JMenuItem("打开"),new JMenuItem("新建"),new JMenuItem("保存")},
					{new JMenuItem("自由"),new JMenuItem("直线"),
					 new JMenuItem("椭圆"),new JMenuItem("矩形")},
					{new JMenuItem("5"), new JMenuItem("10"),new JMenuItem("15"),new JMenuItem("20")},
					{new JMenuItem("红色"),new JMenuItem("黄色"),new JMenuItem("绿色"),
					 new JMenuItem("灰色"),new JMenuItem("黑色"), new JMenuItem("蓝色")}
				};
		
		int i;
		int j;
		for(i=0;i<m.length;i++)
		{
			myBar.add(m[i]);
			for(j=0;j<mI[i].length;j++)
			{
				m[i].add(mI[i][j]);
			}
		}
		
		//画板
		Container contentPane = getContentPane();
		contentPane.setBounds(0, myBar.getHeight(),getWidth(),getHeight() - myBar.getHeight());
		final MyPanel panel = new MyPanel();
		contentPane.add(panel);
		
		//各种监听器

		mI[0][0].addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				panel.readFromFile();
			}
			
		});
		
		mI[0][1].addActionListener(new ActionListener()
		{
			//新建
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				panel.cleanAll();
			}
			
		});
		
		mI[0][2].addActionListener(new ActionListener()
		{
			//保存
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				panel.saveToFile();
			}
			
		});
		
		for(int type=0;type<4;type++)
		{	
			//图形
			final int t = type;
			mI[1][type].addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e) {
					panel.setDrawType(t);
				}	
			});
		}
		
		for(int type=0;type<4;type++)
		{
			//线宽
			mI[2][type].addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e) {
					JMenuItem j = (JMenuItem)e.getSource();
					int width = Integer.parseInt(j.getText().toString());
					panel.setLineWigth(width);
				}
				
			});
		}
		
		for(int type=0;type<6;type++)
		{
			//颜色
			final int t = type;
			mI[3][type].addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e) {
					panel.setLineColor(t);
				}
			});
		}
	}
	
}

mouseDraw.java

import javax.swing.JFrame;


public class MouseDraw {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DrawFrame app = new DrawFrame();
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setBounds(450, 100, 500, 500);
		app.setVisible(true);
	}

}

运行效果


源码下载:JAVA画板DEMO

1. 画板UI设计(总体布局) (1)创建窗体并设置相关属性; (2)给整个窗体添加一个中间容器用来覆盖整个窗体; (3)设置边框布局,因为整个画板大致为左边部分,中间部分,菜单栏三个部分,用边框布局比较合适; (4)给窗体添加左面板,中间面板; (5)给左面板添加按钮,并设置按钮图片和效果; (6)给左面板添加左子面板,用来存放颜色按钮; 2.画板功能设计 (1)给左画板中的按钮组中的每个按钮添加鼠标监听器; (2)点击不同按钮,绘制不同的图形; (3)给左子面板中的每个颜色按钮添加鼠标监听器; (4)根据下面板中选中的颜色按钮,来获取按钮的背景颜色,并将该颜色设置成画笔的颜色; (5)铅笔功能 1、铅笔是鼠标拖动时画的线,所需要实现鼠标移动监听器,我们采用一个类来实现多个接口; 2、添加新的鼠标监听器类; 3、在鼠标移动时间中实现画笔的逻辑(其实就是绘制直线,鼠标每移动一个像素,就会触发移动事件,通过移动事件获取鼠标的坐标,与上一次移动的坐标连线就可以了; (6)刷子功能 刷子其实就是加粗的画笔,画出来的直线更粗,这里需要用到Graphic2D画笔来设置画笔的粗细。 (7)橡皮擦功能 橡皮擦就是把画笔颜色设置成相同的背景颜色就可以了,拖动鼠标时进行擦除,所以在鼠标拖动事件中编写。 (8)喷桶功能 1、定位方法:鼠标拖动事件实现; 2、随机数生成:Random; 3、实现原理,在鼠标拖动附近绘制很多的原点; 3. 画板的保存和重绘的设计 (1)给我们的画板添加菜单条、菜单以及菜单项 (2)给每个菜单项添加监听器 (3)点击不同的菜单项实现相应的功能 (4)图形保存功能:利用对象输出流,将容器对象写入文件; (5)打开图形功能:利用对象输入流,将容器对象读入,然后将容器里面的图形对象在画板上绘制出来 (6)新建文件功能:新建文件,将画板上绘制的内容清空(清空之前可以确认是否需要进行保存)清屏(重绘)功能依次方法; (7)文件保存格式为BMP格式; (8)文字功能:读取文本框中的文本并打印到屏幕鼠标的相应的响应位置,传入输入的文字大小的参数,以此来改变文字的大小; 4.弹泡泡功能的设计 根据Java多线程来实现弹泡泡功能;泡泡的位置颜色随机出现,并且做到碰到边框会变色;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值