📄 plotterpanel.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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.Statistics;import edu.udo.cs.yale.StatisticsListener;import javax.swing.JPanel;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.BorderFactory;import javax.swing.JList;import javax.swing.JScrollPane;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.Insets;import java.awt.BorderLayout;import java.awt.event.ItemListener;import java.awt.event.ItemEvent;import java.awt.geom.AffineTransform;import java.awt.geom.Point2D;import java.awt.event.MouseMotionListener;import java.awt.event.MouseEvent;import javax.swing.event.ListSelectionListener;import javax.swing.event.ListSelectionEvent;import java.awt.Stroke;import java.awt.BasicStroke;import javax.swing.Icon;import javax.swing.ListCellRenderer;import javax.swing.JButton;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Component;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.DecimalFormat;public class PlotterPanel extends JPanel implements MouseMotionListener { 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 JComboBox xAxisCombo = new JComboBox(); private JComboBox yAxisCombo = new JComboBox(); private JList zAxisList = new JList(); public PlotterPanel(Statistics statistics) { super(new BorderLayout()); this.plotter = new Plotter(statistics); this.statistics = statistics; plotter.addMouseMotionListener(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); label = new JLabel("Z-Axis"); gridBag.setConstraints(label, c); axesSelectionPanel.add(label); JScrollPane listScrollPane = new JScrollPane(zAxisList); c.weighty = 1.0; gridBag.setConstraints(listScrollPane, c); axesSelectionPanel.add(listScrollPane); c.weighty = 0.0; gridBag.setConstraints(coordinatesLabel, c); axesSelectionPanel.add(coordinatesLabel); 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(statistics.SAVE_ACTION); gridBag.setConstraints(saveButton, c); axesSelectionPanel.add(saveButton); 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())); } catch (java.awt.geom.NoninvertibleTransformException ex) { ex.printStackTrace(); } } } public void mouseDragged(MouseEvent e) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -