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

📄 paintpanel.java

📁 用Java部分实现了电子导航仪的功能
💻 JAVA
字号:
package shiyan3;




import java.awt.*;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

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

/**
 * 
 * @author jackxy
 */
public class PaintPanel extends javax.swing.JPanel implements MouseListener,
		ActionListener, MouseMotionListener, WindowListener {

	JFrame main;

	// private int isMOUSEMotion=0;//判断是否是由鼠标引起重绘
	protected final int EDIT_MODE = 1;
	protected final int SERVE_MODE = 2;
	protected final int PAINT_POINT = 3;
	protected final int PAINT_LINE = 4;
	protected final int DELETE_POINT = 5;
	protected final int MOUSE_PRESS = 10;
	protected final int MOUSE_RELEASE = 11;
	protected final int MOUSE_MOVE = 12;
	private int mode = 1;
	private int methed = 4;
	private int mouse;
	private int selected;
	private ArrayList<Point> dots;
	private ArrayList<ArrayList<Integer>> lines;
	private Map map;

	private Image mapImage;
	int dot1 = -1;
	int dot2 = -1;//

	private Map makeMap() {
		Map m = new Map();
		for (int i = 0; i < dots.size(); i++) {
			m.addDot();
		}

		int x1 = 0;
		int x2 = 0;
		int y1 = 0;
		int y2 = 0;
		for (int i = 0; i < lines.size(); i++) {
			if(lines.get(i)!=null&&lines.get(i).size()==2){
			int dot1 = lines.get(i).get(0);
			int dot2 = lines.get(i).get(1);
			x1 = dots.get(dot1).x;
			x2 = dots.get(dot2).x;
			y1 = dots.get(dot1).y;
			y2 = dots.get(dot2).y;
			m.addLine(lines.get(i).get(0), lines.get(i).get(1), (int) Math.pow(
					(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 

0.5));
		}
			else{
				lines.remove(i);
			}
			}
		return m;

	}

	public int getLengthOfLine(ArrayList<Point> line) {
		double length = 0;
		for (int i = 0; i < line.size() - 1; i++) {
			length = length
					+ Math
							.pow(
									((line.get(i).getX() 

- line.get(i + 1)
											

.getX())
											* 

(line.get(i).getX() - line.get(
												

	i + 1).getX()) + (line.get(
											

i).getY() - line.get(i + 1).getY())
											* 

(line.get(i).getY() - line.get(
												

	i + 1).getY())), 0.5);
		}
		return (int) length;
	}

	/** Creates new form PaintPanel */
	public PaintPanel(JFrame frame) {
		main = frame;
		initComponents();

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	private void initComponents() {
		read();
		setBackground(new java.awt.Color(255, 255, 255));
		setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 255)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 398,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 298,
				Short.MAX_VALUE));
		this.addMouseListener(this);

		mapImage = Toolkit.getDefaultToolkit().getImage("map/map.jpg");

	}// </editor-fold>

	void read() {
		try {
			FileInputStream fin = new FileInputStream(new File(
					"map/pointPosition.jac"));
			ObjectInputStream oinput = new ObjectInputStream(fin);
			// this.dots=(ArrayList<Dot>)oinput.readObject();
			this.dots = (ArrayList<Point>) oinput.readObject();
			oinput.close();
			fin = new FileInputStream(new File("map/lines.jac"));
			oinput = new ObjectInputStream(fin);
			lines = (ArrayList<ArrayList<Integer>>) oinput.readObject();
			oinput.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not Found");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	void write() {
		System.out.println("write done");
		try {
			File f = new File("map/pointPosition.jac");

			FileOutputStream fou = new FileOutputStream(f);
			ObjectOutputStream ooutput = new ObjectOutputStream(fou);

			ooutput.writeObject(dots);
			ooutput.close();
			f = new File("map/lines.jac");
			fou = new FileOutputStream(f);
			ooutput = new ObjectOutputStream(fou);
			ooutput.writeObject(lines);
			ooutput.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	void paintDot(Graphics g) {

		Point p = getMousePosition();
		g.setColor(Color.blue);
		if (isExist(p) == -1) {
			g.fillOval(p.x - 8, p.y - 8, 16, 16);
			if (dots == null) {
				dots = new ArrayList<Point>();
			}

			dots.add(new Point(p.x, p.y));
		}

	}

	void paintLinesStage1(Graphics g) {
		g.setColor(Color.green);
		Point p = getMousePosition();
		ArrayList<Integer> line1;
		if (lines == null) {
			lines = new ArrayList<ArrayList<Integer>>();
			line1 = new ArrayList<Integer>();
			lines.add(line1);
		} else {
			line1 = new ArrayList<Integer>();
			lines.add(line1);
		}
		if (isExist(p) != -1) {
			Point i = dots.get(isExist(p));
			p.x = i.x;
			p.y = i.y;
		} else {
			paintDot(g);
		}
		int a = -1;
		for (int i = 0; i < dots.size(); i++) {
			if (p.x == dots.get(i).x && p.y == dots.get(i).y) {
				a = i;
			}
		}
		line1.add(a);
		//
		//
		//
		//
		// System.out.println(lines.size());
	}

	void paintLineStage2(Graphics g) {
		Point p = getMousePosition();
		ArrayList<Integer> line1 = lines.get(lines.size() - 1);
		if (isExist(p) != -1) {// 存在点的情况
			Point i = dots.get(isExist(p));
			p.x = i.x;
			p.y = i.y;
			if (p.x != dots.get(line1.get(0)).x
					|| p.y != dots.get(line1.get(0)).y) {// 不是划线起点

//的情况

				line1.add(isExist(p));
				g.drawLine(p.x, p.y, dots.get(line1.get(0)).x, dots.get

(line1
						.get(0)).y);
			} else {
				lines.remove(lines.get(lines.size() - 1));
			}
		} else {// 不存在点的情况
			line1.add(dots.size());
			g.drawLine(p.x, p.y, dots.get(line1.get(0)).x, dots.get(line1
					.get(0)).y);
			paintDot(g);
		}
	}

	void deleteP(Graphics g) {
		Point p = getMousePosition();
		int index = isExist(p);
		if (index != -1) {
			dots.remove(index);
			for (int i = 0; i < lines.size(); i++) {
				if(lines.get(i)!=null&&lines.get(i).size()==2){
				if (lines.get(i).get(0) == index
						|| lines.get(i).get(1) == index) {
					lines.remove(i);
				}
				if (lines.get(i).get(0) > index) {
					int a = lines.get(i).get(0);
					lines.get(i).set(0, a - 1);
				}
				if (lines.get(i).get(1) > index) {
					int a = lines.get(i).get(1);
					lines.get(i).set(1, a - 1);
				}
			}}

		}
	}

	int isExist(Point p) {
		int a = -1;
		if (dots != null) {
			for (int i = 0; i < dots.size(); i++) {

				if ((p.x - dots.get(i).x) * (p.x - dots.get(i).x)
						+ (p.y - dots.get(i).y) * (p.y - dots.get

(i).y) < 64) {
					a = i;
					break;
				}
			}
		}
		return a;
	}

	private int findNearestPoint(int x, int y) {

		int index = -1;
		int l = Integer.MAX_VALUE;
		for (int i = 0; i < dots.size(); i++) {
			int v2 = (int) Math.pow((x - dots.get(i).x) * (x - dots.get(i).x)
					+ (y - dots.get(i).y) * (y - dots.get(i).y), 0.5);
			System.out.println(v2);
			if (v2 < l) {
				l = v2;
				index = i;
			}
		}
		return index;
	}

	public void paintpath(Graphics g, ArrayList<Integer> l) {
		Graphics2D g2d = (Graphics2D) g;
		if (l != null && l.size() > 1)
			g2d.setColor(Color.red);

		g2d.setStroke(new BasicStroke(5));

		for (int i = 1; i < l.size(); i++) {
			g2d.drawLine(dots.get(l.get(i - 1)).x, dots.get(l.get(i - 1)).y,
					dots.get(l.get(i)).x, dots.get(l.get(i)).y);
		}
	}

	public void paint(Graphics g) {
		// 画出已经有的线条和点
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;
		if (mapImage != null) {
			g2d.drawImage(mapImage, 0, 0, this.getWidth(), this.getHeight(),
					this);
		}

		if (dots != null && mode == EDIT_MODE) {
			g2d.setColor(Color.blue);
			for (int i = 0; i < dots.size(); i++) {
				g2d.fillOval(dots.get(i).x - 5, dots.get(i).y - 5, 10, 10);
			}
		}
		if (lines != null && mode == EDIT_MODE) {
			g2d.setColor(Color.green);
			for (int i = 0; i < lines.size(); i++) {
				if (lines.get(i).size() >= 2) {
					for (int j = 1; j < lines.get(i).size(); j++) {
						g2d.drawLine(dots.get(lines.get(i).get(j - 

1)).x, dots
								.get(lines.get(i).get(j - 

1)).y, dots.get(lines
								.get(i).get(j)).x, dots
								.get(lines.get(i).get

(j)).y);
					}
				} else {
					// delete line;
				}
			}
		}
		if (mouse != 0) {
			switch (mode) {
			case SERVE_MODE:
				// //serve mode

				Point p = getMousePosition();
				if (selected == 0 && mouse == MOUSE_PRESS) {
					dot1 = findNearestPoint(p.x, p.y);
					selected = dot1;
				} else if (mouse == MOUSE_RELEASE && selected != 0) {

					dot2 = findNearestPoint(p.x, p.y);

					paintpath(g, map.getPath(dot1, dot2));
					selected = 0;

				}

				break;
			case EDIT_MODE:// /edit mode
				switch (methed) {
				case PAINT_POINT:
					paintDot(g);
					break;

				case PAINT_LINE:
					switch (mouse) {
					case MOUSE_PRESS:
						paintLinesStage1(g);
						break;
					case MOUSE_RELEASE:
						paintLineStage2(g);
						break;

					default:
						break;
					}
					break;
				case DELETE_POINT:
					if (mouse == MOUSE_PRESS) {
						deleteP(g);
					}
					break;
				default:
					break;
				}

				break;

			default:
				break;
			}
			mouse = 0;
		}

	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		mouse = MOUSE_PRESS;
		repaint();
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		mouse = MOUSE_RELEASE;
		repaint();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equalsIgnoreCase("edit mode")) {
			mode = EDIT_MODE;
		} else if (e.getActionCommand().equalsIgnoreCase("serve mode")) {
			mode = SERVE_MODE;
			map = makeMap();

		} else if (e.getActionCommand().equalsIgnoreCase("add")) {
			methed = PAINT_LINE;
		} else if (e.getActionCommand().equalsIgnoreCase("remove")) {
			methed = DELETE_POINT;
		}
		repaint();
	}

	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		write();
		System.exit(0);
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}
}

⌨️ 快捷键说明

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