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

📄 paintpanel.java

📁 用java编写的画图面板程序(有源代码)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		File fileName;
		JFileChooser jfc = new JFileChooser();
		jfc.setDialogTitle("保存文件");
		jfc.setDialogType(JFileChooser.SAVE_DIALOG);
		jfc.setFileFilter(new myFileFilter2());
		jfc.showSaveDialog(instance.f);
		File fileName1 = jfc.getSelectedFile();
		if (fileName1 == null)
			return;
		String s = fileName1.getName();
		fileName = new File(fileName1.getAbsolutePath() + ".jpg");
		BufferedImage image = new BufferedImage(jcomponent.getWidth(),
				jcomponent.getHeight(), BufferedImage.TYPE_3BYTE_BGR);

		Graphics g = image.getGraphics();
		jcomponent.paintAll(g);
		RenderedImage rendImage = image;
		try {
			ImageIO.write(rendImage, "jpg", fileName);
		} catch (IOException e) {
		}
	}

	public void saveFile(boolean showDialog) {
		if (fileName == null || showDialog) {
			JFileChooser jfc = new JFileChooser();
			jfc.setDialogTitle("保存文件");
			jfc.setDialogType(JFileChooser.SAVE_DIALOG);
			jfc.setFileFilter(new myFileFilter());
			jfc.showSaveDialog(instance.f);
			File fileName1 = jfc.getSelectedFile();
			if (fileName1 == null)
				return;
			String s = fileName1.getName();
			if (s.indexOf(".") < 0) {
				fileName = new File(fileName1.getAbsolutePath() + ".paint");
			}
		}

		fileName.canWrite();

		if (fileName == null || "".equals(fileName.getName()))
			instance.setState("无效文件名");
		else {
			try {
				fileName.delete();
				FileOutputStream fos = new FileOutputStream(fileName);
				ObjectOutputStream output = new ObjectOutputStream(fos);
				output.writeInt(width); // 画板宽
				output.writeInt(height); // 画板高
				output.writeInt(graphics.size() - 1);
				output.writeObject(backColor);
				// writeObject(backColor);
				// System.out.println
				// (String.valueOf(width)+" * "+String.valueOf(height));
				// System.out.println (graphics.size()-1);

				for (int i = 0; i < graphics.size(); i++) {
					BaseObject p = (BaseObject) (graphics.get(i));
					// System.out.println (p.toString());
					G2dOperator.writeObject(output, p);
					output.flush(); // 清除缓存,完全写入文件
				}
				output.close();
				fos.close();

				instance.f.setTitle(fileName.getName() + " - " + Project.TITLE);
			} catch (IOException ioe) {
				instance.setState(ioe.toString());
			}
		}
	}

	/**
	 * 打開文件
	 * 
	 * @param strFile
	 *            当 <code>strFile</code> 为 <font color=blue><code>null</code>
	 *            </font> 时显示打開文件对话框, 否则直接打開 <code>strFile</code> 指定文件
	 */
	public void openFile(String strFile) {
		if (strFile == null || "".equals(strFile)) {
			JFileChooser jfc = new JFileChooser();
			jfc.setDialogTitle("打开文件");
			jfc.setDialogType(JFileChooser.OPEN_DIALOG);
			jfc.setFileFilter(new myFileFilter());
			jfc.showOpenDialog(instance.f);
			File fileName1 = jfc.getSelectedFile();
			if (fileName1 == null)
				return;
			fileName = fileName1;
		} else {
			try {
				File fileName1 = new File(strFile);
				fileName = fileName1;
			} catch (Exception e) {
				instance.setState(e.toString());
				return;
			}
		}
		fileName.canRead();
		if (fileName == null || "".equals(fileName.getName()))
			instance.setState("无效文件名");
		else {
			try {
				FileInputStream fis = new FileInputStream(fileName);
				ObjectInputStream input = new ObjectInputStream(fis);
				initParam();
				width = input.readInt(); // 获取宽高
				height = input.readInt();
				int counts = input.readInt();
				backColor = (Color) input.readObject();
				// backColor=(Color)input.readObject();
				// System.out.println
				// (String.valueOf(width)+" * "+String.valueOf(height));
				// System.out.println (counts);

				for (int i = 0; i <= counts; i++) {
					BaseObject gh = G2dOperator.readObject(input);
					graphics.add(gh);
					// System.out.println (graphics.get(i).toString());
				}
				input.close();
				c.setSize(width, height);
				c.setBackground(backColor);
				repaint();

				instance.f.setTitle(fileName.getName() + " - " + Project.TITLE);
			} catch (EOFException eofException) {
				instance.setState("文件结束, 不能创建文件!");
				System.out.println(eofException.toString());
			} catch (ClassNotFoundException classNotFoundException) {
				instance.setState("readObject时错误: "
						+ classNotFoundException.toString());
				// System.out.println
				// ("readObject时错误: "+classNotFoundException.toString());
			} catch (IOException ioException) {
				instance.setState("读文件的时候错误!");
				System.out.println(ioException.getMessage());
			}
		}
	}
}

class myFileFilter extends javax.swing.filechooser.FileFilter {
	/**
	 * 通过这个过滤器过滤可接受类型
	 * 
	 * @Param file - 待判断的文件
	 */
	public boolean accept(File file) {
		if (file.isDirectory()) { // 是目录的话接受
			return true;
		}
		String fileName = file.getName();
		int periodIndex = fileName.lastIndexOf('.');

		boolean accepted = false;

		if (periodIndex > 0 && periodIndex < fileName.length() - 1) {
			String extension = fileName.substring(periodIndex + 1)
					.toLowerCase();
			if ("paint".equals(extension)) // 判断扩展名是不是 ".tsp"
				accepted = true;
		}

		return accepted;
	}

	/**
	 * 过滤器的描述
	 * 
	 * @return 返回过滤类型描述
	 */
	public String getDescription() {
		return "画板文件 (*.paint)";
	}
}

class myFileFilter2 extends javax.swing.filechooser.FileFilter {
	/**
	 * 通过这个过滤器过滤可接受类型
	 * 
	 * @Param file - 待判断的文件
	 */
	public boolean accept(File file) {
		if (file.isDirectory()) { // 是目录的话接受
			return true;
		}
		String fileName = file.getName();
		int periodIndex = fileName.lastIndexOf('.');

		boolean accepted = false;

		if (periodIndex > 0 && periodIndex < fileName.length() - 1) {
			String extension = fileName.substring(periodIndex + 1)
					.toLowerCase();
			if ("jpg".equals(extension)) // 判断扩展名是不是 ".tsp"
				accepted = true;
		}

		return accepted;
	}

	/**
	 * 过滤器的描述
	 * 
	 * @return 返回过滤类型描述
	 */
	public String getDescription() {
		return "图象文件 (*.jpg)";
	}
}

class PopMenu extends JPopupMenu {
	JMenuItem mnu_Delete, mnu_TopMost, mnu_Above, mnu_Below, mnu_Bottom;
	private PaintPanel ptPanel;

	PopMenu(Object o) {
		super();
		if (o instanceof PaintPanel) {
			ptPanel = (PaintPanel) o;
		} else {
			return;
		}
		mnu_Delete = new JMenuItem("删除(D)");
		mnu_Delete.setMnemonic('D');
		mnu_Delete.setActionCommand("Delete");
		mnu_TopMost = new JMenuItem("置于顶层(T)");
		mnu_TopMost.setMnemonic('T');
		mnu_TopMost.setActionCommand("TopMost");
		mnu_Above = new JMenuItem("上移一层(A)");
		mnu_Above.setMnemonic('A');
		mnu_Above.setActionCommand("Above");
		mnu_Below = new JMenuItem("下移一层(B)");
		mnu_Below.setMnemonic('B');
		mnu_Below.setActionCommand("Below");
		mnu_Bottom = new JMenuItem("置于底层(M)");
		mnu_Bottom.setMnemonic('M');
		mnu_Bottom.setActionCommand("Bottom");
		add(mnu_Delete);
		add(mnu_TopMost);
		add(mnu_Above);
		add(mnu_Below);
		add(mnu_Bottom);
		Component[] comp = getComponents();
		for (int i = 0; i < comp.length; i++) {
			if (comp[i] instanceof JMenuItem) {
				JMenuItem mnu = (JMenuItem) comp[i];
				if (mnu != null)
					mnu.addActionListener(new PopMenuHandler(ptPanel));
			}
		}

	}

}

