`

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  
     为什么我运行后没有工具栏呢、、、背景是漆黑一片、不能画图

相关推荐

    有applet实现画图的操作界面

    本案例中,我们探讨的是一个利用Applet来实现画图操作界面的应用。这个界面允许用户绘制直线、椭圆、矩形以及曲线,并且可以自定义颜色和画笔的大小,提供了一种丰富的图形绘制功能。 首先,我们需要了解Applet的...

    java小游戏(鼠标画图)程序

    此Java小游戏利用 `Applet` 类结合 `Graphics` 接口和事件监听器实现了简单的鼠标画图功能,通过按钮可以轻松地更改颜色以及清除图形,是一个很好的学习示例,帮助理解 Java 图形编程的基本概念和技术。

    用java 编的小应用程序Applet 实现风扇的简单功能

    Java Applet 是一种小型的、可嵌入网页中的Java程序。它可以在支持Java的Web浏览器上运行,用于增强网页的功能性和交互性。在本案例中,我们通过Java Applet 实现了一个具有简单功能的风扇模拟器,该程序允许用户...

    2015年电大JAVA语言与面向对象程序设计专科期末复习试题及答案小抄.doc

    3. Applet编程:在Java Applet程序中,需要覆盖父类的paint()方法来完成画图操作。 4. 标识符:Java中的标识符是指变量、方法、类等的名称。合法的标识符必须以字母开头,后跟字母、数字或下划线。 5. 访问控制符...

    Java面试练习题.docx

    5. Java Applet程序用户自定义的Applet子类中,一般需要重载父类的paint()方法来完成一些画图操作。 二、Java语言基础 1. 在Java中,赋值运算符的优先级高于比较运算符。 2. 在Java中,使用类名作为前缀可以调用...

    Java数据结构和算法中文第二版源码

    通过这些Applet,学习者可以更深入地了解数据如何在内存中组织以及算法如何影响其性能。 ExamplePrograms.ZIP则很可能包含了实际的Java源代码,涵盖各种经典的数据结构和算法实现。这些源码可能包括了数组、链表、...

    java入门小程序[收集].pdf

    在这个例子中,`paint`方法使用`Graphics`对象绘制文本,说明了如何使用颜色和字符串在屏幕上画图。 在基本数据类型的例子中,我们看到了`char`、`int`以及它们与Unicode的关系。`Example2_1`展示了如何将Unicode码...

    Java程序设计技术-期末复习.ppt

    11. **Applet绘图**:Applet需要重写`paint()`或`paintComponent()`方法来实现画图功能。 12. **布局管理器**:BorderLayout、FlowLayout、GridLayout和CardLayout分别是Java GUI中常用的布局管理器。- ...

    2021-2022计算机二级等级考试试题及答案No.13958.docx

    26. "画图"软件:Windows 98的附件中包含"画图"程序,用于创建、编辑和查看图片。 27. 局域网与广域网:局域网通常提供高有效性且可靠性高,相比之下,广域网可能在覆盖范围更广的同时牺牲一部分效率和可靠性。 28...

    java复习资源

    20. **重构**:在Eclipse中,可以使用重构功能来统一修改被多次引用的类或者变量的名字。 #### 二十一、Java Web开发技术 21. **Java Web开发技术**:Java开发Web程序的主要技术包括`Servlet`和`JSP`。 #### 二十...

    java基础语法测试题.docx

    2. 自定义的Applet子类通常需要覆盖 `paint()` 方法来完成画图操作。 3. 合法的逻辑表达式是 `9&gt;6&&8。 4. Java 语言由 Sun 公司于1995年发布。 5. 合法的变量定义语句是 `double a=Double.MAX_VALUE`。 6. 被 `...

    java编程100例

    实例31 用Applet显示图片 实例32 图片火焰效果 实例33 图片百叶窗 实例34 图片倒影 实例35 图片翻折 实例36 闹钟 实例37 万年历 实例38 计算器 实例39 电子相册 实例40 声音播放程序 实例41 视频播放程序 实例42 半...

    2021-2022计算机二级等级考试试题及答案No.91490.docx

    Java是一种服务器端或客户端运行的语言,但在Web浏览器中通常使用JavaScript或VBScript来编写客户端脚本,而Java主要用于开发服务器端应用程序或通过Applet在客户端运行。 **正确答案:** C. Java ### 11. Python...

    Java典型模块

    5.2.4 修改后的妈妈类 5.3 知识点扩展——线程的状态 5.3.1 线程的创建状态 5.3.2 线程的暂停状态 5.3.3 线程的结束状态 5.4 小结 第6章 火车站售票系统(线程安全知识) 6.1 火车站售票系统原理 6.1.1 项目结构框架...

    2021-2022计算机二级等级考试试题及答案No.4229.docx

    根据给定文件的信息,我们可以总结出...- **事件处理:** Java Applet中处理事件通常需要导入`java.awt.event`包。 **答案解析:** 正确的答案是**import java.awt.event**。这通常是为了导入处理事件所需的类和接口。

Global site tag (gtag.js) - Google Analytics