如何在netbeans6.8中用Java语言绘制类型于下图的折线图,要有详细代码
成图后,根据一个X的坐标值得到一个Y值?(折线图不用显示出来,只需要得到所求的Y值)要有详细代码...
成图后,根据一个X的坐标值得到一个Y值?(折线图不用显示出来,只需要得到所求的Y值)要有详细代码
展开
1个回答
展开全部
public class XYJFrame extends javax.swing.JFrame {
/**
* Creates new form XYJFrame
*/
public XYJFrame() {
initComponents();
iniCoordinate();
}
/**
* Netbeans 生成的代码,用于界面设计
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lblXCoordinate = new javax.swing.JLabel();
lblYCoordinate = new javax.swing.JLabel();
txtXCoordinate = new javax.swing.JTextField();
txtYCoordinate = new javax.swing.JTextField();
btnCalculateY = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lblXCoordinate.setText("X 坐标");
lblYCoordinate.setText("Y 坐标");
btnCalculateY.setText("计算 Y 坐标");
btnCalculateY.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalculateYActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblXCoordinate)
.addComponent(lblYCoordinate))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtXCoordinate)
.addComponent(txtYCoordinate, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(btnCalculateY)
.addGap(30, 30, 30)))
.addContainerGap(131, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblXCoordinate)
.addComponent(txtXCoordinate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblYCoordinate)
.addComponent(txtYCoordinate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addComponent(btnCalculateY)
.addContainerGap(103, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnCalculateYActionPerformed(java.awt.event.ActionEvent evt) {
this.txtYCoordinate.setText(this.calculateY() + "");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(XYJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(XYJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(XYJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(XYJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new XYJFrame().setVisible(true);
}
});
}
private void iniCoordinate() {
// 转折点 X 坐标
dbXCoordinate = new double[] { 0, 0.1, 0.3, 0.5, 1 };
// 转折点 Y 坐标
dbYCoordinate = new double[] { 0, 0.4, 0.7, 0.85, 1 };
}
// 计算 Y 坐标
private double calculateY() {
double xCoordinate = Double.parseDouble(this.txtXCoordinate.getText());
int xPos = getXPositon(xCoordinate);
return xPos == -1 ? -1 : (dbYCoordinate[xPos] * xCoordinate) / dbXCoordinate[xPos];
}
// 得到输入的 X 值位置,确定比率
private int getXPositon(double xCoordinate) {
for(int i=0; i < dbXCoordinate.length; i++) {
if(xCoordinate <= dbXCoordinate[i]) {
return i;
}
}
return -1;
}
private double[] dbXCoordinate = new double[5];
private double[] dbYCoordinate = new double[5];
private double[] rate = new double[5];
// Variables declaration - do not modify
private javax.swing.JButton btnCalculateY;
private javax.swing.JLabel lblXCoordinate;
private javax.swing.JLabel lblYCoordinate;
private javax.swing.JTextField txtXCoordinate;
private javax.swing.JTextField txtYCoordinate;
// End of variables declaration
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询