📄 visualizepanel.java
字号:
* @param y 0 or the height of the border line if it has one. * @param offset The offset for the border line (either for x or y * dependant on which one doesn't change). * @return double array that contains the coordinate for the point * that the polyline cuts the border (which ever side that may be). */ private double[] lineIntersect(double x1, double y1, double x2, double y2, double x, double y, double offset) { //the first 4 params are thestart and end points of a line //the next param is either 0 for no change in x or change in x, //the next param is the same for y //the final 1 is the offset for either x or y (which ever has no change) double xval; double yval; double xn = -100, yn = -100; double change; if (x == 0) { if ((x1 <= offset && offset < x2) || (x1 >= offset && offset > x2)) { //then continue xval = x1 - x2; change = (offset - x2) / xval; yn = (y1 - y2) * change + y2; if (0 <= yn && yn <= y) { //then good xn = offset; } else { //no intersect xn = -100; } } } else if (y == 0) { if ((y1 <= offset && offset < y2) || (y1 >= offset && offset > y2)) { //the continue yval = (y1 - y2); change = (offset - y2) / yval; xn = (x1 - x2) * change + x2; if (0 <= xn && xn <= x) { //then good yn = offset; } else { xn = -100; } } } double[] ret = new double[2]; ret[0] = xn; ret[1] = yn; return ret; } /** * This will convert a polyline to a polygon for drawing purposes * So that I can simply use the polygon drawing function. * @param v The polyline to convert. * @return A FastVector containing the polygon. */ private FastVector makePolygon(FastVector v) { FastVector building = new FastVector(v.size() + 10); double x1, y1, x2, y2; int edge1 = 0, edge2 = 0; for (int noa = 0; noa < v.size() - 2; noa++) { building.addElement(new Double(((Double)v.elementAt(noa)). doubleValue())); } //now clip the lines double[] new_coords; //note lineIntersect , expects the values to have been converted to //screen coords //note the first point passed is the one that gets shifted. x1 = m_plot2D.convertToPanelX(((Double)v.elementAt(1)).doubleValue()); y1 = m_plot2D.convertToPanelY(((Double)v.elementAt(2)).doubleValue()); x2 = m_plot2D.convertToPanelX(((Double)v.elementAt(3)).doubleValue()); y2 = m_plot2D.convertToPanelY(((Double)v.elementAt(4)).doubleValue()); if (x1 < 0) { //test left new_coords = lineIntersect(x1, y1, x2, y2, 0, this.getHeight(), 0); edge1 = 0; if (new_coords[0] < 0) { //then not left if (y1 < 0) { //test top new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, 0); edge1 = 1; } else { //test bottom new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, this.getHeight()); edge1 = 3; } } } else if (x1 > this.getWidth()) { //test right new_coords = lineIntersect(x1, y1, x2, y2, 0, this.getHeight(), this.getWidth()); edge1 = 2; if (new_coords[0] < 0) { //then not right if (y1 < 0) { //test top new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, 0); edge1 = 1; } else { //test bottom new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, this.getHeight()); edge1 = 3; } } } else if (y1 < 0) { //test top new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, 0); edge1 = 1; } else { //test bottom new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, this.getHeight()); edge1 = 3; } building.setElementAt(new Double(m_plot2D.convertToAttribX(new_coords[0])), 1); building.setElementAt(new Double(m_plot2D.convertToAttribY(new_coords[1])), 2); x1 = m_plot2D.convertToPanelX(((Double)v.elementAt(v.size() - 4)). doubleValue()); y1 = m_plot2D.convertToPanelY(((Double)v.elementAt(v.size() - 3)). doubleValue()); x2 = m_plot2D.convertToPanelX(((Double)v.elementAt(v.size() - 6)). doubleValue()); y2 = m_plot2D.convertToPanelY(((Double)v.elementAt(v.size() - 5)). doubleValue()); if (x1 < 0) { //test left new_coords = lineIntersect(x1, y1, x2, y2, 0, this.getHeight(), 0); edge2 = 0; if (new_coords[0] < 0) { //then not left if (y1 < 0) { //test top new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, 0); edge2 = 1; } else { //test bottom new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, this.getHeight()); edge2 = 3; } } } else if (x1 > this.getWidth()) { //test right new_coords = lineIntersect(x1, y1, x2, y2, 0, this.getHeight(), this.getWidth()); edge2 = 2; if (new_coords[0] < 0) { //then not right if (y1 < 0) { //test top new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, 0); edge2 = 1; } else { //test bottom new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, this.getHeight()); edge2 = 3; } } } else if (y1 < 0) { //test top new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, 0); edge2 = 1; } else { //test bottom new_coords = lineIntersect(x1, y1, x2, y2, this.getWidth(), 0, this.getHeight()); edge2 = 3; } building.setElementAt(new Double(m_plot2D.convertToAttribX(new_coords[0])), building.size() - 2); building.setElementAt(new Double(m_plot2D.convertToAttribY(new_coords[1])), building.size() - 1); //trust me this complicated piece of code will //determine what points on the boundary of the view to add to the polygon int xp, yp; xp = this.getWidth() * ((edge2 & 1) ^ ((edge2 & 2) / 2)); yp = this.getHeight() * ((edge2 & 2) / 2); //System.out.println(((-1 + 4) % 4) + " hoi"); if (inPolyline(v, m_plot2D.convertToAttribX(xp), m_plot2D.convertToAttribY(yp))) { //then add points in a clockwise direction building.addElement(new Double(m_plot2D.convertToAttribX(xp))); building.addElement(new Double(m_plot2D.convertToAttribY(yp))); for (int noa = (edge2 + 1) % 4; noa != edge1; noa = (noa + 1) % 4) { xp = this.getWidth() * ((noa & 1) ^ ((noa & 2) / 2)); yp = this.getHeight() * ((noa & 2) / 2); building.addElement(new Double(m_plot2D.convertToAttribX(xp))); building.addElement(new Double(m_plot2D.convertToAttribY(yp))); } } else { xp = this.getWidth() * ((edge2 & 2) / 2); yp = this.getHeight() * (1 & ~((edge2 & 1) ^ ((edge2 & 2) / 2))); if (inPolyline(v, m_plot2D.convertToAttribX(xp), m_plot2D.convertToAttribY(yp))) { //then add points in anticlockwise direction building.addElement(new Double(m_plot2D.convertToAttribX(xp))); building.addElement(new Double(m_plot2D.convertToAttribY(yp))); for (int noa = (edge2 + 3) % 4; noa != edge1; noa = (noa + 3) % 4) { xp = this.getWidth() * ((noa & 2) / 2); yp = this.getHeight() * (1 & ~((noa & 1) ^ ((noa & 2) / 2))); building.addElement(new Double(m_plot2D.convertToAttribX(xp))); building.addElement(new Double(m_plot2D.convertToAttribY(yp))); } } } return building; } /** * This will extract from a polygon shape its x coodrdinates * so that an awt.Polygon can be created. * @param v The polygon shape. * @return an int array containing the screen x coords for the polygon. */ private int[] getXCoords(FastVector v) { int cach = (v.size() - 1) / 2; int[] ar = new int[cach]; for (int noa = 0; noa < cach; noa ++) { ar[noa] = (int)m_plot2D.convertToPanelX(((Double)v.elementAt(noa * 2 + 1)).doubleValue()); } return ar; } /** * This will extract from a polygon shape its y coordinates * so that an awt.Polygon can be created. * @param v The polygon shape. * @return an int array containing the screen y coords for the polygon. */ private int[] getYCoords(FastVector v) { int cach = (v.size() - 1) / 2; int[] ar = new int[cach]; for (int noa = 0; noa < cach; noa ++) { ar[noa] = (int)m_plot2D. convertToPanelY(((Double)v.elementAt(noa * 2 + 2)). doubleValue()); } return ar; } /** * Renders the polygons if necessary * @param gx the graphics context */ public void prePlot(Graphics gx) { super.paintComponent(gx); if (m_plotInstances != null) { drawShapes(gx); // will be in paintComponent of ShapePlot2D } } } /** default colours for colouring discrete class */ protected Color [] m_DefaultColors = {Color.blue, Color.red, Color.green, Color.cyan, Color.pink, new Color(255, 0, 255), Color.orange, new Color(255, 0, 0), new Color(0, 255, 0), Color.white}; /** Lets the user select the attribute for the x axis */ protected JComboBox m_XCombo = new JComboBox(); /** Lets the user select the attribute for the y axis */ protected JComboBox m_YCombo = new JComboBox(); /** Lets the user select the attribute to use for colouring */ protected JComboBox m_ColourCombo = new JComboBox(); /** Lets the user select the shape they want to create for instance * selection. */ protected JComboBox m_ShapeCombo = new JComboBox(); /** Button for the user to enter the splits. */ protected JButton m_submit = new JButton("Submit"); /** Button for the user to remove all splits. */ protected JButton m_cancel = new JButton("Clear"); /** Button for the user to save the visualized set of instances */ protected JButton m_saveBut = new JButton("Save"); /** Stop the combos from growing out of control */ private Dimension COMBO_SIZE = new Dimension(250, m_saveBut .getPreferredSize().height); /** file chooser for saving instances */ protected JFileChooser m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); /** Filter to ensure only arff files are selected */ protected FileFilter m_ArffFilter = new ExtensionFileFilter(Instances.FILE_EXTENSION, "Arff data files"); /** Label for the jitter slider */ protected JLabel m_JitterLab= new JLabel("Jitter",SwingConstants.RIGHT); /** The jitter slider */ protected JSlider m_Jitter = new JSlider(0,50,0); /** The panel that displays the plot */ protected PlotPanel m_plot = new PlotPanel(); /** The panel that displays the attributes , using color to represent * another attribute. */ protected AttributePanel m_attrib = new AttributePanel(); /** The panel that displays legend info if there is more than one plot */ protected LegendPanel m_legendPanel = new LegendPanel(); /** Panel that surrounds the plot panel with a titled border */ protected JPanel m_plotSurround = new JPanel(); /** Panel that surrounds the class panel with a titled border */ protected JPanel m_classSurround = new JPanel(); /** An optional listener that we will inform when ComboBox selections change */ protected ActionListener listener = null; /** An optional listener that we will inform when the user creates a * split to seperate instances. */ protected VisualizePanelListener m_splitListener = null; /** The name of the plot (not currently displayed, but can be used in the containing Frame or Panel) */ protected String m_plotName = ""; /** The panel that displays the legend for the colouring attribute */ protected ClassPanel m_classPanel = new ClassPanel(); /** The list of the colors used */ protected FastVector m_colorList; /** These hold the names of preferred columns to visualize on---if the user has defined them in the Visualize.props file */ protected String m_preferredXDimension = null; protected String m_preferredYDimension = null; protected String m_preferredColourDimension = null; /** Show the attribute bar panel */ protected boolean m_showAttBars = true; /** the logger */ protected Logger m_Log; /** * Sets the Logger to receive informational messages * * @param newLog the Logger that will now get info messages */ public void setLog(Logger newLog) { m_Log = newLog; } /** This constructor allows a VisualizePanelListener to be set. */ public VisualizePanel(VisualizePanelListener ls) { this(); m_splitListener = ls; } /** * Set the properties for the VisualizePanel */ private void setProperties(String relationName) { if (VisualizeUtils.VISUALIZE_PROPERTIES != null) { String thisClass = this.getClass().getName(); if (relationName == null) { String showAttBars = thisClass+".displayAttributeBars"; String val = VisualizeUtils.VISUALIZE_PROPERTIES. getProperty(showAttBars); if (val == null) { System.err.println("Displaying attribute bars "); m_showAttBars = true; } else { if (val.compareTo("true") == 0 || val.compareTo("on") == 0) { System.err.println("Displaying attribute bars "); m_showAttBars = true; } else { m_showAttBars = false; } } } else { System.err.println("Looking for preferred visualization dimensions for " +relationName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -