java 实现画图板

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;

import java.util.ArrayList;
import javax.swing.*;

class Dshape implements Serializable{
	private static final long serialVersionUID = 1L;
	Shape s;
	Color c;
	boolean f;//填充状态
	String m=new String();//绘画模式
	private void writeObject(java.io.ObjectOutputStream out) throws IOException
	{
		out.defaultWriteObject();
	}
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		in.defaultReadObject();
	}
}
class Dstr implements Serializable{
	private static final long serialVersionUID = 1L;
	String s;
	Font f;
	Color c;
	int x,y;//坐标
	private void writeObject(java.io.ObjectOutputStream out) throws IOException
	{
		out.defaultWriteObject();
	}
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		in.defaultReadObject();
	}
}
class DArray implements Serializable{
	private static final long serialVersionUID = 1L;
	ArrayList<Dshape> shapelist=new ArrayList<Dshape>();
	ArrayList<Dstr> strlist=new ArrayList<Dstr>();
	Color backColor;//背景色
	private void writeObject(java.io.ObjectOutputStream out) throws IOException
	{
		out.defaultWriteObject();
	}
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		in.defaultReadObject();
	}
}

class MA extends MouseAdapter{
	DrawPanel dp;
	MA(DrawPanel dp)
	{
		super();
		this.dp=dp;
	}
	@Override
	public void mousePressed(MouseEvent e){//鼠标按下
	    if(dp.tempshape!=null) {
	    	dp.da.shapelist.add(dp.tempshape);
	    }
	    switch(dp.model)
	    //据绘制模式对“当前绘制图形(tempshape)”进行初始化
	    {
	    case "圆":
	    {
	    	dp.tempshape=new Dshape();
	    	Ellipse2D ep1=new Ellipse2D.Double();
	    	ep1.setFrame(e.getX(), e.getY(), 0, 0);
	    	dp.tempshape.c=dp.currColor;
	    	dp.tempshape.s=ep1;
	    	dp.tempshape.f=dp.fill;
	    	dp.tempshape.m="Ellipse";
			break;
	    }
	    case "直线":
	    {
	    	dp.tempshape=new Dshape();
	    	Line2D l=new Line2D.Double();
			l.setLine(e.getX(), e.getY(), e.getX(), e.getY());
			dp.tempshape.c=dp.currColor;
			dp.tempshape.s=l;
			dp.tempshape.f=dp.fill;
			dp.tempshape.m="Line";
			break;
	    }
	    case "椭圆":
	    {
	    	dp.tempshape=new Dshape();
		//Arc2D ep = new Arc2D.Double();
	    	Ellipse2D ep=new Ellipse2D.Double();
	    	ep.setFrame(e.getX(), e.getY(), 0, 0);
	    	dp.tempshape.c=dp.currColor;
	    	dp.tempshape.s=ep;
	    	dp.tempshape.f=dp.fill;
	    	dp.tempshape.m="Ellipse";
			break;
	    }
	    case "矩形":
	    {
	    	dp.tempshape=new Dshape();
	    	Rectangle2D ra=new Rectangle2D.Double();
	    	ra.setFrame(e.getX(), e.getY(), 0, 0);
	    	dp.tempshape.c=dp.currColor;
	    	dp.tempshape.s=ra;
	    	dp.tempshape.f=dp.fill;
	    	dp.tempshape.m="Rectangle";
			break;
	    }
	    }
	}
	@Override
	public void mouseReleased(MouseEvent e){//鼠标松开
		switch(dp.model)
		//完成tempshape的绘制
	    {
	    case "圆":
	    {
	    	Ellipse2D ep1=(Ellipse2D)(dp.tempshape.s);
	    	ep1.setFrame(ep1.getX(), ep1.getY(), e.getX()-ep1.getX(), e.getX()-ep1.getX());
	    	dp.repaint();
	    	break;
	    }
	    case "直线":
	    {
	    	Line2D l=(Line2D)(dp.tempshape.s);
			l.setLine(l.getX1(), l.getY1(), e.getX(), e.getY());
			dp.repaint();
			break;
	    }
	    case "椭圆":
	    {
	    	Ellipse2D ep=(Ellipse2D)(dp.tempshape.s);
	    	ep.setFrame(ep.getX(), ep.getY(), e.getX()-ep.getX(), e.getY()-ep.getY());
	    	dp.repaint();
	    	break;
	    }
	    case "矩形":
	    {
	    	Rectangle2D ra=(Rectangle2D)(dp.tempshape.s);
	    	ra.setFrame(ra.getX(), ra.getY(), e.getX()-ra.getX(), e.getY()-ra.getY());
	    	dp.repaint();
	    	break;
	    }
	    }
	}
	@Override
	public void mouseClicked(MouseEvent e){//鼠标点击
	//插入文本
		if(e.getButton()==MouseEvent.BUTTON3)
		{
			String inputValue = JOptionPane.showInputDialog("Please input text"); 
			Font f=new Font("宋体",20,20);
			Dstr ms=new Dstr();
			ms.s=inputValue;
			ms.c=dp.currColor;
			ms.f=f;
			ms.x=e.getX();
			ms.y=e.getY();
			dp.da.strlist.add(ms);
			dp.repaint();
		}

	}
}
class MMA extends MouseMotionAdapter{
	private static final long serialVersionUID = 1L;
	DrawPanel dp;
	MMA(DrawPanel dp)
	{
		super();
		this.dp=dp;
	}
	@Override
	public void mouseDragged(MouseEvent e) {//鼠标拖动
	//仅自由绘画模式触发
	    if(dp.model.equals("Draw"))
	    {
	    	if(dp.tempshape!=null) {
				Line2D l=(Line2D)(dp.tempshape.s);
				l.setLine(l.getX1(), l.getY1(), e.getX(), e.getY());
				dp.da.shapelist.add(dp.tempshape);
		    }
	    	dp.tempshape=new Dshape();
			Line2D l=new Line2D.Double();
			l.setLine(e.getX(), e.getY(), e.getX(), e.getY());
			dp.tempshape.c=dp.currColor;
			dp.tempshape.s=l;
			dp.repaint();
	    }
	}
}

class DrawPanel extends JPanel{
	DArray da= new DArray();
	Dshape tempshape;
	Color currColor;
	boolean fill=false;
	String model=new String();
	public DrawPanel()
	{
		super();
		MA ma=new MA(this);
		this.addMouseListener(ma);
		MMA mma=new MMA(this);
		this.addMouseMotionListener(mma);
		//设置监听器
	}
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d=(Graphics2D) g;
		for(Dshape ms:da.shapelist) {
			g2d.setColor(ms.c);
			if(ms.f&&!ms.m.equals("Line"))
				g2d.fill(ms.s);
			else
			    g2d.draw(ms.s);
		}//绘制已记录的图形
		for(Dstr ms:da.strlist) {
			g2d.setColor(ms.c);
			g2d.setFont(ms.f);
			g2d.drawString(ms.s,ms.x,ms.y);
		}//插入已记录的文本
		if(tempshape!=null) {
			g2d.setColor(tempshape.c);
			if(tempshape.f&&!tempshape.m.equals("Line"))
				g2d.fill(tempshape.s);
			else
			    g2d.draw(tempshape.s);
		}//记录当前绘制的图形(若非null)
	}
	
}

class MTLis implements ActionListener{
	DrawBoard bo;
	MTLis(DrawBoard bo)
	{
		super();
		this.bo=bo;
	}
	@Override
	public void actionPerformed(ActionEvent ae) {
		// TODO Auto-generated method stub
		if(ae.getSource().getClass()==JButton.class)
		{
			JButton mid=(JButton)ae.getSource();
			if(mid.getText().equals("打开"))
			{
				JFileChooser jf=new JFileChooser();
				jf.showOpenDialog(bo);	
				String s=jf.getSelectedFile().toString();
				File f=new File(s);
				try {
					read(f);
				} catch (ClassNotFoundException | IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				bo.dpl.repaint();
				bo.dpl.setBackground(bo.dpl.da.backColor);
				bo.message.append("File open "+s+"\n");
			}//加载文件并将其绘制出来,同时设置背景色
			if(mid.getText().equals("保存"))
			{
				if(bo.dpl.tempshape!=null)
					bo.dpl.da.shapelist.add(bo.dpl.tempshape);
				JFileChooser jf=new JFileChooser();
				int n=jf.showSaveDialog(bo);
				String s=jf.getSelectedFile().toString();
				File f=new File(s);
				try {
					write(f);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				bo.message.append("File save "+s+"\n");
			}//记录当前绘制图形,序列化保存
			if(mid.getText().equals("红色")) {
				JColorChooser jc=new JColorChooser();				
				Color c=new Color(255,0,0);
				bo.dpl.currColor=c;
			}//更改前端颜色
			if(mid.getText().equals("黑色"))
			{
				JColorChooser jc=new JColorChooser();				
				Color c=new Color(0,0,0);
				bo.dpl.currColor=c;
			}//更改填充状态
			if(mid.getText().equals("蓝色")) {
				JColorChooser jc=new JColorChooser();				
				Color c=new Color(0,0,255);
				bo.dpl.currColor=c;	
			}//更改背景色
			if(mid.getText().equals("圆"))
			{
				bo.dpl.model="圆";
			}
			if(mid.getText().equals("直线"))
			{
				bo.dpl.model="直线";
			}
			if(mid.getText().equals("椭圆"))
			{
				bo.dpl.model="椭圆";
			}
			if(mid.getText().equals("矩形"))
			{
				bo.dpl.model="矩形";
			}//更改绘画模式
		}
	}
	void write(File f) throws IOException
	{
		FileOutputStream fo=new FileOutputStream(f);
		@SuppressWarnings("resource")
		ObjectOutputStream oo=new ObjectOutputStream(fo);
		oo.writeObject(bo.dpl.da);
	}//序列化写
	void read(File f) throws IOException, ClassNotFoundException
	{
		FileInputStream fi=new FileInputStream(f);
		@SuppressWarnings("resource")
		ObjectInputStream oi=new ObjectInputStream(fi);
		DArray dp=(DArray)oi.readObject();
		bo.dpl.da=dp;
	}//序列化读
}


public class DrawBoard extends JFrame{
	JPanel menu;
	JButton open,save,color,fill,bgc;
	JPanel tool;
	JButton draw,line,el,rect;
	DrawPanel dpl;
	JTextArea message;
	JScrollPane frame;
	DrawBoard(String title)
	{
		super();
		this.setTitle(title);
		menu=new JPanel(new GridLayout(1,5));
		open=new JButton("打开");
		save=new JButton("保存");
		color=new JButton("红色");
		fill=new JButton("黑色");
		bgc=new JButton("蓝色");
		menu.add(open);
		menu.add(save);
		menu.add(color);
		menu.add(fill);
		menu.add(bgc);
		tool=new JPanel(new GridLayout(4,1));
		draw=new JButton("圆");
		line=new JButton("直线");
		el=new JButton("椭圆");
		rect=new JButton("矩形");
		tool.add(draw);
		tool.add(line);
		tool.add(el);
		tool.add(rect);
		dpl=new DrawPanel();
		frame = new JScrollPane(message);
		this.getContentPane().add("North",menu);
		this.getContentPane().add("West",tool);
		this.getContentPane().add("Center",dpl);
		this.getContentPane().add("South",frame);
		addli();
		this.setSize(800,500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);	
	}
	void addli()
	{
		MTLis m =new MTLis(this);
		open.addActionListener(m);
		save.addActionListener(m);
		color.addActionListener(m);
		fill.addActionListener(m);
		bgc.addActionListener(m);
		draw.addActionListener(m);
		line.addActionListener(m);
		el.addActionListener(m);
		rect.addActionListener(m);
	}//加入监听器
	public static void main(String[]arg)
	{
		DrawBoard d=new DrawBoard("Test");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值