`

Applet画图修改中。。。

阅读更多

效果如下图:



 

import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

public class DrawImage extends Applet implements ItemListener , MouseListener ,
		MouseMotionListener , ActionListener
{
	private static final long serialVersionUID = 1L;
	int count = 8; //装载图片的数量
	Vector<Vector<Object>> v = new Vector<Vector<Object>>(); //存储绘图信息
	Image[] img = new Image[count];

	int myWidth = 0; //箭头起点坐标
	int myHeight = 0; //箭头起点坐标
	int widthcount = 0; //工具栏上图标之间隔

	//boolean[] b = new boolean[count];
	int[] width = new int[count];
	int[] height = new int[count];
	int[] w = new int[count];
	int[] h = new int[count];
	int[] wid = new int[count];
	int[] hei = new int[count];
	int[] ww = new int[count];
	int[] hh = new int[count];

	int startX = 0;
	int startY = 0;
	int endX = 0;
	int endY = 0;	
	int clickX = 0; //点击动态图标时的X坐标
	int clickY = 0; //点击动态图标时的Y坐标
	int moveX = 0; //拖动动态图标时的X坐标
	int moveY = 0; //拖动动态图标时的Y坐标

	canvas[] can = null; //动态图标数组
	myCanvas[] mycan = new myCanvas[count]; //动态图标数组
	Canvas curcan = null; //当前获得焦点的图标
	Canvas curdelcan = null; //需要删除的当前图标
	Image curimg = null; //当前焦点图标的图象

	JPanel jp = new JPanel(null); //图像工具条面板
	MediaTracker mt = new MediaTracker(this); //媒体对象,用于装载图象到内存

	PopupMenu popMenu2 = new PopupMenu(); //右键菜单
	MenuItem menuItem1 = null;
	MenuItem menuItem2 = null;
	// MenuItem menuItem3 = null;
	MenuItem menuItem4 = null;
	MenuItem menuItem5 = null;

	int x = -1;
	int y = -1;
	int con = 1; // 画笔大小
	int Econ = 5; // 橡皮大小 

	int toolFlag = 0; //工具标记
	// toolFlag工具对应表:   
	// (0--画笔);(1--橡皮);(2--清除);   
	// (3--直线);(4--圆);(5--矩形);   

	Color c = new Color(0, 0, 0); // 画笔颜色

	BasicStroke size = new BasicStroke(this.con, 0, 2); // 画笔粗细
	Point cutflag = new Point(-1, -1, this.c, 6, this.con); // 截断标志

	Vector<Point> paintInfo = null; // 点信息向量组
	int n = 1;

	FileInputStream picIn = null;
	FileOutputStream picOut = null;

	ObjectInputStream VIn = null;
	ObjectOutputStream VOut = null;
	Panel toolPanel;
	Button eraser;
	Button drLine;
	Button drCircle;
	Button drRect;
	Button clear;
	Button pen;
	Choice ColChoice;
	Choice SizeChoice;
	Choice EraserChoice;
	Button colchooser;
	Label 颜色;
	Label 大小B;
	Label 大小E;
	Button openPic;
	Button savePic;
	//	FileDialog openPicture;
	BufferedImage bi = null;
	Graphics2D g2d = null;

	public void init()
	{
		//		int width2 = Toolkit.getDefaultToolkit().getScreenSize().width; // 屏幕的宽
		//		int height2 = Toolkit.getDefaultToolkit().getScreenSize().height; // 屏蔽的高
		//
		//		this.bi = new BufferedImage(width2, height2, 1);

		this.bi = new BufferedImage(1280, 800, 1); //内存中创建指定大小的绘图区域

		this.img[0] = getImage(getCodeBase(), "1.png");
		this.img[1] = getImage(getCodeBase(), "2.png");
		this.img[2] = getImage(getCodeBase(), "3.png");
		this.img[3] = getImage(getCodeBase(), "4.png");
		this.img[4] = getImage(getCodeBase(), "5.png");
		this.img[5] = getImage(getCodeBase(), "6.png");
		this.img[6] = getImage(getCodeBase(), "7.png");
		this.img[7] = getImage(getCodeBase(), "8.png");

		resize(800, 600); //设置applet的尺寸,在浏览器里浏览根据html代码来设置
		setLayout(null); //设置布局管理器,因为要移动图标的位置,因此设置为null   

		for (int i = 0; i < this.img.length; ++i)
		{
			this.mt.addImage(this.img[i], 0);
		}
		try
		{
			this.mt.waitForAll();
		}
		catch (Exception ex)
		{
			System.err.println(ex.toString());
		}

		add(this.jp);
		this.jp.setBounds(0, 0, 800, 80);

		//---------------绘制图像工具条------------------------------------------
		for (int i = 0; i < this.img.length; ++i)
		{
			this.mycan[i] = new myCanvas(this.img[i]);
			this.width[i] = this.img[i].getWidth(this);
			this.height[i] = this.img[i].getHeight(this);
			this.jp.add(this.mycan[i]);
			this.mycan[i].setBounds(100 + 5 * (i + 1) + this.widthcount, 5,
					this.img[i].getWidth(this), this.img[i].getHeight(this));
			this.widthcount += this.img[i].getWidth(this);
		}
		//---------------绘制图像工具条--------结束--------------------------------

		// ---------------加载右键菜单-----------------------------
		this.menuItem1 = new MenuItem("隐藏图像工具栏");
		this.menuItem2 = new MenuItem("显示图像工具栏");
		// this.menuItem3 = new MenuItem("清空画面");

		this.menuItem4 = new MenuItem("显示绘图工具栏");
		this.menuItem5 = new MenuItem("隐藏绘图工具栏");

		this.popMenu2.add(this.menuItem1);
		this.popMenu2.add(this.menuItem2);
		// this.popMenu2.add(this.menuItem3);
		this.popMenu2.addSeparator();
		this.popMenu2.add(this.menuItem4);
		this.popMenu2.add(this.menuItem5);

		add(this.popMenu2);

		this.menuItem1.addActionListener(this);
		this.menuItem2.addActionListener(this);
		// this.menuItem3.addActionListener(this);
		this.menuItem4.addActionListener(this);
		this.menuItem5.addActionListener(this);
		// ---------------加载右键菜单------结束---------------------

		// -----------------菜单条--------------------------------------
		this.toolPanel = new Panel(null); //菜单条面板
		this.toolPanel.setLayout(new FlowLayout());

		this.paintInfo = new Vector<Point>();

		this.ColChoice = new Choice(); // 画笔颜色选择下拉列表框
		this.ColChoice.add("黑色");
		this.ColChoice.add("红色");
		this.ColChoice.add("蓝色");
		this.ColChoice.add("绿色");
		this.ColChoice.addItemListener(this);

		// 画笔大小选择 
		this.SizeChoice = new Choice(); //画笔大小下拉列表框
		this.SizeChoice.add("1");
		this.SizeChoice.add("3");
		this.SizeChoice.add("5");
		this.SizeChoice.add("7");
		this.SizeChoice.add("9");
		this.SizeChoice.addItemListener(this);

		// 橡皮大小下拉列表框
		this.EraserChoice = new Choice();
		this.EraserChoice.add("5");
		this.EraserChoice.add("9");
		this.EraserChoice.add("13");
		this.EraserChoice.add("17");
		this.EraserChoice.addItemListener(this);

		this.clear = new Button("清除");
		this.eraser = new Button("橡皮擦");
		this.pen = new Button("画笔");
		this.drLine = new Button("画直线");
		this.drCircle = new Button("画圆形");
		this.drRect = new Button("画矩形");

		this.openPic = new Button("导入图片");
		this.savePic = new Button("保存图片");

		this.colchooser = new Button("显示调色板");

		// 各组件事件监听  
		this.clear.addActionListener(this);
		this.eraser.addActionListener(this);
		this.pen.addActionListener(this);
		this.drLine.addActionListener(this);
		this.drCircle.addActionListener(this);
		this.drRect.addActionListener(this);
		this.openPic.addActionListener(this);
		this.savePic.addActionListener(this);
		this.colchooser.addActionListener(this);

		this.颜色 = new Label("画笔颜色", 1);
		this.大小B = new Label("画笔大小", 1);
		this.大小E = new Label("橡皮大小", 1);

		this.toolPanel.add(this.openPic);
		this.toolPanel.add(this.savePic);

		this.toolPanel.add(this.pen);
		this.toolPanel.add(this.drLine);
		this.toolPanel.add(this.drCircle);
		this.toolPanel.add(this.drRect);

		this.toolPanel.add(this.颜色);
		this.toolPanel.add(this.ColChoice);
		this.toolPanel.add(this.大小B);
		this.toolPanel.add(this.SizeChoice);
		this.toolPanel.add(this.colchooser);

		this.toolPanel.add(this.eraser);
		this.toolPanel.add(this.大小E);
		this.toolPanel.add(this.EraserChoice);

		this.toolPanel.add(this.clear);

		add(this.toolPanel);
		this.toolPanel.setBounds(0, 0, 900, 40);
		this.toolPanel.setBackground(Color.black);
		this.toolPanel.setVisible(false);
		// ---------------------菜单条----加载结束-----------------------------------------

		addMouseListener(this);
		addMouseMotionListener(this);
	}

	/**
	 * 功能:设置画笔颜色
	 */
	public void itemStateChanged(ItemEvent e)
	{
		//画笔颜色设置
		if (e.getSource() == this.ColChoice)
		{
			String name = this.ColChoice.getSelectedItem();

			if (name == "黑色")
			{
				this.c = new Color(0, 0, 0);
			} else if (name == "红色")
			{
				this.c = new Color(255, 0, 0);
			} else if (name == "绿色")
			{
				this.c = new Color(0, 255, 0);
			} else
			{
				if (name != "蓝色")
					return;
				this.c = new Color(0, 0, 255);
			}
		}
		//画笔大小设置
		else if (e.getSource() == this.SizeChoice)
		{
			String selected = this.SizeChoice.getSelectedItem();

			if (selected == "1")
			{
				this.con = 1;
			} else if (selected == "3")
			{
				this.con = 3;
			} else if (selected == "5")
			{
				this.con = 5;
			} else if (selected == "7")
			{
				this.con = 7;
			} else if (selected == "9")
			{
				this.con = 9;
			}
			this.size = new BasicStroke(this.con, 0, 2); //设置画笔大小
		} else
		{
			//设置橡皮擦大小
			if (e.getSource() != this.EraserChoice)
				return;
			String Esize = this.EraserChoice.getSelectedItem();

			if (Esize == "5")
			{
				this.Econ = 5 * 2;
			} else if (Esize == "9")
			{
				this.Econ = 9 * 2;
			} else if (Esize == "13")
			{
				this.Econ = 13 * 2;
			} else
			{
				if (Esize != "17")
					return;
				this.Econ = 17 * 3;
			}
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		// ----------------设置右键菜单的显示与隐藏功能-----------------------------
		if (e.getSource() == this.menuItem1)
		{
			this.jp.setVisible(false);
			this.toolPanel.setVisible(true);
			repaint();
		} else if (e.getSource() == this.menuItem2)
		{
			this.jp.setVisible(true);
			this.toolPanel.setVisible(false);
			repaint();
		}
		// else if (e.getSource() == this.menuItem3)
		// {
		// //this.paintInfo.removeAllElements();
		// //repaint();
		// }
		else if (e.getSource() == this.menuItem4)
		{
			this.toolPanel.setVisible(true);
			repaint();
		} else if (e.getSource() == this.menuItem5)
		{
			this.toolPanel.setVisible(false);
			repaint();
		}

		// ----------------设置右键菜单的显示与隐藏功能--------结束---------------------

		if (e.getSource() == this.pen) // 画笔
		{
			this.toolFlag = 0;
		}

		if (e.getSource() == this.eraser) // 橡皮擦
		{
			this.toolFlag = 1;
		}

		if (e.getSource() == this.clear) // 清除
		{
			this.toolFlag = 2;
			this.paintInfo.removeAllElements();
			repaint();
		}

		if (e.getSource() == this.drLine) // 画直线
		{
			this.toolFlag = 3;
		}

		if (e.getSource() == this.drCircle) // 画圆
		{
			this.toolFlag = 4;
		}

		if (e.getSource() == this.drRect) // 画矩形
		{
			this.toolFlag = 5;
		}

		if (e.getSource() == this.colchooser) // 调色板
		{
			Color newColor = JColorChooser.showDialog(this, "调色板", this.c);
			this.c = newColor;
		}

		// -----------------------文件选择器以及打开和保存图片按钮的设置--------------------------------------------
		JFileChooser chooser = new JFileChooser("../Control/WebRoot/images"); // 在当前目录下,创建文件选择器

		JpgFileFilter jpgFilter = new JpgFileFilter(); // jpg过滤器
		GifFileFilter gifFilter = new GifFileFilter(); // gif过滤器
		chooser.addChoosableFileFilter(jpgFilter); // 加载jpg文件过滤器
		chooser.addChoosableFileFilter(gifFilter); // 加载gif文件过滤器

		chooser.setFileFilter(jpgFilter); // 设置默认的文件管理器。如果不设置,则最后添加的文件过滤器为默认过滤器

		if (e.getSource() == this.openPic) // 打开图片按钮
		{
			int returnVal = chooser.showOpenDialog(this); // 打开文件选择器窗口

			if (returnVal == JFileChooser.APPROVE_OPTION)
			{
				String filepath = chooser.getSelectedFile().getPath(); // 选择的文件路径
				String filename = chooser.getSelectedFile().getName(); // 选择的文件名

				try
				{
					this.curimg = ImageIO.read(new File(filepath));

					Canvas ca = new canvas(this.curimg);
					add(ca);
					ca.setBounds(100, 200, this.curimg.getWidth(this),
							this.curimg.getHeight(this));
					validate();
					this.curcan = null;
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
				}
			}

		}

		if (e.getSource() == this.savePic) // 保存图片按钮
		{
			chooser.setSelectedFile(new File("backPicture.jpg")); // 设置保存时的,默认文件名

			int returnVal = chooser.showSaveDialog(this); // 显示保存文件窗口

			String filepath = chooser.getSelectedFile().getPath();
			String filename = chooser.getSelectedFile().getName();

			if (returnVal == JFileChooser.APPROVE_OPTION)
			{
				if (!filepath.endsWith("jpg")) // 如果不以jpg后缀结尾,则加该后缀
				{
					filepath = filepath + ".jpg";
				}
				try
				{
					ImageIO.write(this.bi, "JPEG", new File(filepath));
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
				}
			}
		}

	}

	public void mouseClicked(MouseEvent e)
	{
		clickhere(e);
	}

	public void mouseEntered(MouseEvent e)
	{
	}

	public void mouseExited(MouseEvent e)
	{
	}

	public void update(Graphics g)
	{
		paint(g);
	}

	public void mousePressed(MouseEvent e)
	{
		Point p2;
		switch (this.toolFlag)
		{
			case 3: //直线
				this.x = e.getX();
				this.y = e.getY();
				p2 = new Point(this.x, this.y, this.c, this.toolFlag, this.con);
				this.paintInfo.addElement(p2);
				break;
			case 4: //圆
				this.x = e.getX();
				this.y = e.getY();
				p2 = new Point(this.x, this.y, this.c, this.toolFlag, this.con);
				this.paintInfo.addElement(p2);
				break;
			case 5: //矩形
				this.x = e.getX();
				this.y = e.getY();
				p2 = new Point(this.x, this.y, this.c, this.toolFlag, this.con);
				this.paintInfo.addElement(p2);
		}
	}

	public void mouseReleased(MouseEvent e)
	{
		//单击右键时,显示右键菜单
		if ((e.getButton() == 3))
		{
			this.popMenu2.show(e.getComponent(), e.getX(), e.getY());
		}

		Point p3;
		switch (this.toolFlag)
		{
			case 0: // 画笔
				this.paintInfo.addElement(this.cutflag);
				break;
			case 1: //橡皮擦
				this.paintInfo.addElement(this.cutflag);
				break;
			case 3: //直线
				this.x = e.getX();
				this.y = e.getY();
				p3 = new Point(this.x, this.y, this.c, this.toolFlag, this.con);
				this.paintInfo.addElement(p3);
				this.paintInfo.addElement(this.cutflag);
				repaint();
				break;
			case 4: //圆
				this.x = e.getX();
				this.y = e.getY();
				p3 = new Point(this.x, this.y, this.c, this.toolFlag, this.con);
				this.paintInfo.addElement(p3);
				this.paintInfo.addElement(this.cutflag);
				repaint();
				break;
			case 5: //矩形
				this.x = e.getX();
				this.y = e.getY();
				p3 = new Point(this.x, this.y, this.c, this.toolFlag, this.con);
				this.paintInfo.addElement(p3);
				this.paintInfo.addElement(this.cutflag);
				repaint();
			case 2:
		}
	}

	public void mouseDragged(MouseEvent e)
	{
		Point p1;
		switch (this.toolFlag)
		{
			case 0: //画笔
				this.x = e.getX();
				this.y = e.getY();
				p1 = new Point(this.x, this.y, this.c, this.toolFlag, this.con);
				this.paintInfo.addElement(p1);
				repaint();
				break;
			case 1: //橡皮擦
				this.x = e.getX();
				this.y = e.getY();
				p1 = new Point(this.x, this.y, null, this.toolFlag, this.Econ);
				this.paintInfo.addElement(p1);
				repaint();
		}
	}

	public void mouseMoved(MouseEvent e)
	{
		if (!(this.toolPanel.isVisible()))
			return;
		setCursor(new Cursor(1));
	}

	public void clickhere(MouseEvent e)
	{
		if (this.curcan == null)
			return;

		/**
		 * 当选中图像工具条上的图片后,然后,在绘图区域点击鼠标左键时,
		 * 往绘图区域添加动态图标,位置在鼠标点击时的坐标点
		 */
		if (e.getModifiers() == MouseEvent.BUTTON1_MASK)
		{
			Canvas ca = new canvas(curimg);
			this.add(ca);
			//ca.setBounds(e.getX(), e.getY(), curimg.getWidth(this), curimg.getHeight(this));   
			ca.setBounds(e.getX(), 800 * 2 / 3, curimg.getWidth(this), curimg
					.getHeight(this)); //鼠标点击位置画出图标 
			this.validate();

			curcan = null;
		}
	}

	public void paint(Graphics g)
	{
		//this.g2d = ((Graphics2D) g);
		this.g2d = ((Graphics2D) this.bi.getGraphics());

		this.n = this.paintInfo.size(); //点信息向量Vec   

		if (this.toolFlag == 2) //清除按钮
		{
			g.clearRect(0, 0, getSize().width, getSize().height); // 清除 
			this.g2d.clearRect(0, 0, getSize().width, getSize().height);
		}

		for (int i = 0; i < this.n - 1; ++i)
		{
			DrawImage.Point p1 = this.paintInfo.elementAt(i);
			DrawImage.Point p2 = this.paintInfo.elementAt(i + 1);
			this.size = new BasicStroke(p1.boarder, 0, 2); //画笔粗细

			this.g2d.setColor(p1.col);
			this.g2d.setStroke(this.size);

			if (p1.tool != p2.tool)
				continue;
			switch (p1.tool)
			{
				// 画笔
				case 0:
					Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
					this.g2d.draw(line1);
					break;
				// 橡皮	
				case 1:
					g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
					this.g2d.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
					break;
				// 画直线
				case 3:
					Line2D line2 = null;

					int X = Math.abs(p2.x - p1.x);
					int Y = Math.abs(p2.y - p1.y);

					if (X >= Y)
					{
						line2 = new Line2D.Double(p1.x, p1.y, p2.x, p1.y);
					} else
					{
						line2 = new Line2D.Double(p1.x, p1.y, p1.x, p2.y);
					}

					this.g2d.draw(line2);
					break;
				// 画圆
				case 4:
					Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math
							.abs(p2.x - p1.x), Math.abs(p2.y - p1.y));
					this.g2d.draw(ellipse);
					break;
				// 画矩形
				case 5:
					Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math
							.abs(p2.x - p1.x), Math.abs(p2.y - p1.y));
					this.g2d.draw(rect);
					break;
				// 截断,跳过
				case 6:
					++i;
				case 2:
			}

		}

		g.drawImage(this.bi, 0, 0, this);

	}

	Graphics2D g2 = null;

	//-----------------图像工具条------------------------
	class canvas extends Canvas
	{
		private static final long serialVersionUID = 1L;
		Image im = null;
		int X;
		int Y;
		int bufferX;
		int bufferY;
		int bufferWidth;
		int bufferHeight;

		public canvas(Image paramImage)
		{
			setCursor(new Cursor(Cursor.MOVE_CURSOR)); //设置鼠标悬浮样式,设立为移动形
			this.im = paramImage;

			//注册鼠标按下事件,用于获取需要绘制的图标对象
			addMouseListener(new MouseAdapter()
			{
				public void mousePressed(MouseEvent e)
				{
					canvas.this.clickcanvas(e);
				}

				public void mouseReleased(MouseEvent e)
				{
					Canvas mcan = (Canvas) e.getSource();

					int mtop = mcan.getY();
					int mleft = mcan.getX();
					int mwidth = mcan.getWidth();
					int mheight = mcan.getHeight();

					canvas.this.X = (mleft + e.getX() - DrawImage.this.clickX);
					canvas.this.Y = (mtop + e.getY() - DrawImage.this.clickY);

					canvas.this.bufferX = canvas.this.X;
					canvas.this.bufferY = canvas.this.Y;
					canvas.this.bufferWidth = mwidth;
					canvas.this.bufferHeight = mheight;

					canvas.this.drawBufferImage(); //在缓冲区中绘制出动态图像
				}
			});

			/**  
			 * 动态图标键盘事件  
			 */
			addKeyListener(new KeyAdapter()
			{
				public void keyPressed(KeyEvent e)
				{
					if (e.getKeyCode() == 127)
						canvas.this.removecanvas(e);
				}
			});

			/**  
			 * 动态图标鼠标移动和拖动事件  
			 */
			addMouseMotionListener(new MouseMotionAdapter()
			{
				public void mouseDragged(MouseEvent e)
				{
					canvas.this.movecanvas(e);
				}
			});
		}

		//移动图标
		public void movecanvas(MouseEvent e)
		{
			Canvas mcan = (Canvas) e.getSource();

			int mtop = mcan.getY();
			int mleft = mcan.getX();
			int mwidth = mcan.getWidth();
			int mheight = mcan.getHeight();

			clearImage(); //在内存中画出新图标之前,先清空这一区域以前画的图标

			mcan.setBounds(mleft + e.getX() - DrawImage.this.clickX, mtop
					+ e.getY() - DrawImage.this.clickY, mwidth, mheight);

			for (int i = 0; i < DrawImage.this.v.size(); ++i)
			{
				Vector v1 = DrawImage.this.v.elementAt(i);
				if (((Canvas[]) v1.elementAt(0))[0].equals(mcan))
				{
					((int[]) v1.elementAt(1))[0] = mcan.getX();
					((int[]) v1.elementAt(1))[1] = mcan.getY();
					((int[]) v1.elementAt(1))[2] = mcan.getWidth();
					((int[]) v1.elementAt(1))[3] = mcan.getHeight();
				} else
				{
					if (!(((Canvas[]) v1.elementAt(0))[1].equals(mcan)))
						continue;
					((int[]) v1.elementAt(1))[4] = mcan.getX();
					((int[]) v1.elementAt(1))[5] = mcan.getY();
					((int[]) v1.elementAt(1))[6] = mcan.getWidth();
					((int[]) v1.elementAt(1))[7] = mcan.getHeight();
				}
			}

			((DrawImage) getParent()).repaint();
		}

		public void removecanvas(KeyEvent e)
		{
			try
			{
				Canvas mcan = (Canvas) e.getSource();
				((DrawImage) getParent()).remove(DrawImage.this.curdelcan);
				for (int i = DrawImage.this.v.size() - 1; i >= 0; --i)
				{
					Vector v1 = DrawImage.this.v.elementAt(i);
					if ((!(((Canvas[]) v1.elementAt(0))[0].equals(mcan)))
							&& (!(((Canvas[]) v1.elementAt(0))[1].equals(mcan))))
						continue;
					DrawImage.this.v.removeElementAt(i);
				}
				((DrawImage) getParent()).validate();
				((DrawImage) getParent()).repaint();
			}
			catch (NullPointerException ex)
			{
				return;
			}
		}

		//在单击动态图标
		public void clickcanvas(MouseEvent e)
		{
			DrawImage.this.curdelcan = ((Canvas) e.getSource()); //设置将要删除的图标
			DrawImage.this.clickX = e.getX();
			DrawImage.this.clickY = e.getY();
			((DrawImage) getParent()).repaint();
		}

		public void paint(Graphics g)
		{
			g.drawImage(this.im, 0, 0, this);
		}

		//在缓冲区中绘制出动态图像
		public void drawBufferImage()
		{
			//Graphics2D g2 = (Graphics2D) DrawImage.this.bi.getGraphics();
			g2 = (Graphics2D) DrawImage.this.bi.getGraphics();

			g2.drawImage(this.im, this.X, this.Y, this);
			g2.dispose();
		}

		//清除内存中的该图标区域
		public void clearImage()
		{
			//Graphics2D g2 = (Graphics2D) DrawImage.this.bi.getGraphics();
			g2 = (Graphics2D) DrawImage.this.bi.getGraphics();
			g2.clearRect(this.bufferX, this.bufferY, this.bufferWidth,
					this.bufferHeight);
		}
	}

	//-----------------工具栏图标---------------------------------
	class myCanvas extends Canvas
	{
		private static final long serialVersionUID = 1L;
		Image im = null;
		String str = "";

		public myCanvas(String paramString)
		{
			this.str = paramString;
		}

		public myCanvas(Image paramImage)
		{
			setCursor(new Cursor(Cursor.HAND_CURSOR)); //设置鼠标悬浮时的样式,这里为手型
			this.im = paramImage;
			addMouseListener(new MouseAdapter()
			{
				public void mousePressed(MouseEvent e)
				{
					myCanvas.this.clickmyCanvas(e);
				}
			});
		}

		/**  
		 * 每一个工具按钮单击事件  
		 */
		public void clickmyCanvas(MouseEvent e)
		{
			DrawImage.this.curcan = ((Canvas) e.getSource()); //获取当前图标对象
			DrawImage.this.curimg = this.im; //获取当前图标对象的图象
		}

		public void paint(Graphics g)
		{
			if (this.str.equals(""))
			{
				g.drawImage(this.im, 0, 0, this);
			} else
			{
				g.drawString(this.str, 20, getHeight() / 2);
			}
		}
	}

	//-----------------工具栏图标---------结束------------------------

	class Point implements Serializable
	{
		private static final long serialVersionUID = 1L;
		int x;
		int y;
		Color col;
		int tool;
		int boarder;

		Point(int paramInt1, int paramInt2)
		{
			this.x = paramInt1;
			this.y = paramInt2;
		}

		Point(int paramInt1, int paramInt2, Color paramColor, int paramInt3,
				int paramInt4)
		{
			this.x = paramInt1;
			this.y = paramInt2;
			this.col = paramColor;
			this.tool = paramInt3;
			this.boarder = paramInt4;
		}
	}

	// -----------------文件选择器中jpg和gif文件的过滤-------------------------------------
	class JpgFileFilter extends FileFilter
	{
		public String getDescription()
		{
			return "*.jpg";
		}

		public boolean accept(File file)
		{
			String name = file.getName();
			return name.toLowerCase().endsWith(".jpg");
		}
	}

	class GifFileFilter extends FileFilter
	{
		public String getDescription()
		{
			return "*.gif";
		}

		public boolean accept(File file)
		{
			String name = file.getName();
			return name.toLowerCase().endsWith(".gif");
		}
	}
	//----------------文件选择器中jpg和gif文件的过滤---------结束-----------------------------
}

 

  • 大小: 27.8 KB
0
0
分享到:
评论
1 楼 gym308 2011-10-14  
     为什么我运行后没有工具栏呢、、、背景是漆黑一片、不能画图

相关推荐

    基于java applet的简易画图程序

    基于java applet的简易画图程序,简单易用,有助于理解java事件侦听响应的相关知识。

    Applet画图,上传

    Applet实现window画图功能,同时生成画图的图片在内存中,同时发送到服务器。

    applet 小的画图程序

    这个小程序是我自己搞的,能参数化的绘制椭圆,长方形,以及鼠标绘制多边形,有兴趣的可以研究研究,如果有什么新的思路,留言给我,以后会改进的,

    一个简单的Applet画图程序(鼠标画线)

    一个Applet程序,通过鼠标监听实现鼠标随笔画线 没多大技术含量,比较简单

    Java Applet 设计的画图程序

    Java课程设计中用Applet做的一个简单的画图程序

    有applet实现画图的操作界面

    有applet实现画图的操作界面,有话直线,椭圆,举行,和曲线,还可以调节颜色和画笔大小

    java用applet画图用到的方法(涉及双缓冲)

    主要介绍了java用applet画图用到的方法(涉及双缓冲),然后作为基础的基础的基础,必须学习如何让键盘与界面进行交互。下面就是对一个基础得不能再基础的applet程序,需要的朋友可以参考下

    applet 画图程序

    这是我写的一个简陋的画图版.里面还有很多可以改进的地方,简化版方便大家参考.

    applet中文乱码问题的解决.txt

    applet中文乱码问题的解决 applet中文乱码问题的解决 applet中文乱码问题的解决 applet中文乱码问题的解决 applet中文乱码问题的解决 applet中文乱码问题的解决 applet中文乱码问题的解决

    Java在Applet小程序中显示图片.rar

    Java在Applet小程序中显示图片,看上去就像是网页中显示的图片,不过实现的手段是Java Applet,究竟如何做到的呢?请下载这个源码一看究竟。

    在APPLET中显示图片

    让我来说说如何在_APPLET_中显示图

    3则applet程序

    2.在Applet中加入1个文本框,1个文本区,每次在文本框中输入文本,回车后将文本添加到文本区的最后一行。 3在Applet中加入2个复选框,显示标题为“学习”和“玩耍”,根据选择的情况,分别显示“玩耍”、“学习”、...

    JavaApplet实例及教程

    Java+Applet实例讲解(分为1和2两部分)、Java+Applet实例编程、java+Applet登录、JavaApplet教程.pdf、JAVA_applet应用实例.ppt、JavaApplet编程技巧实例专辑、javaapplet例子.doc、Applet类.doc等教程。...

    Applet小游戏 Applet小游戏 Applet小游戏

    Applet做的一个小游戏,多指教。Applet做的一个小游戏,多指教。

    Applet

    Applet就是使用Java语言编写的一段代码,它可以在浏览器环境中运行。 与Application的区别主要在于其执行方式的不同。 &lt;br&gt; application 是从其中的main() 方法开始运行的,而Applet 是在浏览器中运行的,必须...

    《Java数据结构与算法》中的applet

    《Java数据结构与算法》中的示例applet,很完整很全面,方便大家交流学习

    XP模式中的Applet的小程序开发

    在当今流的Ajax技术+Flash的胖客户端技术时,我们不要忘记Applet小程序是Java技术中非常重要的客户技术,它也是非常成熟的客户端技术。在实际开发中,如果能够利用恰当的Applet技术,那么可以写出维护性、重用性很高...

    Java的线程和Java AppletJava的线程和Java AppletJava的线程和Java Applet

    Java的线程和Java AppletJava的线程和Java AppletJava的线程和Java AppletJava的线程和Java AppletJava的线程和Java Applet

    在Java Applet中实现动画.rar_applet_java Applet

    在Java中实现动画有很多种办法,但它们实现的基本原理是一样的,即在屏幕上画出一系列的帧来造成运动的感觉。 我们先构造一个程序的框架,再慢慢扩展,使之功能比较齐备。

    java applet实例讲解

    java applet实例讲解给你学习applet的捷径!!

Global site tag (gtag.js) - Google Analytics