/**
 * 弹出菜单事件处理类
 */
class PopMenuHandler implements ActionListener {
	private PaintPanel ptPanel;

	public PopMenuHandler(Object o) {
		if (o instanceof PaintPanel) {
			ptPanel = (PaintPanel) o;
		} else {
			return;
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (ptPanel.nowGraphics < 0)
			return;
		String s = e.getActionCommand();
		int counts = ptPanel.graphics.size() - 1;
		int nowGraphics = ptPanel.nowGraphics;
		LinkedList<BaseObject> graphics = ptPanel.graphics;
		BaseObject gh = (BaseObject) (graphics.get(nowGraphics));
		if ("Delete".equals(s)) {
			ptPanel.instance.toolPanel.deleteObject();
		} else if ("TopMost".equals(s)) {
			graphics.remove(nowGraphics);
			graphics.add(gh);
			ptPanel.nowGraphics = counts;
		} else if ("Above".equals(s)) {
			if (nowGraphics == counts)
				return;
			graphics.set(nowGraphics, graphics.get(nowGraphics + 1));
			graphics.set(nowGraphics + 1, gh);
			ptPanel.nowGraphics++;
		} else if ("Below".equals(s)) {
			if (nowGraphics == 0)
				return;
			graphics.set(nowGraphics, graphics.get(nowGraphics - 1));
			graphics.set(nowGraphics - 1, gh);
			ptPanel.nowGraphics--;
		} else if ("Bottom".equals(s)) {
			graphics.remove(nowGraphics);
			graphics.addFirst(gh);
			ptPanel.nowGraphics = 0;
		}
		ptPanel.repaint();
	}
}

class G2dOperator {
	public static BaseObject readObject(ObjectInputStream in)
			throws IOException, ClassNotFoundException {
		int type = in.readInt();
		BaseObject gh;
		switch (type) {
		case BaseObject.LINE:
			gh = new ObjLine();
			break;
		case BaseObject.RECTANGLE:
			gh = new ObjRectangle();
			break;
		case BaseObject.ROUNDRECT:
			gh = new ObjRoundRect();
			break;
		case BaseObject.OVAL:
			gh = new ObjOval();
			break;
		case BaseObject.PENCIL:
			gh = new ObjPolyline();
			break;
		case BaseObject.POLYGON:
			gh = new ObjPolygon();
			break;
		default:
			gh = new BaseObject();
			break;
		}
		gh.stroke = in.readFloat();
		gh.color = (Color) in.readObject();
		gh.color2 = (Color) in.readObject();
		gh.gradientPaint = in.readInt();
		gh.p1 = (Point) in.readObject();
		gh.p2 = (Point) in.readObject();
		gh.filled = in.readBoolean();
		gh.isSquare = in.readBoolean();

		gh.X = (LinkedList<Integer>) in.readObject();
		gh.Y = (LinkedList<Integer>) in.readObject();

		if (gh.gradientPaint > 0)
			gh.setGradientPaint(gh.p1, gh.p2);
		return gh;
	}

	public static void writeObject(ObjectOutputStream out, BaseObject gh)
			throws IOException {
		out.writeInt(gh.type);
		out.writeFloat(gh.stroke);
		out.writeObject(gh.color);
		out.writeObject(gh.color2);
		out.writeInt(gh.gradientPaint);
		out.writeObject(gh.p1);
		out.writeObject(gh.p2);
		out.writeBoolean(gh.filled);
		out.writeBoolean(gh.isSquare);
		out.writeObject(gh.X);
		out.writeObject(gh.Y);
	}

}

⌨️ 快捷键说明

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