Probleme bei der Variablenübergabe

Status
Nicht offen für weitere Antworten.

real8

Mitglied
Hi,
Versuche mich seit einer Woche in Java…die ersten Schritte gingen ganz flott bei mir. Nur jetzt hänge ich schon seit ein paar Tagen. Ich hoffe sehr das mit jemand helfen kann.

Ich möchte aus einer Menübar eines neues Fenster starten und in dem einige Parameter eingeben. Mit diesen Parametern möchte ich etwas zeichnen. Das Menü und das Parameter Fenster hab ich auch schon, die Parameter kann ich auch auslesen.

Das Problem ist folgendes: Ich kann der drawline Funktion nicht beibringen das sie mit den eingegeben Parametern eine Linie zeichnen soll. Wenn ich in meiner Funktion calculate x und y ausgebe, zeigt mir die Console noch den Wert an den ich in der Textbox eingegeben habe, nur in der paint Funktion werden x und y immer wieder null gesetzt! Ich glaub das Problem liegt darin das die Werte zwischen den zwei Frames (test und neu bzw. gui_ksp und gui_ksp(int i)) verloren gehen…bin schon langsam am verzweifeln…
Hier mein gekürzter Code:

Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension.*;
import java.awt.Color.*;
import java.awt.Canvas.*;

public class gui_ksp extends Frame implements ActionListener, WindowListener
{
	MyCanvas canvas = new MyCanvas();
	input myinput = new input();
	
	TextField field_vr_x = new TextField("");
	TextField field_vr_y = new TextField("");
		
	int vr_x;
	int vr_y;
	
  public static void main(String args[])
  {
	 gui_ksp gui_ksp1 = new gui_ksp();
	 gui_ksp1.setSize(800, 600);
	 gui_ksp1.setLocation(200, 100);
	 gui_ksp1.setTitle("test");
	 gui_ksp1.show();
   }

  //Menü wird hier initialisiert
  public gui_ksp()
  {
     MenuBar hauptMenue = new MenuBar();
     Menu menue1 = new Menu("Datei");
     menue1.add("Neu");
     menue1.add("Beenden");
     menue1.addActionListener(this);
     hauptMenue.add(menue1);
     setMenuBar(hauptMenue);
     addWindowListener(this);
     
     GridBagLayout myGridBag = new GridBagLayout();
     GridBagConstraints myGbConstraints = new GridBagConstraints();
     Font fontHelvBold14 = new Font("Helvetica", Font.BOLD, 14);
     setLayout(myGridBag);
     
     buildConstraints(myGbConstraints, 0, 9, 10, 10, 10,10);
     myGbConstraints.fill = GridBagConstraints.BOTH;
     myGridBag.setConstraints(canvas, myGbConstraints);
     add(canvas);     
   }
  
  void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) 
  {
	 gbc.gridx = gx;
	 gbc.gridy = gy;
	 gbc.gridwidth = gw;
	 gbc.gridheight = gh;
	 gbc.weightx = wx;
	 gbc.weighty = wy;
  }
  
  public gui_ksp(int i)
  {
	 Color custom_gray = new Color(212,208,200);
	 GridBagLayout gbl = new GridBagLayout();
	 GridBagConstraints gbc;
	 this.setLayout(gbl);
	 this.setBackground(custom_gray);
	 //addWindowListener(this);
	 
	 //Label x
	 gbc = makegbc(1, 0, 1, 1, 1);
	 gbc.fill = GridBagConstraints.NONE;
	 Label label_x = new Label("x");
	 gbl.setConstraints(label_x, gbc);
	 add(label_x);
	 
	//Label y 
	 gbc = makegbc(2, 0, 1, 1, 1);
	 gbc.fill = GridBagConstraints.NONE;
	 Label label_y = new Label("y");
	 gbl.setConstraints(label_y, gbc);
	 add(label_y);
	
	//Label vr
	 gbc = makegbc(0, 2, 1, 1, 1);
	 gbc.fill = GridBagConstraints.NONE;
	 Label label_vr = new Label("VR");
	 gbl.setConstraints(label_vr, gbc);
	 add(label_vr);
	
	 //Texfeld vr_x
	 gbc = makegbc(1, 2, 1, 1, 1);
	 gbc.weightx = 0;
	 gbc.fill = GridBagConstraints.HORIZONTAL;
	 gbl.setConstraints(field_vr_x, gbc);
	 add(field_vr_x);
	 
	//Texfeld vr_y
	 gbc = makegbc(2, 2, 1, 1, 1);
	 gbc.weightx = 0;
	 gbc.fill = GridBagConstraints.HORIZONTAL;
	 gbl.setConstraints(field_vr_y, gbc);
	 add(field_vr_y);
	 
	 //Ok-Button
	 Button button_ok = new Button("Ok");
	 gbc = makegbc(1, 9, 1, 1, 45);
	 gbc.fill = GridBagConstraints.NONE;
	 gbc.anchor = GridBagConstraints.SOUTHEAST;
	 gbl.setConstraints(button_ok, gbc);
	 add(button_ok);
	 button_ok.addActionListener(this);
	 
	//Abbrechen-Button
	 Button button_abbrechen = new Button("Abbrechen");
	 gbc = makegbc(2, 9, 1, 1, 1);
	 gbc.fill = GridBagConstraints.NONE;
	 gbc.anchor = GridBagConstraints.SOUTHEAST;
	 gbl.setConstraints(button_abbrechen, gbc);
	 add(button_abbrechen);
	 button_abbrechen.addActionListener(this);
	 
	//Passt die Fenstergröße an die Elemente an 
	 pack();
  }
  
  //Parameter für das GridBagConstraints
  private GridBagConstraints makegbc(int x, int y, int width, int height, int padx)
  {
	  GridBagConstraints gbc = new GridBagConstraints();
	  gbc.gridx = x;
	  gbc.gridy = y;
	  gbc.gridwidth = width;
	  gbc.gridheight = height;
	  gbc.insets = new Insets(1, 1, 1, 1);
	  gbc.ipadx = padx;
	  return gbc;
  }

  public void actionPerformed(ActionEvent evt)
  {
     if (evt.getSource() instanceof MenuItem)
    {
       String menuAdd = evt.getActionCommand();
       if (menuAdd == "Beenden") System.exit(0);
       if (menuAdd == "Neu")
       {
    	   gui_ksp gui_ksp2 = new gui_ksp(1);
    	   gui_ksp2.setLocation(300, 300);
    	   gui_ksp2.setTitle("Neu");
    	   gui_ksp2.show();
       }
     }
     if (evt.getSource() instanceof Button)
     {
    	 String buttonAdd = evt.getActionCommand();
    	 if (buttonAdd == "Abbrechen") 
    	 {
    		dia_closing();
    	 }
    	 if (buttonAdd == "Ok")
    	 {    		 
    		 vr_x=Integer.parseInt(field_vr_x.getText());
    		 vr_y=Integer.parseInt(field_vr_y.getText());
    		 
    		 myinput.temp_x = vr_x;
    		 myinput.temp_y = vr_y;
    		 canvas.calculate(myinput);
    		 dia_closing();
    	 }
     }
   }

  public void dia_closing()
  {
	  setVisible(false);
	  dispose();
  }
  
  //Programm Schließen
  public void windowClosing(WindowEvent evt)
  {
     System.exit(0);
  }

}

class MyCanvas extends Canvas 
{
	int x;
	int y;
	
	 public void calculate(input myinput) 
	  {
		  
		  x=myinput.temp_x;
		  y=myinput.temp_y;
		  repaint();
	  }
	
	  public void paint(Graphics g)
	  {	  
	 	 g.setColor(Color.black);
	 	 g.drawLine(x,y,100,100);
	  }

public class input 
{
	int temp_x;
	int temp_y;

}

Vielen Dank für eure Hilfe!
 
ch glaub das Problem liegt darin das die Werte zwischen den zwei Frames (test und neu bzw. gui_ksp und gui_ksp(int i)) verloren gehen…
Ja. Mach' das Fenster zum Eingeben und das Fenster zum Zeichnen als getrennte Klassen.
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben