📄 matrixpanel.java
字号:
setPercent(); setupAttribLists(); m_rseed.setText("1"); origDist.setSelected(true); initInternalFields(); m_cp.setInstances(m_data); m_cp.setCindex(m_classIndex); m_updateBt.doClick(); } /** Main method for testing this class */ public static void main(String [] args) { final JFrame jf = new JFrame("Weka Knowledge Explorer: MatrixPanel"); final JButton setBt = new JButton("Set Instances"); Instances data = null; try { if(args.length==1) data = new Instances( new BufferedReader( new FileReader(args[0])) ); else { System.out.println("Usage: MatrixPanel <arff file>"); System.exit(-1); } } catch(IOException ex) { ex.printStackTrace(); System.exit(-1); } final MatrixPanel mp = new MatrixPanel(); mp.setInstances(data); setBt.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(new java.io.File(System.getProperty("user.dir"))); ExtensionFileFilter myfilter = new ExtensionFileFilter("arff", "Arff data files"); chooser.setFileFilter(myfilter); int returnVal = chooser.showOpenDialog(jf); if(returnVal == JFileChooser.APPROVE_OPTION) { try{ System.out.println("You chose to open this file: " +chooser.getSelectedFile().getName()); Instances in = new Instances ( new FileReader(chooser.getSelectedFile().getAbsolutePath()) ); mp.setInstances(in); } catch(Exception ex) { ex.printStackTrace(); } } } }); //System.out.println("Loaded: "+args[0]+"\nRelation: "+data.relationName()+"\nAttributes: "+data.numAttributes()); //System.out.println("The attributes are: "); //for(int i=0; i<data.numAttributes(); i++) // System.out.println(data.attribute(i).name()); //RepaintManager.currentManager(jf.getRootPane()).setDoubleBufferingEnabled(false); jf.getContentPane().setLayout( new BorderLayout() ); jf.getContentPane().add(mp, BorderLayout.CENTER); jf.getContentPane().add(setBt, BorderLayout.SOUTH); jf.getContentPane().setFont( new java.awt.Font( "SansSerif", java.awt.Font.PLAIN, 11) ); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(800, 600); jf.show(); jf.repaint(); } /** Internal class responsible for displaying the actual matrix Requires the internal data fields of the parent class to be properly initialized before being created */ private class Plot extends JPanel implements MouseMotionListener, MouseListener { int extpad=3, intpad=4, cellSize=100, cellRange=100, lastx=0, lasty=0, jitter=0; Color fontColor = new Color(98, 101, 156); java.awt.Rectangle r; java.awt.FontMetrics fm = this.getFontMetrics(this.getFont()); int lastxpos, lastypos; /** Constructor */ public Plot() { super(); this.setToolTipText("blah"); this.addMouseMotionListener( this ); this.addMouseListener( this ); initialize(); } /** Initializes the internal fields */ public void initialize() { lastxpos = lastypos = 0; cellRange = cellSize; cellSize = cellRange + 2*intpad; } public void mouseMoved(MouseEvent e) { Graphics g = this.getGraphics(); int xpos=100+extpad, ypos=extpad+2*fm.getHeight(); for(int j=m_selectedAttribs.length-1; j>=0; j--) { for(int i=0; i<m_selectedAttribs.length; i++) { if(e.getX()>=xpos && e.getX()<=xpos+cellSize+extpad) if(e.getY()>=ypos && e.getY()<=ypos+cellSize+extpad) { if(xpos!=lastxpos || ypos!=lastypos) { g.setColor( Color.red ); g.drawRect(xpos-1, ypos-1, cellSize+1, cellSize+1); if(lastxpos!=0 && lastypos!=0) { g.setColor( this.getBackground().darker() ); g.drawRect(lastxpos-1, lastypos-1, cellSize+1, cellSize+1); } lastxpos = xpos; lastypos = ypos; } return; } xpos+=cellSize+extpad; } xpos=100+extpad; ypos+=cellSize+extpad; } if(lastxpos!=0 && lastypos!=0) { g.setColor( this.getBackground().darker() ); g.drawRect(lastxpos-1, lastypos-1, cellSize+1, cellSize+1); } lastxpos=lastypos=0; } public void mouseDragged(MouseEvent e){ } public void mouseClicked(MouseEvent e) { int i=0, j=0, found=0; int xpos=100+extpad, ypos=extpad+2*fm.getHeight(); for(j=m_selectedAttribs.length-1; j>=0; j--) { for(i=0; i<m_selectedAttribs.length; i++) { if(e.getX()>=xpos && e.getX()<=xpos+cellSize+extpad) if(e.getY()>=ypos && e.getY()<=ypos+cellSize+extpad) { found=1; break; } xpos+=cellSize+extpad; } if(found==1) break; xpos=100+extpad; ypos+=cellSize+extpad; } if(found==0) return; JFrame jf = new JFrame("Weka Knowledge Explorer: Visualizing "+m_data.relationName() ); VisualizePanel vp = new VisualizePanel(); try { PlotData2D pd = new PlotData2D(m_data); pd.setPlotName("Master Plot"); vp.setMasterPlot(pd); //System.out.println("x: "+i+" y: "+j); vp.setXIndex(i); vp.setYIndex(j); vp.m_ColourCombo.setSelectedIndex( m_classIndex ); } catch(Exception ex) { ex.printStackTrace(); } jf.getContentPane().add(vp); jf.setSize(800,600); jf.show(); } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } /** sets the new jitter value for the plots */ public void setJitter(int newjitter) { jitter = newjitter; } /** sets the new size for the plots */ public void setCellSize(int newCellSize) { cellSize = newCellSize; initialize(); } /** Returns the X and Y attributes of the plot the mouse is currently on */ public String getToolTipText(MouseEvent event) { int xpos=100+extpad, ypos=extpad+2*fm.getHeight(); for(int j=m_selectedAttribs.length-1; j>=0; j--) { for(int i=0; i<m_selectedAttribs.length; i++) { if(event.getX()>=xpos && event.getX()<=xpos+cellSize+extpad) if(event.getY()>=ypos && event.getY()<=ypos+cellSize+extpad) return("X: "+m_data.attribute(m_selectedAttribs[i]).name()+ " Y: "+m_data.attribute(m_selectedAttribs[j]).name()+ " (click to enlarge)"); xpos+=cellSize+extpad; } xpos=100+extpad; ypos+=cellSize+extpad; } return ("Matrix Panel"); } /** Paints a single Plot at xpos, ypos. and xattrib and yattrib on X and Y axes */ public void paintGraph(Graphics g, int xattrib, int yattrib, int xpos, int ypos) { int x, y; g.setColor( this.getBackground().darker() ); g.drawRect(xpos-1, ypos-1, cellSize+1, cellSize+1); g.setColor(Color.white); g.fillRect(xpos, ypos, cellSize, cellSize); for(int i=0; i<m_points.length; i++) { if( !(m_missing[i][yattrib] || m_missing[i][xattrib]) ) { if(m_type[0][m_classIndex]==0) if(m_missing[i][m_classIndex]) g.setColor(m_defaultColors[m_defaultColors.length-1]); else g.setColor( new Color(m_pointColors[i],150,(255-m_pointColors[i]))); else g.setColor((Color)m_colorList.elementAt(m_pointColors[i])); if(m_points[i][xattrib]+jitterVals[i][0]<0 || m_points[i][xattrib]+jitterVals[i][0]>cellRange) if(cellRange-m_points[i][yattrib]+jitterVals[i][1]<0 || cellRange-m_points[i][yattrib]+jitterVals[i][1]>cellRange) { //both x and y out of range don't add jitter x=intpad+m_points[i][xattrib]; y=intpad+(cellRange - m_points[i][yattrib]); } else { //only x out of range x=intpad+m_points[i][xattrib]; y=intpad+(cellRange - m_points[i][yattrib])+jitterVals[i][1]; } else if(cellRange-m_points[i][yattrib]+jitterVals[i][1]<0 || cellRange-m_points[i][yattrib]+jitterVals[i][1]>cellRange) { //only y out of range x=intpad+m_points[i][xattrib]+jitterVals[i][0]; y=intpad+(cellRange - m_points[i][yattrib]); } else { //none out of range x=intpad+m_points[i][xattrib]+jitterVals[i][0]; y=intpad+(cellRange - m_points[i][yattrib])+jitterVals[i][1]; } g.drawLine(x+xpos, y+ypos, x+xpos, y+ypos); } } g.setColor( fontColor ); } /** Paints the matrix of plots in the current visible region */ public void paintME(Graphics g) { r = g.getClipBounds(); g.setColor( this.getBackground() ); g.fillRect(r.x, r.y, r.width, r.height); g.setColor( fontColor ); int xpos = 0, ypos = 0, attribWidth=0; xpos = extpad; ypos=extpad+fm.getHeight(); if(r.y < (ypos+cellSize+extpad)) { g.drawString("Plot Matrix", xpos, ypos); xpos += 100; for(int i=0; i<m_selectedAttribs.length; i++) { if( xpos+cellSize < r.x) { xpos += cellSize+extpad; continue; } else if(xpos > r.x+r.width) { break; } else { attribWidth = fm.stringWidth(m_data.attribute(m_selectedAttribs[i]).name()); g.drawString(m_data.attribute(m_selectedAttribs[i]).name(), (attribWidth<cellSize) ? (xpos + (cellSize/2 - attribWidth/2)):xpos, ypos); } xpos += cellSize+extpad; } } xpos = extpad; ypos += fm.getHeight(); for(int j=m_selectedAttribs.length-1; j>=0; j--) { if( ypos+cellSize < r.y ) { ypos += cellSize+extpad; continue; } else if( ypos > r.y+r.height ) break; else { if(r.x < (xpos+cellSize+extpad)) {g.drawString(m_data.attribute(m_selectedAttribs[j]).name(), xpos+extpad, ypos+cellSize/2); } xpos += 100; for(int i=0; i<m_selectedAttribs.length; i++) { if( xpos+cellSize < r.x) { xpos += cellSize+extpad; continue; } else if(xpos > r.x+r.width) break; else paintGraph(g, m_selectedAttribs[i], m_selectedAttribs[j], xpos, ypos); xpos += cellSize+extpad; } } xpos = extpad; ypos += cellSize+extpad; } } /** paints this JPanel (PlotsPanel) */ public void paintComponent(Graphics g) { paintME(g); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -