⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paintbrush.java

📁 一个简单的画图板JAVA程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			 * tempy);
			 * 
			 * px = tempx + 1; py = tempy; // currentChoice = 6;
			 * 
			 * //drawArea.getGraphics().drawLine(px,py,px,py); g2d.drawLine(px,
			 * py, px, py);
			 * 
			 * stack.push(ur_point); } if
			 * (color.equals(drawArea.getPixleColor(tempx - 1, tempy))) {
			 * System.out.print("d"); Point dr_point = new Point(tempx - 1,
			 * tempy);
			 * 
			 * px = tempx - 1; py = tempy;
			 * 
			 * 
			 * g2d.drawLine(px, py, px, py);
			 * 
			 * stack.push(dr_point); } }
			 */
		}
	}

	/**
	 * @author 王宇新,@author 王德恩
	 * @version 1.0 07-6-14
	 * @see Character 功能:定义铅笔类实现铅笔画功能 方法:先将文字写到对话框中,然后调用drawString函数将文字写到指定的位置
	 */
	class Character extends Drawings

	{
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setFont(new Font(fonts, x2 + y2, (int) stroke));
			if (string != null)
				g2d.drawString(string, x1, y1);
		}
	}

	/**
	 * @author 赵磊, 颜钟敏
	 * @version 1.0 07-6-14
	 * @see Line 功能:定义直线类实现画直线功能
	 */
	class Line extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
					BasicStroke.JOIN_BEVEL));
			g2d.drawLine(x1, y1, x2, y2);
		}
	}

	/**
	 * @author 赵磊, 颜钟敏
	 * @version 1.0 07-6-14
	 * @see Rectangle 功能:定义普通矩形类实现画普通矩形功能
	 */
	class Rectangle extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
		}
	}

	/**
	 * @author 赵磊, 颜钟敏
	 * @version 1.0 07-6-14
	 * @see EmptyRectangle 功能:定义虚体矩形类实现画虚体矩形功能<br>
	 *      方法:写画一个空心(白色)的矩形,然后在同一地方画一个普通的矩形<br>
	 *      以下画虚体图形方法相同
	 */
	class EmptyRectangle extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(255, 255, 255));
			g2d.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
		}
	}

	/**
	 * @author 赵磊, 颜钟敏,胡如兴
	 * @version 1.0 07-6-14
	 * @see SolidRectangle 功能:定义实体矩形类实现画实体矩形功能<br>
	 *      方法:调用 fill 函数来填充矩形<br>
	 *      以下画实体图形方法相同
	 */
	class SolidRectangle extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
		}
	}

	/**
	 * @author 周新飞
	 * @version 1.0 07-6-14
	 * @see Polygon 功能:定义实体矩形类实现画实体矩形功能<br>
	 *      方法:
	 * 
	 */
	class Polygon extends Drawings {

		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));

			g2d.drawPolygon(polygon);

			/*
			 * if (isfirstline==false&&Math.abs(x2 - px) < 10 && Math.abs(y2 -
			 * py)< 10 ) { isfirstline=true; g2d.drawLine(x2,y2,px,py); }else
			 * g2d.drawLine(x1,y1,x2,y2);
			 */
		}
	}

	/**
	 * @author 周新飞
	 * @version 1.0 07-6-14
	 * @see EmptyPolygon 功能:定义虚体多边形类实现画虚体多边形功能
	 */
	class EmptyPolygon extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(255, 255, 255));
			g2d.setStroke(new BasicStroke(stroke));

			g2d.fillPolygon(polygon);
			g2d.setPaint(new Color(R, G, B));
			g2d.drawPolygon(polygon);

			/*
			 * if (isfirstline==false&&Math.abs(x2 - px) < 10 && Math.abs(y2 -
			 * py)< 10 ) { isfirstline=true; generalPath.closePath();
			 * g2d.fill(generalPath); }else g2d.drawLine(x1,y1,x2,y2);
			 */
		}
	}

	/**
	 * @author 周新飞
	 * @version 1.0 07-6-18
	 * @see SolidPolygon 功能:定义实体多边形类实现画实体多边形功能
	 */
	class SolidPolygon extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.fillPolygon(polygon);

			/*
			 * if (isfirstline==false&&Math.abs(x2 - px) < 10 && Math.abs(y2 -
			 * py)< 10 ) { isfirstline=true; generalPath.closePath();
			 * g2d.fill(generalPath); }else g2d.drawLine(x1,y1,x2,y2);
			 */
		}
	}

	/**
	 * @author 王宇新, 王德恩,胡如兴
	 * @version 1.0 07-6-14
	 * @see Ellipse 功能:定义椭圆类实现画椭圆功能
	 */
	class Ellipse extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
		}
	}

	/**
	 * @author 王宇新, 王德恩
	 * @version 1.0 07-6-14
	 * @see EmptyEllipse 功能:定义虚体椭圆类实现画虚体椭圆功能
	 */
	class EmptyEllipse extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(255, 255, 255));
			g2d.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
		}
	}

	/**
	 * @author 王宇新, 王德恩
	 * @version 1.0 07-6-14
	 * @see SolidEllipse 功能:定义实体椭圆类实现画实体椭圆功能
	 */
	class SolidEllipse extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
					Math.abs(y1 - y2));
			repaint();
		}
	}

	/**
	 * @author 王宇新, 王德恩
	 * @version 1.0 07-6-14
	 * @see Roundrectangle 功能:定义普通圆角矩形类实现画普通圆角矩形功能
	 */
	class Roundrectangle extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1
					- x2), Math.abs(y1 - y2), 50, 35);
		}
	}

	/**
	 * @author 王宇新,王德恩
	 * @version 1.0 07-6-14
	 * @see EmptyRoundrectangle 功能:定义虚体圆角矩形类实现画虚体圆角矩形功能
	 */
	class EmptyRoundrectangle extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(255, 255, 255));
			g2d.fillRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1
					- x2), Math.abs(y1 - y2), 50, 35);
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1
					- x2), Math.abs(y1 - y2), 50, 35);
		}
	}

	/**
	 * @author 王宇新, 王德恩
	 * @version 1.0 07-6-14
	 * @see SolidRoundrectangle 功能:定义实体圆角矩形类实现画实体圆角矩形功能
	 */
	class SolidRoundrectangle extends Drawings {
		void draw(Graphics2D g2d) {
			g2d.setPaint(new Color(R, G, B));
			g2d.setStroke(new BasicStroke(stroke));
			g2d.fillRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1
					- x2), Math.abs(y1 - y2), 50, 35);
		}
	}

	/**
	 * 功能:将画笔传入到各个子类中,用来完成各自的绘图
	 * 
	 * @param g2d
	 * @param a
	 */
	public void draw(Graphics2D g2d, Drawings a) {
		a.draw(g2d); // 将画笔传入到各个子类中,用来完成各自的绘图
	}

	/**
	 * 无参数无返回值<br>
	 * 功能:设置颜色
	 */
	public void chooseColor() {// 设置颜色
		color = JColorChooser.showDialog(this, "Choose a color", color);
		R = color.getRed();
		G = color.getGreen();
		B = color.getBlue();
		itemList[index].R = R;
		itemList[index].G = G;
		itemList[index].B = B;
	}

	/**
	 * 无参数无返回值<br>
	 * 功能:设置画笔笔触宽度
	 */
	public void setStroke() {// 设置画笔笔触
		itemList[index].stroke = stroke1;
	}

	/**
	 * @author 周新飞
	 * @version 1.0 07-6-16
	 * @see Point 功能:定义一个点类 作用: 在喷枪功能里用到点的压栈,出栈
	 */
	public class Point {
		private int x;

		private int y;

		public Point() {
			// implicit call to Object constructor occurs here
		}

		public Point(int xValue, int yValue) {
			x = xValue;
			y = yValue;
		}

		public void setX(int xValue) {
			x = xValue;
		}

		public int getX() {
			return x;
		}

		public void setY(int yValue) {
			y = yValue;
		}

		public int getY() {
			return y;
		}

		public String toString() {
			return "(" + getX() + "," + getY() + ")";
		}
	}

	/**
	 * @author 赵磊, 颜钟敏,胡如兴
	 * @version 1.0 07-6-14
	 * @see JPGFilter 功能:'jpg'文件过滤器
	 */
	public class JPGFilter extends FileFilter {// 'jpg'各市文件过滤器
		public JPGFilter() {
		}

		/**
		 * 功能:文件过滤
		 * 
		 * @param fileName
		 * @return boolean
		 */
		public boolean accept(File fileName) {
			boolean acc = fileName.isDirectory();
			if (!acc) {
				String suffix = getSuffix(fileName);
				if (suffix != null)
					acc = suffix.equals("jpg");
			}
			return acc;
		}

		/**
		 * 功能:得到文件类型
		 * 
		 * @return 文件格式
		 */
		public String getDescription() {
			return "JPG Files (*jpg)";
		}

		/**
		 * 功能:得到文件后缀名
		 * 
		 * @param fileName
		 * @return 文件后缀名
		 */
		public String getSuffix(File fileName) {
			String s = fileName.getPath();
			String suffix = null;
			int i = s.lastIndexOf('.');
			suffix = s.substring(i + 1).toLowerCase();
			return suffix;
		}

		public void addSuffix(File fileName) {
			fileName.getName().concat(".jpg");
		}
	}

	/**
	 * @author 赵磊, 颜钟敏,胡如兴
	 * @version 1.0 07-6-14
	 * @see GIFFilter 功能:'gif'文件过滤器
	 */
	public class GIFFilter extends FileFilter {// 'gif'各市文件过滤器
		public GIFFilter() {
		}

		/**
		 * 功能:文件过滤
		 * 
		 * @param fileName
		 * @return boolean
		 */
		public boolean accept(File fileName) {
			boolean acc = fileName.isDirectory();
			if (!acc) {
				String suffix = getSuffix(fileName);
				if (suffix != null)
					acc = suffix.equals("gif");
			}
			return acc;
		}

		/**
		 * 功能:得到文件类型
		 * 
		 * @return 文件格式
		 */
		public String getDescription() {
			return "GIF Files (*gif)";
		}

		/**
		 * 功能:得到文件后缀名
		 * 
		 * @param fileName
		 * @return 文件后缀名
		 */
		public String getSuffix(File fileName) {
			String s = fileName.getPath();
			String suffix = null;
			int i = s.lastIndexOf('.');
			suffix = s.substring(i + 1).toLowerCase();
			return suffix;
		}
	}

	/**
	 * @author 赵磊, 颜钟敏,胡如兴
	 * @version 1.0 07-6-14
	 * @see PNGFilter 功能:'png'文件过滤器
	 */
	public class PNGFilter extends FileFilter {// 'png'各市文件过滤器
		public PNGFilter() {
		}

		/**
		 * 功能:文件过滤
		 * 
		 * @param fileName
		 * @return bollean
		 */
		public boolean accept(File fileName) {
			boolean acc = fileName.isDirectory();
			if (!acc) {
				String suffix = getSuffix(fileName);
				if (suffix != null)
					acc = suffix.equals("png");
			}
			return acc;
		}

		/**
		 * 功能:得到文件类型
		 * 
		 * @return 文件格式
		 */
		public String getDescription() {
			return "PNG Files (*png)";
		}

		/**
		 * 功能:得到文件后缀名
		 * 
		 * @param fileName
		 * @return 文件后缀名
		 */
		public String getSuffix(File fileName) {
			String s = fileName.getPath();
			String suffix = null;
			int i = s.lastIndexOf('.');
			suffix = s.substring(i + 1).toLowerCase();
			return suffix;
		}
	}

	public static void main(String args[]) {//主函数	
		/*	try {
		 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
		 }
		 catch ( Exception e ) {}//将界面设置为当前windows风格
		 */
		PaintBrush application = new PaintBrush();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}//end class PaintBrushTest

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -