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

📄 visualizepanel.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	    
	    gx.setColor(Color.gray);
	    gx.fillPolygon(ar1, ar2, (tmp.size() - 1) / 2);
	    gx.setColor(Color.black);
	    gx.drawPolyline(ar1, ar2, (tmp.size() - 1) / 2);
	  }
	}
      }
      
      if (m_shapePoints != null) {
	//then the current image needs to be refreshed
	if (((Double)m_shapePoints.elementAt(0)).intValue() == 2 ||
	    ((Double)m_shapePoints.elementAt(0)).intValue() == 3) {
	  gx.setColor(Color.black);
	  gx.setXORMode(Color.white);
	  int[] ar1, ar2;
	  ar1 = getXCoords(m_shapePoints);
	  ar2 = getYCoords(m_shapePoints);
	  gx.drawPolyline(ar1, ar2, (m_shapePoints.size() - 1) / 2);
	  m_newMousePos.width = (int)Math.ceil
	    (m_plot2D.convertToPanelX(((Double)m_shapePoints.elementAt
			      (m_shapePoints.size() - 2)).doubleValue()));
	  
	  m_newMousePos.height = (int)Math.ceil
	    (m_plot2D.convertToPanelY(((Double)m_shapePoints.elementAt
			      (m_shapePoints.size() - 1)).doubleValue()));
	  
	  gx.drawLine((int)Math.ceil
		     (m_plot2D.convertToPanelX(((Double)m_shapePoints.elementAt
						(m_shapePoints.size() - 2)).
					       doubleValue())),
		      (int)Math.ceil(m_plot2D.convertToPanelY
				     (((Double)m_shapePoints.elementAt
				       (m_shapePoints.size() - 1)).
				      doubleValue())),
		      m_newMousePos.width, m_newMousePos.height);
	  gx.setPaintMode();
	}
      }
    }
    
    /**
     * This is called for polylines to see where there two lines that
     * extend to infinity cut the border of the view.
     * @param x1 an x point along the line
     * @param y1 the accompanying y point.
     * @param x2 The x coord of the end point of the line.
     * @param y2 The y coord of the end point of the line.
     * @param x 0 or the width of the border line if it has one.
     * @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. */

⌨️ 快捷键说明

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