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

📄 classpanel.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *    ClassPanel.java
 *    Copyright (C) 2000 Mark Hall, Malcolm Ware
 *
 */


package weka.gui.visualize;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import weka.core.FastVector;
import weka.core.Instances;
import weka.core.Utils;
import eti.bi.common.Locale.Resource;
/**
 * This panel displays coloured labels for nominal attributes and a spectrum
 * for numeric attributes. It can also be told to colour on the basis
 * of an array of doubles (this can be useful for displaying coloured labels
 * that correspond to a clusterers predictions).
 *
 * @author Mark Hall (mhall@cs.waikato.ac.nz)
 * @author Malcolm Ware (mfw4@cs.waikato.ac.nz)
 * @version $Revision$
 */
public class ClassPanel extends JPanel {
    
  /** True when the panel has been enabled (ie after 
      setNumeric or setNominal has been called */
  private boolean m_isEnabled = false;

  /** True if the colouring attribute is numeric */
  private boolean m_isNumeric = false;
    
  /** The height of the spectrum for numeric class */
  private final int m_spectrumHeight = 5;

  /**  The maximum value for the colouring attribute */
  private double m_maxC;
    
  /** The minimum value for the colouring attribute */
  private double m_minC;

  /** The size of the ticks */
  private final int m_tickSize = 5;

  /** Font metrics */
  private FontMetrics m_labelMetrics = null;

  /** The font used in labeling */
  private Font m_labelFont = null;

  /** The amount of space to leave either side of the legend */ 
  private int m_HorizontalPad=0;

  /** The precision with which to display real values */
  private int m_precisionC;

  /** Field width for numeric values */
  private int m_fieldWidthC;

  /** The old width. */
  private int m_oldWidth = -9000;
    
  /** Instances being plotted */
  private Instances m_Instances=null;

  /** Index of the colouring attribute */
  private int m_cIndex;

  /** the list of colours to use for colouring nominal attribute labels */
  private FastVector m_colorList;

  /** An optional list of Components that use the colour list
      maintained by this class. If the user changes a colour
      using the colour chooser, then these components need to
      be repainted in order to display the change */
  private FastVector m_Repainters = new FastVector();

  /** An optional list of listeners who want to know when a colour
      changes. Listeners are notified via an ActionEvent */
  private FastVector m_ColourChangeListeners = new FastVector();

  /** 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};

  /** Inner Inner class used to create labels for nominal attributes
   * so that there color can be changed.
   */
  private class NomLabel extends JLabel {
    private int m_index = 0;

    /** 
     * Creates a label with its name and class index value.
     * @param name The name of the nominal class value.
     * @param id The index value for that class value.
     */
    public NomLabel(String name, int id) {
      super(name);
      m_index = id;

      this.addMouseListener(new MouseAdapter() {
	  public void mouseClicked(MouseEvent e) {
	      
	    if ((e.getModifiers() & e.BUTTON1_MASK) == e.BUTTON1_MASK) {
	      Color tmp = JColorChooser.showDialog
		(ClassPanel.this, Resource.srcStr("SelectnewColor"), 
		 (Color)m_colorList.elementAt(m_index));
		
	      if (tmp != null) {
		m_colorList.setElementAt(tmp, m_index);
		m_oldWidth = -9000;
		ClassPanel.this.repaint();
		if (m_Repainters.size() > 0) {
		  for (int i=0;i<m_Repainters.size();i++) {
		    ((Component)(m_Repainters.elementAt(i))).repaint();
		  }
		}
		
		if (m_ColourChangeListeners.size() > 0) {
		  for (int i = 0; i < m_ColourChangeListeners.size(); i++) {
		    ((ActionListener)(m_ColourChangeListeners.elementAt(i))).
		      actionPerformed(new ActionEvent(this, 0, ""));
		  }
		}
	      }
	    }
	  }
	});
    }
  }

  public ClassPanel() {
    /** Set up some default colours */
    m_colorList = new FastVector(10);
    for (int noa = m_colorList.size(); noa < 10; noa++) {
      Color pc = m_DefaultColors[noa % 10];
      int ija =  noa / 10;
      ija *= 2; 
      for (int j=0;j<ija;j++) {
	pc = pc.darker();
      }
	
      m_colorList.addElement(pc);
    }
  }

  /**
   * Adds a component that will need to be repainted if the user
   * changes the colour of a label.
   * @param c the component to be repainted
   */
  public void addRepaintNotify(Component c) {
    m_Repainters.addElement(c);
  }

  /**
   * Add an action listener that will be notified if the user changes the
   * colour of a label
   *
   * @param a an <code>ActionListener</code> value
   */
  public void addActionListener(ActionListener a) {
    m_ColourChangeListeners.addElement(a);
  }

