Ich hab hier mal ein kleines Programm geschrieben zur Linearen Interpolation mit GUI:
Die Main Class:
[code:1]
public class Main
{
public static void main(String[] args)
{
Window hauptfenster = new Window();
hauptfenster.pack();
hauptfenster.setSize( 200 , 150 );
hauptfenster.setLocation( 250 , 250 );
hauptfenster.setVisible( true );
hauptfenster.setResizable(false);
}
}
[/code:1]
Die Window Class:
[code:1]
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
public class Window extends JFrame implements ActionListener
{
private JButton okButton = new JButton( "OK" );
private JButton endButton = new JButton( "Ende" );
private JTextField eingx1 = new JTextField( "x1" , 4 );
private JTextField eingx2 = new JTextField( "x2" , 4 );
private JTextField eingy1 = new JTextField( "y1" , 4 );
private JTextField eingy2 = new JTextField( "y2" , 4 );
private JTextField edtm = new JTextField( "m" , 4 );
private JTextField edtn = new JTextField( "n" , 4 );
private JTextField edtfx = new JTextField( "f(x)" , 4);
public Window()
{
super( "Interpolation" );
DecimalFormat formatter = new DecimalFormat( "#,##0.00" );
JPanel cp = new JPanel();
JPanel cpLeft = new JPanel();
JPanel cpRight = new JPanel();
JPanel cpSouth = new JPanel();
JPanel cpCenter = new JPanel();
GridLayout grid = new GridLayout(4,1);
GridLayout grid2 = new GridLayout(1,2);
GridLayout grid3 = new GridLayout(4,1);
cp.setLayout( new BorderLayout(5,5) ) ;
cpLeft.setLayout( grid );
cpRight.setLayout( grid );
cpSouth.setLayout( grid2 );
cpCenter.setLayout( grid3 );
cp.add( cpLeft, BorderLayout.WEST );
cp.add( cpRight , BorderLayout.EAST );
cp.add( cpSouth, BorderLayout.SOUTH );
cp.add( cpCenter, BorderLayout.CENTER );
cpLeft.add( eingx1 );
cpLeft.add( eingy1 );
cpRight.add( edtm );
cpRight.add( edtn );
cpSouth.add( okButton );
cpSouth.add( endButton );
cpCenter.add( eingx2 );
cpCenter.add( eingy2 );
cpCenter.add( new JLabel(""));
cpCenter.add( edtfx );
okButton.addActionListener( this );
endButton.addActionListener( this );
setContentPane( cp );
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == okButton )
{
doBerechnen();
}
if (source == endButton )
{
doEnd();
}
}
private void doEnd()
{
System.exit(0);
}
private void doBerechnen()
{
try
{
double x1 = Double.parseDouble( eingx1.getText() );
double y1 = Double.parseDouble( eingy1.getText() );
double x2 = Double.parseDouble( eingx2.getText() );
double y2 = Double.parseDouble( eingy2.getText() );
double m = ( y2 - y1 ) / ( x2 - x1 );
double n = ( ( y1 * x2 ) - ( y2 * x1) ) / ( x2 - x1 );
double x = Double.parseDouble( edtfx.getText() );
double fx = m * x + n ;
edtm.setText( Double.toString( m ));
edtn.setText( Double.toString( n ));
edtfx.setText( Double.toString( fx ));
}
catch ( Exception exception )
{
JOptionPane.showMessageDialog(null,
"Bitte eine Zahl eingeben!",
"Fehler",
JOptionPane.ERROR_MESSAGE);
}
}
}
[/code:1]
Die Main Class:
[code:1]
public class Main
{
public static void main(String[] args)
{
Window hauptfenster = new Window();
hauptfenster.pack();
hauptfenster.setSize( 200 , 150 );
hauptfenster.setLocation( 250 , 250 );
hauptfenster.setVisible( true );
hauptfenster.setResizable(false);
}
}
[/code:1]
Die Window Class:
[code:1]
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
public class Window extends JFrame implements ActionListener
{
private JButton okButton = new JButton( "OK" );
private JButton endButton = new JButton( "Ende" );
private JTextField eingx1 = new JTextField( "x1" , 4 );
private JTextField eingx2 = new JTextField( "x2" , 4 );
private JTextField eingy1 = new JTextField( "y1" , 4 );
private JTextField eingy2 = new JTextField( "y2" , 4 );
private JTextField edtm = new JTextField( "m" , 4 );
private JTextField edtn = new JTextField( "n" , 4 );
private JTextField edtfx = new JTextField( "f(x)" , 4);
public Window()
{
super( "Interpolation" );
DecimalFormat formatter = new DecimalFormat( "#,##0.00" );
JPanel cp = new JPanel();
JPanel cpLeft = new JPanel();
JPanel cpRight = new JPanel();
JPanel cpSouth = new JPanel();
JPanel cpCenter = new JPanel();
GridLayout grid = new GridLayout(4,1);
GridLayout grid2 = new GridLayout(1,2);
GridLayout grid3 = new GridLayout(4,1);
cp.setLayout( new BorderLayout(5,5) ) ;
cpLeft.setLayout( grid );
cpRight.setLayout( grid );
cpSouth.setLayout( grid2 );
cpCenter.setLayout( grid3 );
cp.add( cpLeft, BorderLayout.WEST );
cp.add( cpRight , BorderLayout.EAST );
cp.add( cpSouth, BorderLayout.SOUTH );
cp.add( cpCenter, BorderLayout.CENTER );
cpLeft.add( eingx1 );
cpLeft.add( eingy1 );
cpRight.add( edtm );
cpRight.add( edtn );
cpSouth.add( okButton );
cpSouth.add( endButton );
cpCenter.add( eingx2 );
cpCenter.add( eingy2 );
cpCenter.add( new JLabel(""));
cpCenter.add( edtfx );
okButton.addActionListener( this );
endButton.addActionListener( this );
setContentPane( cp );
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == okButton )
{
doBerechnen();
}
if (source == endButton )
{
doEnd();
}
}
private void doEnd()
{
System.exit(0);
}
private void doBerechnen()
{
try
{
double x1 = Double.parseDouble( eingx1.getText() );
double y1 = Double.parseDouble( eingy1.getText() );
double x2 = Double.parseDouble( eingx2.getText() );
double y2 = Double.parseDouble( eingy2.getText() );
double m = ( y2 - y1 ) / ( x2 - x1 );
double n = ( ( y1 * x2 ) - ( y2 * x1) ) / ( x2 - x1 );
double x = Double.parseDouble( edtfx.getText() );
double fx = m * x + n ;
edtm.setText( Double.toString( m ));
edtn.setText( Double.toString( n ));
edtfx.setText( Double.toString( fx ));
}
catch ( Exception exception )
{
JOptionPane.showMessageDialog(null,
"Bitte eine Zahl eingeben!",
"Fehler",
JOptionPane.ERROR_MESSAGE);
}
}
}
[/code:1]
mfg KC