📄 whatifplot.java
字号:
double height = yRange[1] - yRange[0];
double newWidth = (xRange[1] - xRange[0])*PLOT_ZOOM_FACTOR;
double newHeight = (yRange[1] - yRange[0])*PLOT_ZOOM_FACTOR;
double newXMin = xRange[0] + (width - newWidth)/2;
//The next 7 lines check that the new range is valid
if (newXMin < XBounds[0]) newXMin = XBounds[0];
double newXMax = xRange[0] + (width - newWidth)/2 + newWidth;
if (newXMax > XBounds[1]) newXMax = XBounds[1];
double newYMin = yRange[0] + (height - newHeight)/2;
if (newYMin < YBounds[0]) newYMin = YBounds[0];
double newYMax = yRange[0] + (height - newHeight)/2 + newHeight;
if (newYMax > YBounds[1]) newYMax = YBounds[1];
setXRange(newXMin,newXMax);
setYRange(newYMin,newYMax);
WhatIfPlot.this.repaint();
}
});
zoomOut.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];
double height = yRange[1] - yRange[0];
double newWidth = (xRange[1] - xRange[0])/PLOT_ZOOM_FACTOR;
double newHeight = (yRange[1] - yRange[0])/PLOT_ZOOM_FACTOR;
double newXMin = xRange[0] + (width - newWidth)/2;
//The next 7 lines check that the new range is valid
if (newXMin < XBounds[0]) newXMin = XBounds[0];
double newXMax = xRange[0] + (width - newWidth)/2 + newWidth;
if (newXMax > XBounds[1]) newXMax = XBounds[1];
double newYMin = yRange[0] + (height - newHeight)/2;
if (newYMin < YBounds[0]) newYMin = YBounds[0];
double newYMax = yRange[0] + (height - newHeight)/2 + newHeight;
if (newYMax > YBounds[1]) newYMax = YBounds[1];
setXRange(newXMin,newXMax);
setYRange(newYMin,newYMax);
WhatIfPlot.this.repaint();
}
});
saveAs.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
PlotImagesFileFilter PNGfilter = new PlotImagesFileFilter(".png","Portable Network Graphics images");
PlotImagesFileFilter EPSfilter = new PlotImagesFileFilter(".eps","Encapsulated Post Script images");
PlotImagesFileChooser fileChooser = new PlotImagesFileChooser(PNGfilter);
fileChooser.setFileFilter(PNGfilter);
fileChooser.addChoosableFileFilter(EPSfilter);
int r = fileChooser.showSaveDialog(WhatIfPlot.this);
if (r == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (fileChooser.getFileFilter().equals(EPSfilter)) {
WhatIfPlot plot = WhatIfPlot.this;
try {
FileOutputStream fileStream = new FileOutputStream(file);
plot.export(fileStream);
fileStream.close();
}
catch (FileNotFoundException fnf) {
JOptionPane.showMessageDialog(fileChooser,"File not found","JMT - Error",JOptionPane.ERROR_MESSAGE);
}
catch (IOException ioe) {
JOptionPane.showMessageDialog(fileChooser,"I/O exception","JMT - Error",JOptionPane.ERROR_MESSAGE);
}
}
else {
WhatIfPlot plot = WhatIfPlot.this;
BufferedImage image = plot.exportImage();
try{
BufferedImage originalImage = convertType(image, BufferedImage.TYPE_INT_RGB);
ImageIO.write(originalImage,"png",file);
ImageIO.createImageOutputStream(file).close();
}
catch (IOException fnf) {
JOptionPane.showMessageDialog(null,"File not found");
}
}
}
}
});
}
BufferedImage convertType(BufferedImage src, int targetType) {
if (src.getType() == targetType)
return src;
BufferedImage tgt = new BufferedImage(src.getWidth(), src.getHeight(), targetType);
Graphics2D g = tgt.createGraphics();
g.drawRenderedImage(src, null);
g.dispose();
return tgt;
}
}
/**
* Custom file chooser class
*/
protected static class PlotImagesFileChooser extends JFileChooser {
protected PlotImagesFileFilter defaultFilter;
/**
* Creates a File chooser in the appropriate directory user deafault.
* @param defaultFilter default file filter
*/
public PlotImagesFileChooser(PlotImagesFileFilter defaultFilter) {
super(new File(System.getProperty("user.dir")));
this.defaultFilter = defaultFilter;
}
/**
* Overrides default method to provide a warning if saving over an existing file
*/
public void approveSelection() {
// Gets the choosed file name
String name = getSelectedFile().getName();
String parent = getSelectedFile().getParent();
if (getDialogType() == OPEN_DIALOG) {
super.approveSelection();
}
if (getDialogType() == SAVE_DIALOG) {
PlotImagesFileFilter used = ((PlotImagesFileFilter)this.getFileFilter());
if (!name.toLowerCase().endsWith(used.getExtension())) {
name = name + used.getExtension();
setSelectedFile(new File(parent, name));
}
if (getSelectedFile().exists()) {
int resultValue = JOptionPane.showConfirmDialog(this,
"<html>File <font color=#0000ff>" + name + "</font> already exists in this folder.<br>Do you want to replace it?</html>",
"JMT - Warning",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (resultValue == JOptionPane.OK_OPTION) {
getSelectedFile().delete();
super.approveSelection();
}
} else {
super.approveSelection();
}
}
}
}
/**
* Inner class used to create simple file filters with only extension check
*/
protected static class PlotImagesFileFilter extends javax.swing.filechooser.FileFilter {
private String extension, description;
/**
* Creates a new filefilter with specified extension and description
* @param extension extension of this filter (for example ".jmt")
* @param description description of this filter
*/
public PlotImagesFileFilter(String extension, String description) {
this.extension = extension;
this.description = description;
}
/**
* Whether the given file is accepted by this filter.
*/
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return name.endsWith(extension) || f.isDirectory();
}
/**
* The description of this filter
* @see javax.swing.filechooser.FileView#getName
*/
public String getDescription() {
return description + " (*" + extension + ")";
}
/**
* Gets extension of this filter
* @return extension of this filter
*/
public String getExtension() {
return extension;
}
}
// ----------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -