Hallo,
ich habe ein kleines Programm geschrieben dass eine Funktion in ein Koordinatensystem zeichnen soll.
Jetzt habe ich 2 Probleme:
1. Wie mache ich es, dass die Methode paint weiß, dass sie in Panel 2 malen soll
2. Wie sage ich wenn der Button gedrückt wird, er soll die Funktion neu zeichnen.
Ich weiß, dass die Methode nicht aufgerufen wird, habe schon allerlei probiert aber nix kam bei raus...
Über Hilfe würde ich mich sehr freuen...
Danke
s1d
P.S. ich schließe nicht aus dass einiges überflüssig ist, aber noch bin ich Anfänger
ich habe ein kleines Programm geschrieben dass eine Funktion in ein Koordinatensystem zeichnen soll.
Jetzt habe ich 2 Probleme:
1. Wie mache ich es, dass die Methode paint weiß, dass sie in Panel 2 malen soll
2. Wie sage ich wenn der Button gedrückt wird, er soll die Funktion neu zeichnen.
Code:
package Frames;
import java.awt.*;
import java.awt.event.*;
public class FunktionZ extends Frame implements ActionListener {
class Fenster_Listener extends WindowAdapter {
public void windowClosing(WindowEvent ev){
System.exit(0);
}
}
Button bt;
Label l1;
Label l2;
Panel p1, p2, p3;
Panel ptfa, ptfb, ptfc, ptfd;
TextField tfa, tfb, tfc, tfd;
TextField tfay, tfby, tfcy, tfdy;
double a, b, c, d, ay, by, cy, dy;
public FunktionZ(String titel) {
super(titel);
setSize(600,400);
setLocation(200,200);
setResizable(true);
setVisible(true);
addWindowListener(new Fenster_Listener());
setLayout(new BorderLayout());
p1 = new Panel();
p2 = new Panel();
ptfa = new Panel();
ptfb = new Panel();
ptfc = new Panel();
ptfd = new Panel();
tfa = new TextField("a",1);
tfb = new TextField("b",1);
tfc = new TextField("c",1);
tfd = new TextField("d",1);
tfay = new TextField("ay",1);
tfby = new TextField("by",1);
tfcy = new TextField("cy",1);
tfdy = new TextField("dy",1);
//Festlegen
bt = new Button("Draw");
bt.addActionListener(this);
l1 = new Label("Funktion lautet:ax^ay+bx^by+cx^cy+dx^dy");
l2 = new Label();
p1.setLayout(new FlowLayout(FlowLayout.RIGHT));
p2.setLayout(new FlowLayout());
ptfa.setLayout(new GridLayout(1,2));
ptfb.setLayout(new GridLayout(1,2));
ptfc.setLayout(new GridLayout(1,2));
ptfd.setLayout(new GridLayout(1,2));
ptfa.add(tfa);
ptfa.add(tfay);
ptfb.add(tfb);
ptfb.add(tfby);
ptfc.add(tfc);
ptfc.add(tfcy);
ptfd.add(tfd);
ptfd.add(tfdy);
p1.add(l1);
p1.add(ptfa);
p1.add(ptfb);
p1.add(ptfc);
p1.add(ptfd);
p1.add(bt);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(l2, BorderLayout.SOUTH);
}
public void doit() {
a = Double.parseDouble(tfa.getText());
b = Double.parseDouble(tfb.getText());
c = Double.parseDouble(tfc.getText());
d = Double.parseDouble(tfd.getText());
ay = Double.parseDouble(tfay.getText());
by = Double.parseDouble(tfby.getText());
cy = Double.parseDouble(tfcy.getText());
dy = Double.parseDouble(tfdy.getText());
}
public void actionPerformed (ActionEvent ev) {
doit();
setLabel();
}
public void setLabel() {
String s;
try{
s = Double.toString(a)+ "^" + Double.toString(ay);
s = s + Double.toString(b)+ "^" + Double.toString(by);
s = s + Double.toString(c)+ "^" + Double.toString(cy);
s = s + Double.toString(d)+ "^" + Double.toString(dy);
l2.setText(s);
}
catch(NumberFormatException nfe){
l2.setText("Falsche Eingabe!");
}
}
public void paint(Graphics g) {
super.paint(g);
int y = 199;
int x = 299;
//Koordinatenkreuz
g.setColor(Color.black);
g.drawLine(299, 0, 299, 400);
g.drawLine(0, 199, 600, 199);
//10 Pixel entsprechen 1cm
for(int i = 0; i < 40; i++)
g.drawLine(298, i*10, 300, i*10);
//10 Pixel entsprechen 1cm
for(int i = 0; i < 60; i++)
g.drawLine(i*10, 198, i*10, 200);
//beliebige Funktion
for(int i = 0; i < 10; i++) {
//Funktionswert berechnen
double fwert1 = Math.round((a*Math.pow(i, ay))+(b*Math.pow(i, by))+(c*Math.pow(i, cy))+(d*Math.pow(i, dy)));
//Punkte setzen
int fwert = (int) fwert1;
g.setColor(Color.red);
g.drawOval(x+(i*10),y-fwert,1,1);
g.drawOval(x-(i*10), y-fwert, 1, 1);
}
}
public static void main(String[] args) {
new FunktionZ("Funktion");
}
}
Über Hilfe würde ich mich sehr freuen...
Danke
s1d
P.S. ich schließe nicht aus dass einiges überflüssig ist, aber noch bin ich Anfänger