📄 viewplotframe.java
字号:
/*
*@(#)ViewPlotFrame.java 2.0 2005/05/14
*
*清华大学 精密仪器与机械学系
*范灿升 fancansheng@163.com
*/
package plot;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.*;
import function.Function;
import lib.Library;
import lib.DataFileFilter;
import plot.Plotting;
import plot.PlotPanel;
/**
*这个类用于显示数据拟合时的数据点及相关的拟合曲线,显示方式为新建一个JFrame窗口。
*@version 2.0, 2005/05/14
*@author 范灿升
*@see plot.Plotting
*@see plot.PlotPanel
*@see input.FitInput
*@see function.Function
*/
public class ViewPlotFrame extends JFrame implements ActionListener,MouseMotionListener
{
private double[] xData,yData;
private Function f;
private String resultString;
private int x=Library.PLOT_WIDTH/2;
private int h0=127;
private int gap=4;//JPanel与JFrame之间的空隙
private double xValue;
private float yValue;
private JButton computeButton;
private JButton saveImageButton;
private JTextField xText;
private JTextField yText;
private PlotPanel plotPanel;
/**
*x最小值
*/
public double xMin;
/**
*缩放比例,图上距离比实际距离,即double/int
*/
public double xScale;
/**
*x方向的最大最小值到绘图左边界、上边界的距离(不是图形边界)
*/
public double xSpace;
/**
*使用数据构造类。
*@param xData 数据点的x坐标
*@param yData 数据点的y坐标
*@param f 因变量与自变量之间的函数关系
*@param resultString 已经处理好的结果字符串
*/
public ViewPlotFrame(double[] xData,double[] yData,Function f,String resultString)
{
super("拟合结果分析");
this.xData=xData;
this.yData=yData;
this.f=f;
this.resultString=resultString;
}
/**
*显示拟合结果,绘制数据点及拟合后的曲线。
*/
public void showPlot()
{
Container plotContainer=getContentPane();
plotContainer.setLayout(new BorderLayout());
JPanel resultPanel=new JPanel(new FlowLayout(FlowLayout.LEFT),true);
plotPanel=new PlotPanel(xData,yData,f);
plotContainer.add(resultPanel,BorderLayout.NORTH);
plotContainer.add(plotPanel,BorderLayout.CENTER);
//添加数据显示面板和图形面板
JTextArea text=new JTextArea(resultString,5,40);
JScrollPane scrollPane=new JScrollPane(text);
resultPanel.add(scrollPane);
GridBagLayout gridBag=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
JPanel computePanel=new JPanel(gridBag);
JLabel xLabel=new JLabel("x",JLabel.CENTER);
JLabel yLabel=new JLabel("y",JLabel.CENTER);
xLabel.setFont(Library.font);
yLabel.setFont(Library.font);
xText=new JTextField(8+Library.TEXTFIELD_LENGTH);
yText=new JTextField(8+Library.TEXTFIELD_LENGTH);
xText.addActionListener(this);
yText.setEditable(false);
c.gridwidth=GridBagConstraints.RELATIVE;
gridBag.setConstraints(xLabel,c);
computePanel.add(xLabel);
c.gridwidth=GridBagConstraints.REMAINDER;
gridBag.setConstraints(xText,c);
computePanel.add(xText);
c.gridwidth=GridBagConstraints.RELATIVE;
gridBag.setConstraints(yLabel,c);
computePanel.add(yLabel);
c.gridwidth=GridBagConstraints.REMAINDER;
gridBag.setConstraints(yText,c);
computePanel.add(yText);
computeButton=new JButton("计算y值",new ImageIcon(Library.polyhedronIcont_Scaled));
computeButton.setFont(Library.font);
computeButton.addActionListener(this);
c.gridwidth=GridBagConstraints.RELATIVE;
gridBag.setConstraints(computeButton,c);
computePanel.add(computeButton);
saveImageButton=new JButton("保存图像",new ImageIcon(Library.aidIcon_Scaled));
saveImageButton.setFont(Library.font);
saveImageButton.addActionListener(this);
c.gridwidth=GridBagConstraints.REMAINDER;
gridBag.setConstraints(saveImageButton,c);
computePanel.add(saveImageButton);
resultPanel.add(computePanel);
addMouseMotionListener(this);
setIconImage(Library.polyhedronIcon.getImage());
setSize(Library.PLOT_WIDTH+10,Library.PLOT_HEIGHT+140);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
}
/**
*由ActionListener所指定的方法,响应用户单击按钮时的动作
*@param e 单击按钮所产生的事件
*/
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==saveImageButton)
//保存图像
{
JFileChooser saveFile=new JFileChooser();
saveFile.setFont(Library.font);
saveFile.setFileFilter(new DataFileFilter("png","PNG图像文件(*.png)"));
int choice=saveFile.showSaveDialog(this);
if(choice==JFileChooser.ERROR_OPTION)
{
JOptionPane.showMessageDialog(this,"文件读取错误!","错误",JOptionPane.ERROR_MESSAGE);
return;
}
else if(choice==JFileChooser.APPROVE_OPTION)
//保存图像
{
File file=saveFile.getSelectedFile();
String fileName=file.getAbsolutePath();
int lastIndex=fileName.toLowerCase().lastIndexOf(".png",fileName.length()-4);
if(file.exists() && lastIndex!=-1)
{
int overWrite=JOptionPane.showConfirmDialog(this,"文件已经存在,是否覆盖?",
"覆盖文件",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(overWrite==JOptionPane.NO_OPTION)
return;
}
try
{
if(lastIndex==-1)
fileName=fileName+".png";
//保证以.jpeg为后缀
BufferedImage image=new BufferedImage(Library.PLOT_WIDTH,Library.PLOT_HEIGHT,BufferedImage.TYPE_INT_RGB);
Graphics g=image.createGraphics();
Plotting plotting=new Plotting(g,Library.PLOT_WIDTH,Library.PLOT_HEIGHT,
Library.PLOT_BACKGROUND,Library.PLOT_PERCENTAGE,Library.PLOT_LOGOHEIGHT);
plotting.plotAll(f,xData,yData,Library.CURVE_COLOR,Library.POINT_COLOR,
Library.AXIS_COLOR,Library.PLOT_RADIUS);
//对图像进行处理
BufferedOutputStream fileWriter=new BufferedOutputStream(new FileOutputStream(fileName,false));
ImageIO.write(image,"png",fileWriter);
fileWriter.close();
}
catch(IOException ioE)
{
JOptionPane.showMessageDialog(this,"文件读写错误!\n请重试!",
"读写错误",JOptionPane.ERROR_MESSAGE);
}
}
}
if(e.getSource()==xText || e.getSource()==computeButton)
{
try
{
xValue=Double.parseDouble(xText.getText());
yValue=(float)f.yValue(xValue);
yText.setText(""+yValue);
}
catch(NumberFormatException numE)
{
JOptionPane.showMessageDialog(this,"请输入一个浮点数","输入错误",JOptionPane.ERROR_MESSAGE);
}
}
}
/**
*重写JFrame的paint(Graphics g)方法,用于显示游标直线。
*@param g JVM传过来的Graphics类
*/
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Library.LINE_COLOR);
g.drawLine(x+gap,h0,x+gap,h0+Library.PLOT_HEIGHT);
}
/**
*MouseMotionListener指定的方法,拖动游标线。
*@param e 鼠标事件
*/
public void mouseDragged(MouseEvent e)
{
if(e.getY()>=h0)
{
xMin=plotPanel.xMin;
xScale=plotPanel.xScale;
xSpace=plotPanel.xSpace;
x=e.getX();
xValue=x*xScale-xSpace+xMin;
yValue=(float)f.yValue(xValue);
xText.setText(""+(float)xValue);
yText.setText(""+yValue);
repaint();
}
}
/**
*MouseMotionListener指定的方法,空方法
*@param e 鼠标事件
*/
public void mouseMoved(MouseEvent e)
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -