📄 treevisualizer.java
字号:
JOptionPane.showMessageDialog(this, "Error, there is no " + "selected Node to perform this " + "operation on.", "Error!", JOptionPane.ERROR_MESSAGE); } } else if (e.getActionCommand().equals("Remove Child Nodes")) { if (m_focusNode >= 0) { if (m_listener != null) { //then send message to the listener m_listener.userCommand(new TreeDisplayEvent(TreeDisplayEvent.REMOVE_CHILDREN, m_nodes[m_focusNode].m_node.getRefer())); } else { JOptionPane.showMessageDialog(this, "Sorry, there is no " + "available Decsion Tree to " + "perform this operation on.", "Sorry!", JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(this, "Error, there is no " + "selected Node to perform this " + "operation on.", "Error!", JOptionPane.ERROR_MESSAGE); } } else if (e.getActionCommand().equals("classify_child")) { if (m_focusNode >= 0) { if (m_listener != null) { //then send message to the listener m_listener.userCommand(new TreeDisplayEvent (TreeDisplayEvent.CLASSIFY_CHILD, m_nodes[m_focusNode].m_node.getRefer())); } else { JOptionPane.showMessageDialog(this, "Sorry, there is no " + "available Decsion Tree to " + "perform this operation on.", "Sorry!", JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(this, "Error, there is no " + "selected Node to perform this " + "operation on.", "Error!", JOptionPane.ERROR_MESSAGE); } } else if (e.getActionCommand().equals("send_instances")) { if (m_focusNode >= 0) { if (m_listener != null) { //then send message to the listener m_listener.userCommand(new TreeDisplayEvent (TreeDisplayEvent.SEND_INSTANCES, m_nodes[m_focusNode].m_node.getRefer())); } else { JOptionPane.showMessageDialog(this, "Sorry, there is no " + "available Decsion Tree to " + "perform this operation on.", "Sorry!", JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(this, "Error, there is no " + "selected Node to perform this " + "operation on.", "Error!", JOptionPane.ERROR_MESSAGE); } } else if (e.getActionCommand().equals("Accept The Tree")) { if (m_listener != null) { //then send message to the listener saying that the tree is done m_listener.userCommand(new TreeDisplayEvent(TreeDisplayEvent.ACCEPT, null)); } else { JOptionPane.showMessageDialog(this, "Sorry, there is no " + "available Decision Tree to " + "perform this operation on.", "Sorry!", JOptionPane.WARNING_MESSAGE); } } } /** * Performs the action associated with the ItemEvent. * * @param e the item event. */ public void itemStateChanged(ItemEvent e) { JRadioButtonMenuItem c = (JRadioButtonMenuItem)e.getSource(); if (c.getActionCommand().equals("Size 24")) { changeFontSize(24); } else if (c.getActionCommand().equals("Size 22")) { changeFontSize(22); } else if (c.getActionCommand().equals("Size 20")) { changeFontSize(20); } else if (c.getActionCommand().equals("Size 18")) { changeFontSize(18); } else if (c.getActionCommand().equals("Size 16")) { changeFontSize(16); } else if (c.getActionCommand().equals("Size 14")) { changeFontSize(14); } else if (c.getActionCommand().equals("Size 12")) { changeFontSize(12); } else if (c.getActionCommand().equals("Size 10")) { changeFontSize(10); } else if (c.getActionCommand().equals("Size 8")) { changeFontSize(8); } else if (c.getActionCommand().equals("Size 6")) { changeFontSize(6); } else if (c.getActionCommand().equals("Size 4")) { changeFontSize(4); } else if (c.getActionCommand().equals("Size 2")) { changeFontSize(2); } else if (c.getActionCommand().equals("Size 1")) { changeFontSize(1); } else if (c.getActionCommand().equals("Hide Descendants")) { //focus_node.setCVisible(!c.isSelected()); //no longer used... } } /** * Does nothing. * @param e the mouse event. */ public void mouseClicked(MouseEvent e) { //if the mouse was left clicked on //the node then if (m_clickAvailable) { //determine if the click was on a node or not int s = -1; for (int noa = 0; noa < m_numNodes;noa++) { if (m_nodes[noa].m_quad == 18) { //then is on the screen calcScreenCoords(noa); if (e.getX() <= m_nodes[noa].m_center + m_nodes[noa].m_side && e.getX() >= m_nodes[noa].m_center - m_nodes[noa].m_side && e.getY() >= m_nodes[noa].m_top && e.getY() <= m_nodes[noa].m_top + m_nodes[noa].m_height) { //then it is this node that the mouse was clicked on s = noa; } m_nodes[noa].m_top = 32000; } } m_focusNode = s; if (m_focusNode != -1) { if (m_listener != null) { //then set this to be the selected node for editing actionPerformed(new ActionEvent(this, 32000, "Create Child Nodes")); } else { //then open a visualize to display this nodes instances if possible actionPerformed(new ActionEvent(this, 32000, "Visualize The Node")); } } } } /** * Determines what action the user wants to perform. * * @param e the mouse event. */ public void mousePressed(MouseEvent e) { m_frameLimiter.setRepeats(true); if ((e.getModifiers() & e.BUTTON1_MASK) != 0 && m_mouseState == 0 && m_scaling == 0) { //then the left mouse button has been pressed //check for modifiers if ((e.getModifiers() & e.CTRL_MASK) != 0) { //then is in zoom out mode m_mouseState = 2; } else if ((e.getModifiers() & e.SHIFT_MASK) != 0) { //then is in zoom mode //note if both are pressed default action is to zoom out m_oldMousePos.width = e.getX(); m_oldMousePos.height = e.getY(); m_newMousePos.width = e.getX(); m_newMousePos.height = e.getY(); m_mouseState = 3; Graphics g = getGraphics(); g.setColor(Color.black); g.setXORMode(Color.white); g.drawRect(m_oldMousePos.width, m_oldMousePos.height, m_newMousePos.width - m_oldMousePos.width, m_newMousePos.height - m_oldMousePos.height); g.dispose(); } else { //no modifiers drag area around m_oldMousePos.width = e.getX(); m_oldMousePos.height = e.getY(); m_newMousePos.width = e.getX(); m_newMousePos.height = e.getY(); m_mouseState = 1; m_frameLimiter.start(); } } else if (m_mouseState == 0 && m_scaling == 0) { //either middle or right mouse button pushed //determine menu to use } } /** * Performs the final stages of what the user wants to perform. * * @param e the mouse event. */ public void mouseReleased(MouseEvent e) { if (m_mouseState == 1) { //this is used by mouseClicked to determine if it is alright to do //something m_clickAvailable = true; //note that a standard click with the left mouse is pretty much the //only safe input left to be assigned anything. } else { m_clickAvailable = false; } if (m_mouseState == 2 && mouseInBounds(e)) { //then zoom out; m_mouseState = 0; Dimension ns = new Dimension(m_viewSize.width / 2, m_viewSize.height / 2); if (ns.width < 10) { ns.width = 10; } if (ns.height < 10) { ns.height = 10; } Dimension d = getSize(); Dimension np = new Dimension((int)(d.width / 2 - ((double)d.width / 2 - m_viewPos.width) / 2), (int)(d.height / 2 - ((double)d.height / 2 - m_viewPos.height) / 2)); animateScaling(np, ns, 10); //view_pos.width += view_size.width / 2; //view_pos.height += view_size.height / 2; } else if (m_mouseState == 3) { //then zoom in m_mouseState = 0; Graphics g = getGraphics(); g.setColor(Color.black); g.setXORMode(Color.white); g.drawRect(m_oldMousePos.width, m_oldMousePos.height, m_newMousePos.width - m_oldMousePos.width, m_newMousePos.height - m_oldMousePos.height); g.dispose(); int cw = m_newMousePos.width - m_oldMousePos.width; int ch = m_newMousePos.height - m_oldMousePos.height; if (cw >= 1 && ch >= 1) { if (mouseInBounds(e) && (getSize().width / cw) <= 6 && (getSize().height / ch) <= 6) { //now calculate new position and size Dimension ns = new Dimension(); Dimension np = new Dimension(); double nvsw = getSize().width / (double)(cw); double nvsh = getSize().height / (double)(ch); np.width = (int)((m_oldMousePos.width - m_viewPos.width) * -nvsw); np.height = (int)((m_oldMousePos.height - m_viewPos.height) * -nvsh); ns.width = (int)(m_viewSize.width * nvsw); ns.height = (int)(m_viewSize.height * nvsh); animateScaling(np, ns, 10); } } } else if (m_mouseState == 0 && m_scaling == 0) { //menu m_mouseState = 0; setFont(new Font("A Name", 0, 12)); //determine if the click was on a node or not int s = -1; for (int noa = 0; noa < m_numNodes;noa++) { if (m_nodes[noa].m_quad == 18) { //then is on the screen calcScreenCoords(noa); if (e.getX() <= m_nodes[noa].m_center + m_nodes[noa].m_side && e.getX() >= m_nodes[noa].m_center - m_nodes[noa].m_side && e.getY() >= m_nodes[noa].m_top && e.getY() <= m_nodes[noa].m_top + m_nodes[noa].m_height) { //then it is this node that the mouse was clicked on s = noa; } m_nodes[noa].m_top = 32000; } } if (s == -1) { //the mouse wasn't clicked on a node m_winMenu.show(this,e.getX(),e.getY()); } else { //the mouse was clicked on a node m_focusNode = s; m_nodeMenu.show(this, e.getX(), e.getY()); } setFont(m_currentFont); } else if (m_mouseState == 1) { //dragging m_mouseState = 0; m_frameLimiter.stop(); repaint(); } } /** * Checks to see if the coordinates of the mouse lie on this JPanel. * * @param e the mouse event. * @return true if the mouse lies on this JPanel. */ private boolean mouseInBounds(MouseEvent e) { //this returns true if the mouse is currently over the canvas otherwise //false if (e.getX() < 0 || e.getY() < 0 || e.getX() > getSize().width || e.getY() > getSize().height) { return false; } return true; } /** * Performs intermediate updates to what the user wishes to do. * * @param e the mouse event. */ public void mouseDragged(MouseEvent e) { //use mouse state to determine what to do to the view of the tree if (m_mouseState == 1) { //then dragging view m_oldMousePos.width = m_newMousePos.width; m_oldMousePos.height = m_newMousePos.height; m_newMousePos.width = e.getX(); m_newMousePos.height = e.getY(); m_viewPos.width += m_newMousePos.width - m_oldMousePos.width; m_viewPos.height += m_newMousePos.height - m_oldMousePos.height; } else if (m_mouseState == 3) { //then zoom box being created //redraw the zoom box Graphics g = getGraphics(); g.setColor(Color.black); g.setXORMode(Color.white); g.drawRect(m_oldMousePos.width, m_oldMousePos.height, m_newMousePos.width - m_oldMousePos.width, m_newMousePos.height - m_oldMousePos.height); m_newMousePos.width = e.getX(); m_newMousePos.height = e.getY(); g.drawRect(m_oldMousePos.width, m_oldMousePos.height, m_newMousePos.width - m_oldMousePos.width, m_newMousePos.height - m_oldMousePos.height); g.dispose(); } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -