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

📄 ui.java

📁 深度优先
💻 JAVA
字号:
package hartech.kids.grapher;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import hartech.ds.Queue;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Date: 2006-09-07
 * </p>
 */
public class UI extends JPanel {

	private static final long serialVersionUID = -9151033854365106916L;

	static DrawPanel drawPanel;

	static JPanel jPanel_control, jPanel_DFS, jPanel_BFS, jPanel_tree,
			jPanel_shortest, jPanel_setting;

	static JComboBox jComboBox_DFS_begin, jComboBox_BFS_begin,
			jComboBox_shortest_begin, jComboBox_shortest_end;

	static JCheckBox jCheckBox_show_route, jCheckBox_show_number;

	static JTextField jTextField_DFS, jTextField_BFS, jTextField_tree,
			jTextField_short, jTextField_route;

	static Integer[] intArray;

	// kind 影响线、序号的偏移量
	static final int TREE = -2, DFS = 1, BFS = -1, SHORT = 2, ROUTE = 0;

	static final Color COLOR_DFS = new Color(124, 188, 227),
			COLOR_BFS = new Color(121, 235, 117), COLOR_TREE = new Color(252,
					106, 180), COLOR_SHORT = new Color(188, 86, 212),
			COLOR_ROUTE = new Color(149, 172, 193), COLOR_BG = new Color(243,
					245, 219);

	static BasicStroke dotted;

	// 箭头两边长度
	static final double arrow_length = 22;

	// 箭头两边偏移角度
	static final double arrow_diff = 9;

	// 线缩短长度
	static final double line_cut = 20;

	static ComboBoxRenderer comboBoxRenderer;

	static {
		intArray = new Integer[Main.icon_num];
		for (int i = 0; i < Main.icon_num; i++) {
			intArray[i] = new Integer(i);
		}
		dotted = new BasicStroke(1, BasicStroke.CAP_SQUARE,
				BasicStroke.JOIN_ROUND, 0, new float[] { 3, 7 }, 0);
		comboBoxRenderer = new ComboBoxRenderer();
		comboBoxRenderer.setPreferredSize(new Dimension(30, 20));
	}

	public UI() {
		super(new BorderLayout());
		// add
		add(drawPanel(), BorderLayout.CENTER);
		add(jPanel_control(), BorderLayout.SOUTH);
	}

	static DrawPanel drawPanel() {

		drawPanel = new DrawPanel(null);

		for (int i = 0; i < Main.icon_num; i++) {
			drawPanel.add(Main.icon[i]);
		}

		jTextField_DFS = new JTextField(String.valueOf(Main.length_DFS));
		jTextField_DFS.setHorizontalAlignment(JTextField.RIGHT);
		jTextField_DFS.setEditable(false);
		jTextField_DFS.setBackground(Color.white);
		jTextField_DFS.setForeground(COLOR_DFS);
		jTextField_DFS.setBorder(BorderFactory.createEmptyBorder());

		jTextField_BFS = new JTextField(String.valueOf(Main.length_BFS));
		jTextField_BFS.setHorizontalAlignment(JTextField.RIGHT);
		jTextField_BFS.setEditable(false);
		jTextField_BFS.setBackground(Color.white);
		jTextField_BFS.setForeground(COLOR_BFS);
		jTextField_BFS.setBorder(BorderFactory.createEmptyBorder());

		jTextField_tree = new JTextField(String.valueOf(Main.length_tree));
		jTextField_tree.setHorizontalAlignment(JTextField.RIGHT);
		jTextField_tree.setEditable(false);
		jTextField_tree.setBackground(Color.white);
		jTextField_tree.setForeground(COLOR_TREE);
		jTextField_tree.setBorder(BorderFactory.createEmptyBorder());

		jTextField_short = new JTextField(String.valueOf(Main.length_short));
		jTextField_short.setHorizontalAlignment(JTextField.RIGHT);
		jTextField_short.setEditable(false);
		jTextField_short.setBackground(Color.white);
		jTextField_short.setForeground(COLOR_SHORT);
		jTextField_short.setBorder(BorderFactory.createEmptyBorder());

		jTextField_route = new JTextField(String.valueOf(Main.length_tree));
		jTextField_route.setHorizontalAlignment(JTextField.RIGHT);
		jTextField_route.setEditable(false);
		jTextField_route.setBackground(Color.white);
		jTextField_route.setForeground(COLOR_ROUTE);
		jTextField_route.setBorder(BorderFactory.createEmptyBorder());

		drawPanel.add(jTextField_DFS);
		drawPanel.add(jTextField_BFS);
		drawPanel.add(jTextField_tree);
		drawPanel.add(jTextField_short);
		drawPanel.add(jTextField_route);

		drawPanel.setBackground(Color.white);
		return drawPanel;
	}

	static JPanel jPanel_control() {
		jPanel_control = new JPanel(new GridLayout(2, 1));

		JPanel jPanel = new JPanel(new GridLayout(1, 2));
		jPanel.add(jPanel_DFS());
		jPanel.add(jPanel_BFS());
		jPanel_control.add(jPanel);

		//
		jPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;

		jPanel.add(jPanel_shortest(), c);
		jPanel.add(jPanel_tree(), c);
		jPanel.add(jPanel_setting(), c);
		jPanel_control.add(jPanel);

		return jPanel_control;
	}

	static JPanel jPanel_DFS() {
		JLabel jLabel = new JLabel("起点:");
		jLabel.setForeground(UI.COLOR_DFS);

		jComboBox_DFS_begin = new JComboBox(intArray);
		jComboBox_DFS_begin.setRenderer(comboBoxRenderer);
		jComboBox_DFS_begin.setMaximumRowCount(10);
		jComboBox_DFS_begin.setSelectedIndex(5);

		JButton jButton_DFS_begin = new JButton("显示");
		jButton_DFS_begin.setForeground(UI.COLOR_DFS);
		jButton_DFS_begin.addActionListener(new ActionListener_buttons());
		jButton_DFS_begin.setActionCommand("DFS_begin");

		JButton jButton_DFS_clean = new JButton("清除");
		jButton_DFS_clean.setForeground(UI.COLOR_DFS);
		jButton_DFS_clean.addActionListener(new ActionListener_buttons());
		jButton_DFS_clean.setActionCommand("DFS_clean");

		jPanel_DFS = new JPanel();
		jPanel_DFS.add(jLabel);
		jPanel_DFS.add(jComboBox_DFS_begin);
		jPanel_DFS.add(jButton_DFS_begin);
		jPanel_DFS.add(jButton_DFS_clean);

		jPanel_DFS.setBorder(BorderFactory.createTitledBorder("深度优先遍历"));
		jPanel_DFS.setBackground(COLOR_BG);
		return jPanel_DFS;
	}

	static JPanel jPanel_BFS() {
		JLabel jLabel = new JLabel("起点:");
		jLabel.setForeground(UI.COLOR_BFS);

		jComboBox_BFS_begin = new JComboBox(intArray);
		jComboBox_BFS_begin.setRenderer(comboBoxRenderer);
		jComboBox_BFS_begin.setMaximumRowCount(10);
		jComboBox_BFS_begin.setSelectedIndex(3);

		JButton jButton_BFS_begin = new JButton("显示");
		jButton_BFS_begin.setForeground(UI.COLOR_BFS);
		jButton_BFS_begin.addActionListener(new ActionListener_buttons());
		jButton_BFS_begin.setActionCommand("BFS_begin");

		JButton jButton_BFS_clean = new JButton("清除");
		jButton_BFS_clean.setForeground(UI.COLOR_BFS);
		jButton_BFS_clean.addActionListener(new ActionListener_buttons());
		jButton_BFS_clean.setActionCommand("BFS_clean");

		jPanel_BFS = new JPanel();
		jPanel_BFS.add(jLabel);
		jPanel_BFS.add(jComboBox_BFS_begin);
		jPanel_BFS.add(jButton_BFS_begin);
		jPanel_BFS.add(jButton_BFS_clean);

		jPanel_BFS.setBorder(BorderFactory.createTitledBorder("广度优先遍历"));
		jPanel_BFS.setBackground(COLOR_BG);
		return jPanel_BFS;
	}

	static JPanel jPanel_tree() {

		JButton jButton_tree_begin = new JButton("显示");
		jButton_tree_begin.setForeground(UI.COLOR_TREE);
		jButton_tree_begin.addActionListener(new ActionListener_buttons());
		jButton_tree_begin.setActionCommand("tree_begin");

		JButton jButton_tree_clean = new JButton("清除");
		jButton_tree_clean.setForeground(UI.COLOR_TREE);
		jButton_tree_clean.addActionListener(new ActionListener_buttons());
		jButton_tree_clean.setActionCommand("tree_clean");

		jPanel_tree = new JPanel();
		jPanel_tree.add(jButton_tree_begin);
		jPanel_tree.add(jButton_tree_clean);
		jPanel_tree.setBorder(BorderFactory.createTitledBorder("最小生成树"));
		jPanel_tree.setBackground(COLOR_BG);
		return jPanel_tree;
	}

	static JPanel jPanel_shortest() {

		JLabel jLabel_begin = new JLabel("起点:");
		jLabel_begin.setForeground(UI.COLOR_SHORT);

		jComboBox_shortest_begin = new JComboBox(intArray);
		jComboBox_shortest_begin.setRenderer(comboBoxRenderer);
		jComboBox_shortest_begin.setMaximumRowCount(10);
		jComboBox_shortest_begin.setSelectedIndex(2);

		JLabel jLabel_end = new JLabel("终点:");
		jLabel_end.setForeground(UI.COLOR_SHORT);

		jComboBox_shortest_end = new JComboBox(intArray);
		jComboBox_shortest_end.setRenderer(comboBoxRenderer);
		jComboBox_shortest_end.setMaximumRowCount(10);
		jComboBox_shortest_end.setSelectedIndex(24);

		JButton jButton_shortest_begin = new JButton("显示");
		jButton_shortest_begin.setForeground(UI.COLOR_SHORT);
		jButton_shortest_begin.addActionListener(new ActionListener_buttons());
		jButton_shortest_begin.setActionCommand("shortest_begin");

		JButton jButton_shortest_clean = new JButton("清除");
		jButton_shortest_clean.setForeground(UI.COLOR_SHORT);
		jButton_shortest_clean.addActionListener(new ActionListener_buttons());
		jButton_shortest_clean.setActionCommand("shortest_clean");

		jPanel_shortest = new JPanel();
		jPanel_shortest.add(jLabel_begin);
		jPanel_shortest.add(jComboBox_shortest_begin);
		jPanel_shortest.add(jLabel_end);
		jPanel_shortest.add(jComboBox_shortest_end);
		jPanel_shortest.add(jButton_shortest_begin);
		jPanel_shortest.add(jButton_shortest_clean);

		jPanel_shortest.setBorder(BorderFactory.createTitledBorder("最短路径"));
		jPanel_shortest.setBackground(COLOR_BG);
		return jPanel_shortest;
	}

	static JPanel jPanel_setting() {
		jCheckBox_show_route = new JCheckBox("使用路线图");
		jCheckBox_show_route.setBackground(UI.COLOR_BG);
		jCheckBox_show_route.setForeground(UI.COLOR_ROUTE);
		jCheckBox_show_route.addActionListener(new ActionListener_buttons());
		jCheckBox_show_route.setActionCommand("show_route");

		jCheckBox_show_number = new JCheckBox("显示序号");
		jCheckBox_show_number.setBackground(UI.COLOR_BG);
		jCheckBox_show_number.setForeground(UI.COLOR_ROUTE);
		jCheckBox_show_number.addActionListener(new ActionListener_buttons());
		jCheckBox_show_number.setActionCommand("show_number");

		jPanel_setting = new JPanel();
		jPanel_setting.add(jCheckBox_show_route);
		jPanel_setting.add(jCheckBox_show_number);
		jPanel_setting.setBorder(BorderFactory.createTitledBorder("设置"));
		jPanel_setting.setBackground(COLOR_BG);
		return jPanel_setting;
	}
}

class DrawPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1552643368774889978L;

	public DrawPanel(LayoutManager layout) {
		super(layout);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		int from, no, to;

		// drawing DFS
		Queue queue = Main.Q_DFS.clone();
		from = UI.jComboBox_DFS_begin.getSelectedIndex();
		no = 0;
		to = 0;
		queue.deQueue();
		while (!queue.isEmpty()) {
			no++;
			to = ((Integer) queue.deQueue()).intValue();
			drawLine(g, from, to, UI.DFS, no, UI.COLOR_DFS);
			from = to;
		}

		// drawing BFS
		queue = Main.Q_BFS.clone();
		from = UI.jComboBox_BFS_begin.getSelectedIndex();
		no = 0;
		to = 0;
		while (!queue.isEmpty()) {
			no++;
			to = ((Integer) queue.deQueue()).intValue();
			drawLine(g, from, to, UI.BFS, no, UI.COLOR_BFS);
			from = to;
		}

		// drawing shortest
		queue = Main.Q_shortest.clone();
		from = UI.jComboBox_shortest_begin.getSelectedIndex();
		no = 0;
		to = 0;
		while (!queue.isEmpty()) {
			no++;
			to = ((Integer) queue.deQueue()).intValue();
			drawLine(g, from, to, UI.SHORT, no, UI.COLOR_SHORT);
			from = to;
		}

		// drawing tree
		queue = Main.Q_tree.clone();
		no = 0;
		Dimension d;
		while (!queue.isEmpty()) {
			no++;
			d = (Dimension) queue.deQueue();
			drawLine(g, d.width, d.height, UI.TREE, no, UI.COLOR_TREE);
		}

		// drawing route
		if (UI.jCheckBox_show_route.isSelected()) {
			queue = Main.Q_route.clone();
			no = -1;

			((Graphics2D) g).setStroke(UI.dotted);
			while (!queue.isEmpty()) {
				d = (Dimension) queue.deQueue();
				drawLine(g, d.width, d.height, UI.ROUTE, no, UI.COLOR_ROUTE);
			}
		}
		d = (Dimension) UI.drawPanel.getSize();

		// 总长度
		UI.jTextField_DFS.setBounds(d.width - 45, d.height - 100, 40, 20);

		UI.jTextField_BFS.setBounds(d.width - 45, d.height - 80, 40, 20);

		UI.jTextField_short.setBounds(d.width - 45, d.height - 60, 40, 20);

		UI.jTextField_tree.setBounds(d.width - 45, d.height - 40, 40, 20);

		UI.jTextField_route.setBounds(d.width - 45, d.height - 20, 40, 20);

	}

	// 画从n1节点到n2节点的箭头
	// kind为线的种类(如 MainUI.TREE = 0),决定线的偏移以防四线重叠
	private void drawLine(Graphics g, int n1, int n2, int kind, int no,
			Color color) {
		int x1, y1, x2, y2;
		g.setColor(color);
		x1 = Main.icon[n1].getX() + 10;
		y1 = Main.icon[n1].getY() + 10;
		x2 = Main.icon[n2].getX() + 10;
		y2 = Main.icon[n2].getY() + 10;

		y1 += kind;
		y2 += kind;

		if (kind == UI.ROUTE || kind == UI.TREE) {
			g.drawLine(x1, y1, x2, y2);
		} else {
			drawArrow(g, x1, y1, x2, y2);
		}
		if (UI.jCheckBox_show_number.isSelected() && kind != UI.ROUTE) {
			g.drawString(String.valueOf(no), (x1 + (x2 - x1) / 2 + kind * 10),
					(y1 + (y2 - y1) / 2 + 15));
		}
	}

	// 画箭头,给定源坐标与目的坐标,路径序号
	private void drawArrow(Graphics g, int x1, int y1, int x2, int y2) {
		g.drawLine(x1, y1, x2, y2);
		double angle = Math.atan(((double) (y2 - y1)) / (x2 - x1));
		angle += (UI.arrow_diff / 180) * Math.PI;
		int dx, dy, k;
		if (x2 >= x1) {
			k = 1;
		} else {
			k = -1;
		}
		dx = k * (int) (UI.arrow_length * Math.cos(angle));
		dy = k * (int) (UI.arrow_length * Math.sin(angle));
		g.drawLine(x2, y2, x2 - dx, y2 - dy);
		angle -= (UI.arrow_diff / 180) * Math.PI * 2;
		dx = k * (int) (UI.arrow_length * Math.cos(angle));
		dy = k * (int) (UI.arrow_length * Math.sin(angle));
		g.drawLine(x2, y2, x2 - dx, y2 - dy);
	}
}

⌨️ 快捷键说明

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