📄 matrixpanel.java
字号:
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.setVisible(true); 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; java.awt.Rectangle r; java.awt.FontMetrics fm; int lastxpos, lastypos; JPanel jPlColHeader, jPlRowHeader; /** 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; jPlColHeader = new JPanel() { java.awt.Rectangle r; public void paint(Graphics g) { r = g.getClipBounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); g.setFont( f ); fm = g.getFontMetrics(); int xpos = 0, ypos = 0, attribWidth=0; g.setColor(fontColor); xpos = extpad; ypos=extpad+fm.getHeight(); 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; } fm = null; r=null; } public Dimension getPreferredSize() { fm = this.getFontMetrics(this.getFont()); return new Dimension( m_selectedAttribs.length*(cellSize+extpad), 2*extpad + fm.getHeight() ); } }; jPlRowHeader = new JPanel() { java.awt.Rectangle r; public void paint(Graphics g) { r = g.getClipBounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); g.setFont( f ); fm = g.getFontMetrics(); int xpos = 0, ypos = 0; g.setColor(fontColor); xpos = extpad; ypos=extpad; 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 { g.drawString(m_data.attribute(m_selectedAttribs[j]).name(), xpos+extpad, ypos+cellSize/2); } xpos = extpad; ypos += cellSize+extpad; } r=null; } public Dimension getPreferredSize() { return new Dimension( 100+extpad, m_selectedAttribs.length*(cellSize+extpad) ); } }; jPlColHeader.setFont(f); jPlRowHeader.setFont(f); this.setFont(f); } public JPanel getRowHeader() { return jPlRowHeader; } public JPanel getColHeader() { return jPlColHeader; } public void mouseMoved(MouseEvent e) { Graphics g = this.getGraphics(); int xpos=extpad, ypos=extpad; 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=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=extpad, ypos=extpad; 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=extpad; ypos+=cellSize+extpad; } if(found==0) return; JFrame jf = new JFrame("Weka 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(m_selectedAttribs[i]); vp.setYIndex(m_selectedAttribs[j]); vp.m_ColourCombo.setSelectedIndex( m_classIndex ); } catch(Exception ex) { ex.printStackTrace(); } jf.getContentPane().add(vp); jf.setSize(800,600); jf.setVisible(true); } 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=extpad, ypos=extpad; 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=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().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]==0) if(m_missing[i][m_missing[0].length-1]) 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]; } if(datapointSize==1) g.drawLine(x+xpos, y+ypos, x+xpos, y+ypos); else g.drawOval(x+xpos-datapointSize/2, y+ypos-datapointSize/2, datapointSize, datapointSize); } } 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; xpos = extpad; ypos=extpad; 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 { 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, i, j, xpos, ypos); //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 + -