import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class JFGraph extends JFrame {
private JTextField tfA = null;
private JTextField tfB = null;
private JTextField tfC = null;
private JTextField tfD = null;
private JTextField tfE = null;
private JTextField tfF = null;
public JFGraph() {
super("Graph");
guiInit();
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void guiInit() {
getContentPane().setLayout(new BorderLayout());
JScrollPane scroller = new JScrollPane();
final GraphPanel gp = new GraphPanel();
gp.setBackground(Color.WHITE);
scroller.getViewport().add(gp);
getContentPane().add(scroller, BorderLayout.CENTER);
tfA = new JTextField();
tfB = new JTextField();
tfC = new JTextField();
tfD = new JTextField();
tfE = new JTextField();
tfF = new JTextField();
JPanel jpTF = new JPanel();
GridLayout gl = new GridLayout(6, 2);
jpTF.setLayout(gl);
jpTF.add(new JLabel("a:"));
jpTF.add(tfA);
jpTF.add(new JLabel("b:"));
jpTF.add(tfB);
jpTF.add(new JLabel("c:"));
jpTF.add(tfC);
jpTF.add(new JLabel("d"));
jpTF.add(tfD);
jpTF.add(new JLabel("e:"));
jpTF.add(tfE);
jpTF.add(new JLabel("f:"));
jpTF.add(tfF);
tfA.setText("0.00001");
tfB.setText("0.001");
tfC.setText("0.001");
tfD.setText("0.001");
tfE.setText("0.001");
tfF.setText("0.001");
getContentPane().add(jpTF, BorderLayout.NORTH);
JButton jbAnzeigen = new JButton("Anzeigen");
jbAnzeigen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
double a = Double.parseDouble(tfA.getText());
double b = Double.parseDouble(tfB.getText());
double c = Double.parseDouble(tfC.getText());
double d = Double.parseDouble(tfD.getText());
double e = Double.parseDouble(tfE.getText());
double f = Double.parseDouble(tfF.getText());
double[][] xy = new double[gp.getWidth() - 10][2];
for (int i = 0; i < xy.length; i++) {
xy[i][0] = i;
xy[i][1] = function(a, b, c, d, e, f, i);
}
int w = (int) xy[xy.length - 1][0];
int h = (int) xy[xy.length - 1][1];
Dimension dim = new Dimension(w + 10, h + 10);
gp.setPreferredSize(dim);
gp.setMinimumSize(dim);
gp.setMaximumSize(dim);
gp.setXY(xy);
} catch (NumberFormatException ex) {
ex.printStackTrace();
JOptionPane
.showMessageDialog(
JFGraph.this,
ex.getMessage() != null
&& ex.getMessage().length() > 0 ? ex
.getMessage()
: "Es ist ein Fehler aufgetreten\nSiehe mehr in der Konsole",
"Fehler", JOptionPane.ERROR_MESSAGE);
}
}
});
getContentPane().add(jbAnzeigen, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new JFGraph().setVisible(true);
}
private class GraphPanel extends JPanel {
private double[][] xy = null;
private GraphPanel() {
}
public void setXY(double[][] xy) {
this.xy = xy;
revalidate();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.xy == null)
return;
int w = getWidth();
int h = getHeight();
g.drawLine(5, h - 5, w - 5, h - 5);
g.drawLine(5, h - 5, 5, 5);
g.setColor(Color.RED);
int originX = 5;
int originY = getHeight() - 5;
int[][] xy = new int[this.xy.length][2];
for (int i = 0; i < xy.length; i++) {
xy[i] = fromOrigin(originX, originY, this.xy[i][0],
this.xy[i][1]);
}
Point p = new Point(xy[0][0], xy[0][1]);
for (int i = 1; i < xy.length; i++) {
Point _p = new Point(xy[i][0], xy[i][1]);
g.drawLine(p.x, p.y, _p.x, _p.y);
p = _p;
}
}
private int[] fromOrigin(int originX, int originY, double x, double y) {
int _x = (int) (originX + x);
int _y = (int) (originY - y);
return new int[] { _x, _y };
}
}
public static double function(double a, double b, double c, double d,
double e, double f, double x) {
double y = 0;
y = a * (Math.pow(x, 5));
y += b * (Math.pow(x, 4));
y += c * (Math.pow(x, 3));
y += d * (Math.pow(x, 2));
y += e * x;
y += f;
return y;
}
}