  /**
   * Set up fonts and font metrics
   * @param gx the graphics context
   */
  private void setFonts(Graphics gx) {
    if (m_labelMetrics == null) {
      m_labelFont = new Font("Monospaced", Font.PLAIN, 12);
      m_labelMetrics = gx.getFontMetrics(m_labelFont);
      int hf = m_labelMetrics.getAscent();
      if (this.getHeight() < (3*hf)) {
	m_labelFont = new Font("Monospaced",Font.PLAIN,11);
	m_labelMetrics = gx.getFontMetrics(m_labelFont);
      }
    }
    gx.setFont(m_labelFont);
  }
    
  /**
   * Enables the panel
   * @param e true to enable the panel
   */
  public void setOn(boolean e) {
    m_isEnabled = e;
  }

  /**
   * Set the instances.
   * @param insts the instances
   */
  public void setInstances(Instances insts) {
    m_Instances = insts;
  }

  /**
   * Set the index of the attribute to display coloured labels for
   * @param cIndex the index of the attribute to display coloured labels for
   */
  public void setCindex(int cIndex) {
    if (m_Instances.numAttributes() > 0) {
      m_cIndex = cIndex;
      if (m_Instances.attribute(m_cIndex).isNumeric()) {
	setNumeric();
      } else {
	if (m_Instances.attribute(m_cIndex).numValues() > m_colorList.size()) {
	  extendColourMap();
	}
	setNominal();
      }
    }
  }

  /**
   * Extends the list of colours if a new attribute with more values than
   * the previous one is chosen
   */
  private void extendColourMap() {
    if (m_Instances.attribute(m_cIndex).isNominal()) {
      for (int i = m_colorList.size(); 
	   i < m_Instances.attribute(m_cIndex).numValues();
	   i++) {
	Color pc = m_DefaultColors[i % 10];
	int ija =  i / 10;
	ija *= 2; 
	for (int j=0;j<ija;j++) {
	  pc = pc.brighter();
	}
	
	m_colorList.addElement(pc);
      }
    }
  }

  /**
   * Set a list of colours to use for colouring labels
   * @param a list containing java.awt.Colors
   */
  public void setColours(FastVector cols) {
    m_colorList = cols;
  }
    
  /**
   * Sets the legend to be for a nominal variable
   * @param plotInstances the instances currently being plotted
   * @param cIndex the index of the colouring attribute
   */
  protected void setNominal() {
    m_isNumeric = false;
    m_HorizontalPad = 0;
    setOn(true);
    m_oldWidth = -9000;
     
    this.repaint();
  }

  /**
   * Sets the legend to be for a numeric variable
   * @param mxC the maximum value of the colouring attribute
   * @param mnC the minimum value of the colouring attribute
   */
  protected void setNumeric() {
    m_isNumeric = true;
    /*      m_maxC = mxC;
	    m_minC = mnC; */

    double min=Double.POSITIVE_INFINITY;
    double max=Double.NEGATIVE_INFINITY;
    double value;

    for (int i=0;i<m_Instances.numInstances();i++) {
      if (!m_Instances.instance(i).isMissing(m_cIndex)) {
	value = m_Instances.instance(i).value(m_cIndex);
	if (value < min) {
	  min = value;
	}
	if (value > max) {
	  max = value;
	}
      }
    }
     
    // handle case where all values are missing
    if (min == Double.POSITIVE_INFINITY) min = max = 0.0;

    m_minC = min; m_maxC = max;

    int whole = (int)Math.abs(m_maxC);
    double decimal = Math.abs(m_maxC) - whole;
    int nondecimal;
    nondecimal = (whole > 0) 
      ? (int)(Math.log(whole) / Math.log(10))
      : 1;
    
    m_precisionC = (decimal > 0) 
      ? (int)Math.abs(((Math.log(Math.abs(m_maxC)) / 
				      Math.log(10))))+2
      : 1;
    if (m_precisionC > VisualizeUtils.MAX_PRECISION) {
      m_precisionC = 1;
    }

    String maxStringC = Utils.doubleToString(m_maxC,
					     nondecimal+1+m_precisionC
					     ,m_precisionC);
    if (m_labelMetrics != null) {
      m_HorizontalPad = m_labelMetrics.stringWidth(maxStringC);
    }

    whole = (int)Math.abs(m_minC);
    decimal = Math.abs(m_minC) - whole;
    nondecimal = (whole > 0) 
      ? (int)(Math.log(whole) / Math.log(10))
      : 1;
    
     m_precisionC = (decimal > 0) 
       ? (int)Math.abs(((Math.log(Math.abs(m_minC)) / 
				      Math.log(10))))+2
      : 1;
     if (m_precisionC > VisualizeUtils.MAX_PRECISION) {
       m_precisionC = 1;
     }
    

⌨️ 快捷键说明

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