📄 whatifplot.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.exact.utils;
import ptolemy.plot.Plot;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.util.Vector;
import java.util.Iterator;
/**
* <p>Title: What-If analysis Plot</p>
* <p>Description: This class provides a customization of Ptolemy Plot
* object used for JMVA what-if analysis.</p>
*
* @author Bertoli Marco
* Date: 1-giu-2006
* Time: 15.44.43
*/
public class WhatIfPlot extends Plot {
// Zoom factor for zoom-in / zoom-out
public static final double PLOT_ZOOM_FACTOR = 0.5;
// Values for x-axis
private double[] xAxis;
// Popup menu
private PlotPopupMenu popup = new PlotPopupMenu();
// Rescale listeners
private Vector listeners= new Vector();
public WhatIfPlot(double[] xAxis) {
super();
this.xAxis = xAxis;
// Adds popup menu
this.addMouseListener(new MouseAdapter() {
/**
* Invoked when the mouse has been clicked on a component.
*/
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
popup.show(WhatIfPlot.this, e.getX(), e.getY());
}
});
}
/**
* Draw given values on selected line with selected caption
* @param num number of dataset to be drawn
* @param values values to be drawn
* @throws IllegalArgumentException if dataset size does not match with x-axis values
*/
public void draw(int num, double[] values) throws IllegalArgumentException{
if (values.length != xAxis.length)
throw new IllegalArgumentException("Length of provided dataset does not match x-axis points.");
// Clears target dataset
clear(num);
// Adds values
for (int i=0; i<xAxis.length; i++)
addPoint(num, xAxis[i], values[i], true);
}
// --- Callbacks for rescale --------------------------------------------------------------
/**
* Adds a rescale listener that is notified each time graph is rescaled.
* @param listener listener to be added
*/
public void addRescaleListener(RescaleListener listener) {
listeners.add(listener);
}
/**
* Removes a rescale listener
* @param listener listener to be removed
*/
public void removeRescaleListener(RescaleListener listener) {
listeners.remove(listener);
}
/**
* Called each time a rescale event occurrs
*/
protected void fireRescaleEvent() {
Iterator i = listeners.iterator();
while (i.hasNext())
((RescaleListener)i.next()).Rescaled();
}
/**
* Interface used for rescaling callbacks
*/
public interface RescaleListener {
/**
* Called each time graph is rescaled
*/
public void Rescaled();
}
/**
* Overrides default method to add firing of rescale change events
*/
public synchronized void setXRange(double v, double v1) {
super.setXRange(v, v1);
fireRescaleEvent();
}
/**
* Overrides default method to add firing of rescale change events
* Avoid problems with machine precision on constant measures too.
*/
public synchronized void setYRange(double v, double v1) {
// Avoid rescaling to a too small scale (for machine precision problems)
if (Math.abs(v - v1) > 1e-8)
super.setYRange(v, v1);
else {
double mean = (v + v1) / 2;
super.setYRange(mean, mean);
}
fireRescaleEvent();
}
// ----------------------------------------------------------------------------------------
// --- Methods for popup menu -------------------------------------------------------------
/**
* A simple JPopupMenu used to manage operations on plot. It gives the
* choice to zoom in and out on the plot, restore original view and save plot to
* images (in EPS or PNG format)
*/
protected class PlotPopupMenu extends JPopupMenu {
public JMenuItem restore;
public JMenuItem zoomIn;
public JMenuItem zoomOut;
public JMenuItem saveAs;
public PlotPopupMenu () {
restore = new JMenuItem("Original view");
zoomIn = new JMenuItem("Zoom in");
zoomOut = new JMenuItem("Zoom out");
saveAs = new JMenuItem("Save as...");
this.add(restore);
this.add(zoomIn);
this.add(zoomOut);
this.addSeparator();
this.add(saveAs);
addListeners();
}
public void addListeners() {
restore.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
fillPlot();
}
});
zoomIn.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// Gets maximum and minimum values
double[] XBounds = getXAutoRange();
double[] YBounds = getYAutoRange();
double[] xRange = getXRange();
double[] yRange = getYRange();
double width = xRange[1] - xRange[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -