📄 displayarea.java,v
字号:
1.6log@fixed javadoc errors.@text@d1 467a467 554/** * * file: DisplayArea.java * * last editted: Ryan Irwin * */// import necessary java libraries//import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;//These imports are not needed - Phil T. 6-23-03//import javax.swing.Timer;/** * class implements the graph plot that displays the output points * and the comouted decision region based on the algorithm used * * hierarchy: JPanel->DisplayArea * */class DisplayArea extends JPanel { // ********************************************************************* // // declare global variables and components // // ********************************************************************* static final double ZOOM_SCALE = 1.25; static final double precision = 400; // output points // Vector output_points_d = new Vector(); Vector type_d = new Vector(); Vector color_d = new Vector(); // declare global variables // int width = 0; // width of the canvas int height = 0; // height of the canvas int zeroX = 0; // zero position on x axis int zeroY = 0; // zero position on y axis int currObject = 0; // current array object being referenced double scaleX = 0.0; // scale for point on the x-axis; double scaleY = 0.0; // scale for point on the y-axis; int xGridDiv = 21; // plot grid division for the x-axis int yGridDiv = 21; // plot grid division for the y-axis Vector disp; // vector of data objects for plotting Vector supp; // vector of support region data points Vector decisionPoints; // vector which stores all decision points Vector kmeans; // vector of k-mean classifications Vector binary; // vector of binary classifications //Vector color; // vector of color objects double xmax = 1.0; // maximum limit of the x-axis double xmin = -1.0; // minimum limit of the x-axis double ymax = 1.0; // maximum limit of the y-axis double ymin = -1.0; // minimum limit of the y-axis int[][] pointsTable; // table of points membership // ********************************************************************* // // declare class constructors // // ********************************************************************* /** * constructor initializes the samples to be plotted in the signal panel * */ public DisplayArea() { super(); // initialize the vector of data objects // color_d = new Vector(13, 10); // initialize the color vector // /* color.addElement(new Color(240, 230, 140)); // khaki color.addElement(new Color(255, 200, 0)); // orange color.addElement(new Color(255, 215, 0)); // gold color.addElement(new Color(255, 0, 255)); // magenta color.addElement(new Color(0, 255, 255)); // cyan color.addElement(new Color(192, 192, 192)); // light green color.addElement(new Color(255, 175, 175)); // pink color.addElement(new Color(64, 224, 208)); // turquoise color.addElement(new Color(0, 0, 255)); // blue color.addElement(new Color(50, 205, 50)); // lime green color.addElement(new Color(160, 32, 240)); // purple color.addElement(new Color(127, 255, 212)); // aquamarine color.addElement(new Color(255, 0, 0)); // red color.addElement(new Color(176, 48, 96)); // maroon color.addElement(new Color(0, 255, 0)); // green color.addElement(new Color(238, 130, 238)); // violet color.addElement(new Color(189, 183, 107)); // dark khaki color.addElement(new Color(95, 158, 160)); // cadet blue */ // add action listeners to the canvas // MyListener myListener = new MyListener(); addMouseListener(myListener); addMouseMotionListener(myListener); } /** * * class implements a mouse listener, mouse motion listener for the * output display canvas * */ class MyListener extends MouseAdapter implements MouseListener, MouseMotionListener { /** * listen and act on actions fired by components attached to the mouse * * @@param e MouseEvent that was fired * */ public void mouseClicked(MouseEvent e) { if(SwingUtilities.isLeftMouseButton(e) && Classify.main_menu_d.zoomi) { Point eventPoint = e.getPoint(); // System.out.println("x"+eventPoint.x+"y"+eventPoint.y); MyPoint center = DataPoints.convertPoint(eventPoint, getWidth(), getHeight(), getDisplayScale()); // System.out.println("x"+eventPoint.x+"y"+eventPoint.y); //System.out.println(center); setDisplayScale(new DisplayScale( center.x + (xmax - xmin)/2, center.x - (xmax - xmin)/2, center.y + (ymax - ymin)/2, center.y - (ymax - ymin)/2)); setDisplayScale(new DisplayScale( MathUtil.setDecimal(xmax/ZOOM_SCALE, 2), MathUtil.setDecimal(xmin/ZOOM_SCALE, 2), MathUtil.setDecimal(ymax/ZOOM_SCALE, 2), MathUtil.setDecimal(ymin/ZOOM_SCALE, 2))); Classify.main_menu_d.zoomi = false; } if(SwingUtilities.isLeftMouseButton(e) && Classify.main_menu_d.zoomo) { Point eventPoint = e.getPoint(); MyPoint center = DataPoints.convertPoint(eventPoint, getWidth(), getHeight(), getDisplayScale()); setDisplayScale(new DisplayScale( center.x + (xmax - xmin)/2, center.x - (xmax - xmin)/2, center.y + (ymax - ymin)/2, center.y - (ymax - ymin)/2)); setDisplayScale(new DisplayScale( MathUtil.setDecimal(xmax*ZOOM_SCALE, 2), MathUtil.setDecimal(xmin*ZOOM_SCALE, 2), MathUtil.setDecimal(ymax*ZOOM_SCALE, 2), MathUtil.setDecimal(ymin*ZOOM_SCALE, 2))); Classify.main_menu_d.zoomo = false; } } /** * listen and act on mouse press * * @@param e MouseEvent that was fired * */ public void mousePressed(MouseEvent e) { // this method is required to be present } /** * listen and act on mouse release * * @@param e MouseEvent that was fired * */ public void mouseReleased(MouseEvent e) { // this method is required to be present } /** * listen and act on mouse drag * * @@param e MouseEvent that was fired * */ public void mouseDragged(MouseEvent e) { // this method is required to be present } /** * listen and act on this mouse action * * @@param e MouseEvent that was fired * */ public void mouseMoved(MouseEvent e) { // this method is required to be present } /** * listen and act on this mouse action * * @@param e MouseEvent that was fired * */ public void mouseExited(MouseEvent e) { // this method is required to be present } /** * listen and act on this mouse action * * @@param e MouseEvent that was fired * */ public void mouseEntered(MouseEvent e) { // this method is required to be present } } // ********************************************************************* // // declare class methods // // ********************************************************************* /** * returns the width on the output canvas * * @@return width of the output plot * */ public int getWidth() { // determine the dimensions of the canvas // DetermineDimensions(); // return the width // return width; } /** * returns the height on the output canvas * * @@return height of the output plot * */ public int getHeight() { // determine the dimensions of the canvas // DetermineDimensions(); // return the height // return height; } /** * sets the points table * */ public void setPointsTable() { // declare local variables // Point p; // allocate memory to the table // pointsTable = new int[width][height]; // reset the table // for (int i=0; i<width; i++) { for (int j=0; j<height; j++) { pointsTable[i][j] = 0; } } // add the members of the first set to the table // for (int i=0; i<data.set1.size(); i++) { p = (Point)data.set1.elementAt(i); if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) { pointsTable[p.x][p.y] = 1; } } // add the members of the second set to the table // for (int i=0; i<data.set2.size(); i++) { p = (Point)data.set2.elementAt(i); if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) { pointsTable[p.x][p.y] = 2; } } // add the members of the third set to the table // for (int i=0; i<data.set3.size(); i++) { p = (Point)data.set3.elementAt(i); if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) { pointsTable[p.x][p.y] = 3; } } // add the members of the forth set to the table // for (int i=0; i<data.set4.size(); i++) { p = (Point)data.set4.elementAt(i); if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) { pointsTable[p.x][p.y] = 4; } } } /* */ /** * determine the dimensions of the selection area * */ public void DetermineDimensions() { Dimension dimen = this.getSize(); width = dimen.width; height = dimen.height; // divide by 2 // zeroX = width >> 1; zeroY = height >> 1; } /** * draw the selection area grid lines * * @@param g graphics paint object * */ public void DrawGrid(Graphics g) { // determine the dimensions of the drawing canvas // DetermineDimensions(); // clear the canvas // setBackground(Color.white); // draw minor grid in light gray // start from center and draw in each direction // g.setColor(Color.gray); g.drawLine(0, zeroY, width, zeroY); g.drawLine(zeroX, 0, zeroX, height); g.setColor(Color.lightGray); for(int i=zeroY+yGridDiv; i<height; i+=yGridDiv) { g.drawLine(0, i, width, i); } for(int i=zeroY-yGridDiv; i>=0; i-=yGridDiv) { g.drawLine(0, i, width, i); } for(int i=zeroX+xGridDiv; i<width; i+=xGridDiv) { g.drawLine(i, 0, i, height); } for(int i=zeroX-xGridDiv; i>=0; i-=xGridDiv) { g.drawLine(i, 0, i, height); } // declare the axis label fonts // Font currentFont = getFont(); Font newFont = new Font(currentFont.getName(), currentFont.getStyle(), 10); // set the asix label font and color // g.setFont(newFont); g.setColor(Color.black); // initialize the labels // String xmin = new String("" + this.xmin); String xmax = new String("" + this.xmax); String ymin = new String("" + this.ymin); String ymax = new String("" + this.ymax); // add the labels to the x-axis and y-axis // g.drawString(ymax, zeroX+3, 12); g.drawString(ymin, zeroX+3, height-3); g.drawString(xmin, 3, zeroY+12); g.drawString(xmax, width-24, zeroY+12); } /** * method paints the selection area * * @@param Graphics g: graphics paint object * */ public void paintComponent(Graphics g) { super.paintComponent(g); // local variables // DrawGrid(g); // draw all input points // for ( int i=0; i < output_points_d.size(); i++ ) { Vector points = (Vector)output_points_d.get(i); int type = ((Integer)type_d.get(i)).intValue(); Color color = (Color)color_d.get(i); Classify.drawPoints( g, Classify.input_points_d.convertMyPoints(points, getWidth(), getHeight(), getDisplayScale()), type, color); } } /** * method clears the output points * */ public void clear() { output_points_d.clear(); color_d.clear(); type_d.clear(); } /** * get the current DisplayScale * * @@return DisplayScale of Object * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -