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;
补充:软件开发 , Java ,