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

📄 plotterpanel.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.gui;

import edu.udo.cs.yale.Yale;
import edu.udo.cs.yale.ObjectVisualizer;
import edu.udo.cs.yale.Statistics;
import edu.udo.cs.yale.StatisticsListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.text.DecimalFormat;

public class PlotterPanel extends JPanel implements MouseMotionListener, MouseListener {

    private static  Icon[] LINE_STYLE_ICONS;

    static {
	LINE_STYLE_ICONS = new LineStyleIcon[Plotter.LINE_STYLES.length];
	for (int i = 0; i < LINE_STYLE_ICONS.length; i++) {
	    LINE_STYLE_ICONS[i] = new LineStyleIcon(i);
	}
    }

    private static class LineStyleIcon implements Icon {
	private int index;
	private LineStyleIcon(int index) {
	    this.index = index;
	}
	public int getIconWidth() { return 26; }
	public int getIconHeight() { return 1; }
	public void paintIcon(Component c, Graphics g, int x, int y) {
	    Plotter.LINE_STYLES[index].set((Graphics2D)g);
	    g.drawLine(x+2, y, x+26, y);
	}
    }

    

    private static class LineStyleCellRenderer extends JLabel implements ListCellRenderer {
	public Component getListCellRendererComponent(JList list, 
						      Object value, 
						      int index, 
						      boolean isSelected, 
						      boolean cellHasFocus) {
	    String s = value.toString();
	    setText(s);
	    setIcon(LINE_STYLE_ICONS[index%LINE_STYLE_ICONS.length]);
	    if (isSelected) {
		setBackground(list.getSelectionBackground());
		setForeground(list.getSelectionForeground());
	    }
	    else {
		setBackground(list.getBackground());
		setForeground(list.getForeground());
	    }
	    setEnabled(list.isEnabled());
	    setFont(list.getFont());
	    setOpaque(true);
	    return this;
	}
    }

    private Plotter plotter;
    private Statistics statistics;

    private JLabel      coordinatesLabel = new JLabel("                      ");
    private JLabel      zAxisLabel = new JLabel("Z-Axis");
    private JComboBox   xAxisCombo = new JComboBox();
    private JComboBox   yAxisCombo = new JComboBox();
    private JList       zAxisList = new JList();
    private JCheckBox   plot3DBox = new JCheckBox("3D plot", true);
    private JSlider     drawAmountSlider = new JSlider(1,100,100);

    private int pressX = -1;
    private int pressY = -1;

    public PlotterPanel(Statistics statistics) {
	super(new BorderLayout());
	this.plotter    = new Plotter(statistics);
	this.statistics = statistics;

	plotter.addMouseMotionListener(this);
	plotter.addMouseListener(this);

	add(plotter, BorderLayout.CENTER);
	coordinatesLabel.setBorder(BorderFactory.createEtchedBorder());
	coordinatesLabel.setFont(new Font("Monospaced", Font.PLAIN, coordinatesLabel.getFont().getSize()));

	init();

	GridBagLayout gridBag = new GridBagLayout();
	GridBagConstraints c = new GridBagConstraints();
	c.fill      = GridBagConstraints.BOTH;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.insets    = new Insets(2,2,2,2);

	JPanel axesSelectionPanel = new JPanel(gridBag);

	JLabel label = new JLabel("X-Axis");
	gridBag.setConstraints(label, c);
	axesSelectionPanel.add(label);
	gridBag.setConstraints(xAxisCombo, c);
	axesSelectionPanel.add(xAxisCombo);

	label = new JLabel("Y-Axis");
	gridBag.setConstraints(label, c);
	axesSelectionPanel.add(label);
	gridBag.setConstraints(yAxisCombo, c);
	axesSelectionPanel.add(yAxisCombo);

	gridBag.setConstraints(zAxisLabel, c);
	axesSelectionPanel.add(zAxisLabel);
	zAxisList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	JScrollPane listScrollPane = new JScrollPane(zAxisList);
	c.weighty = 1.0;
	gridBag.setConstraints(listScrollPane, c);
	axesSelectionPanel.add(listScrollPane);

	c.weighty = 0.0;
	plot3DBox.setToolTipText("Indicates if a 3D plot or a color plot should be used.");
	gridBag.setConstraints(plot3DBox, c);
	axesSelectionPanel.add(plot3DBox);
	plot3DBox.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    boolean selected = plot3DBox.isSelected();
		    if (selected) {
			zAxisList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			zAxisLabel.setText("Z-Axis");
		    } else {
			if (yAxisCombo.getSelectedIndex() == 0) {
			    zAxisList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			    zAxisLabel.setText("Z-Axis");
			} else {
			    zAxisLabel.setText("Color");
			    zAxisList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			}
		    }
		    plotter.set3DPlot(selected);
		    plotter.repaint();
		}
	    });	

	label = new JLabel("Draw amount");
	gridBag.setConstraints(label, c);
	axesSelectionPanel.add(label);
	gridBag.setConstraints(drawAmountSlider, c);
	axesSelectionPanel.add(drawAmountSlider);
	drawAmountSlider.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent e) {
		    plotter.setDrawAmount(drawAmountSlider.getValue());
		    plotter.repaint();		    
		}
	    });

	JButton optionsButton = new JButton("Options");
  	gridBag.setConstraints(optionsButton, c);
	axesSelectionPanel.add(optionsButton);
	optionsButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    plotter.showGNUPlotDialog();
		}
	    });
	

	JButton saveButton = new JButton(plotter.SAVE_ACTION);
  	gridBag.setConstraints(saveButton, c);
	axesSelectionPanel.add(saveButton);

  	gridBag.setConstraints(coordinatesLabel, c);
	axesSelectionPanel.add(coordinatesLabel);	

	axesSelectionPanel.setAlignmentX(LEFT_ALIGNMENT);

	add(axesSelectionPanel, BorderLayout.WEST);
    }

    private void init() {
	for (int i = 0; i < statistics.getNumberOfColumns(); i++) {
	    xAxisCombo.addItem(statistics.getColumnName(i));
	}
	if (xAxisCombo.getItemCount() == 0) xAxisCombo.addItem("            ");
	xAxisCombo.addItemListener(new ItemListener() {
		public void itemStateChanged(ItemEvent e) {
		    plotter.setXAxis(xAxisCombo.getSelectedIndex());
		    plotter.repaint();
		}
	    });

	yAxisCombo.addItem("None");
	for (int i = 0; i < statistics.getNumberOfColumns(); i++) {
	    yAxisCombo.addItem(statistics.getColumnName(i));
	}
	yAxisCombo.addItemListener(new ItemListener() {
  		public void itemStateChanged(ItemEvent e) {
  		    plotter.setYAxis(yAxisCombo.getSelectedIndex()-1);
  		    plotter.repaint();
  		}
	    });

	zAxisList = new JList(statistics.getColumnNames());
	zAxisList.setBorder(BorderFactory.createLoweredBevelBorder());
	zAxisList.setCellRenderer(new LineStyleCellRenderer());
	zAxisList.addListSelectionListener(new ListSelectionListener() {
		public void valueChanged(ListSelectionEvent e) {
		    for (int i = 0; i < zAxisList.getModel().getSize(); i++)
			plotter.plotColumn(i, zAxisList.isSelectedIndex(i));
		    plotter.repaint();
		}
	    });
    }


    public void mouseMoved(MouseEvent e) {
	AffineTransform transform       = plotter.getTransform();
	if (transform != null) {
	    try {
		Point2D p = transform.inverseTransform(e.getPoint(), null);
  		DecimalFormat format = new DecimalFormat(" 0.000E0;-0.000E0");
  		coordinatesLabel.setText(format.format(p.getX())+" , "+format.format(p.getY()));
  		if (plotter.setMousePosInDataSpace(e.getX(), e.getY()))
  		    plotter.repaint();

	    } catch (java.awt.geom.NoninvertibleTransformException ex) { 
		ex.printStackTrace();
	    }
	}
    }

    public void mouseDragged(MouseEvent e) {
	if ((Math.abs(e.getX() - pressX) > 5) && (Math.abs(e.getY() - pressY) > 5)) {
	    plotter.setDragBounds(Math.min(pressX, e.getX()), Math.min(pressY, e.getY()), 
				  Math.abs(pressX - e.getX()), Math.abs(pressY - e.getY()));
	    plotter.repaint();
	} else {
	    plotter.setDragBounds(-1, -1, -1, -1);
	    plotter.repaint();
	}
    }

    public void mouseClicked(MouseEvent e) {
	if (e.getButton() == e.BUTTON1) {
	    if (e.getClickCount() > 1) {
		Plotter.PlotterPoint point = plotter.getPlotterPointForPos(e.getX(), e.getY());
		if (point != null) {
		    String id = point.getId();
		    if (id != null) {
			ObjectVisualizer visualizer = Yale.getExperiment().getVisualizerForObject(id);
			visualizer.startVisualization(id);
		    }
		}
	    }
	} else if (e.getButton() == e.BUTTON3) {
	    plotter.setDrawRange(-1,-1,-1,-1);
	    pressX = -1;
	    pressY = -1;
	    plotter.setDragBounds(-1, -1, -1, -1);
	    plotter.repaint();
	}
    }

    public void mouseReleased(MouseEvent e) {
	if (e.getButton() == e.BUTTON1) {
	    if ((Math.abs(e.getX() - pressX) > 5) && (Math.abs(e.getY() - pressY) > 5)) {
		if ((pressX != -1) && (pressY != -1)) {
		    int releaseX = e.getX();
		    int releaseY = e.getY();
		    plotter.setDrawRange(Math.min(pressX, releaseX), Math.max(pressX, releaseX), 
					 Math.min(pressY, releaseY), Math.max(pressY, releaseY));
		}
	    }
	}
	plotter.setDragBounds(-1, -1, -1, -1);
	pressX = -1;
	pressY = -1;
	plotter.repaint();
    }

    public void mousePressed(MouseEvent e) {
	if (e.getButton() == e.BUTTON1) {
	    if (!plotter.is3DMode()) {
		pressX = e.getX();
		pressY = e.getY();
	    }
	}
    }


    public void mouseExited(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
}

⌨️ 快捷键说明

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