GridBagLayout mit Canvas-Positionsproblem

  • Themenstarter Themenstarter Guest
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hallo,
habe Probleme mit den Positionen von dem GridBagLayout, das ist so kompliziert 🙁. Diese Code gibt das hier aus:

1xs2.jpg



Aber ich will dass es so wird:
2vt0.jpg


Das Problem hierbei ist glaube ich die Canvas(Zeichenfläche). Wie mache ich das am besten? habe das schon mit gridx und gridy zu positionieren aber es geht nicht (ohne Canvas würde es gehen).

Code:
import java.awt.*;
import java.awt.event.*;

public class Gui1 extends Frame {

	public static void main(String[] args) {
		new Gui1();

	}

	public Gui1() {

		super("Gui1");
		Frame f = new Frame();
		f.setBounds(0, 0, 300, 200);
		GridBagLayout gridLayout = new GridBagLayout();
		f.setLayout(gridLayout);
		GridBagConstraints gridC = new GridBagConstraints();

		// Legt Fest, dass die einzelnen Gui-Elemente um sich 5 pixel Abstand
		// haben
		// gridC.insets = new Insets(5,5,5,5);
		//gridC.fill=GridBagConstraints.RELATIVE;
		Canvas canvas = new Canvas();
		canvas.setSize(f.getWidth() /2, f.getHeight());
		canvas.setBackground(Color.YELLOW);
		gridC.gridx = 0;
		gridC.gridy = 0;
		gridC.gridheight=3;
		f.add(canvas);

		Choice choice = new Choice();
		choice.add("1. Choice");
		choice.add("2. Choice");
		gridC.gridx = 1;
		gridC.gridheight=1;
		// gridC.gridy = 0;
		gridLayout.setConstraints(choice, gridC);
		f.add(choice);

		Button okButton = new Button("Button1");
		gridC.gridy = 1;
		gridLayout.setConstraints(okButton, gridC);
		f.add(okButton);

		Button lButton = new Button("Button2");
		// gridC.gridx = 3;
		gridC.gridy = 2;
		gridLayout.setConstraints(lButton, gridC);
		f.add(lButton);

		Button b = new Button("Button3");
		// gridC.gridx = 4;
		gridC.gridy = 3;
		gridLayout.setConstraints(b, gridC);
		
		f.add(b);
		f.pack();

		final WindowListener wndCloser = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		};

		f.addWindowListener(wndCloser);
		f.setVisible(true);

	}

}
 
So besser? Es lohnt sich nicht, das GridBagConstraints-Objekt mehrfach zu benutzen. Nicht zurückgesetzte Werte verursachen mehr Probleme, als die 5 zusätzlich erstellen Objekte... :wink:
Code:
import java.awt.*;
import java.awt.event.*;

public class Gui1 extends Frame {

   public static void main(String[] args) {
      new Gui1();

   }

   public Gui1() {

      super("Gui1");
      Frame f = new Frame();
      GridBagLayout gridLayout = new GridBagLayout();
      f.setLayout(gridLayout);
      
      // Legt Fest, dass die einzelnen Gui-Elemente um sich 5 pixel Abstand
      // haben
      // gridC.insets = new Insets(5,5,5,5);
      //gridC.fill=GridBagConstraints.RELATIVE;
      
      Insets insets = new Insets( 0, 0, 0, 0 );
      
      Canvas canvas = new Canvas();
      canvas.setPreferredSize( new Dimension( 150, 300 ));
      canvas.setBackground(Color.YELLOW);
      f.add( canvas, new GridBagConstraints( 0, 0, 1, 5, 1.0, 1.0,
              GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0 ));
            
      Choice choice = new Choice();
      choice.add("1. Choice");
      choice.add("2. Choice");
      f.add(choice, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
              GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insets, 0, 0 ));

      Button okButton = new Button("Button1");
      f.add(okButton, new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0,
              GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insets, 0, 0 ));

      Button lButton = new Button("Button2");
      f.add(lButton, new GridBagConstraints( 1, 2, 1, 1, 0.0, 0.0,
              GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insets, 0, 0 ));

      Button b = new Button("Button3");
      f.add(b, new GridBagConstraints( 1, 3, 1, 1, 0.0, 0.0,
              GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insets, 0, 0 ));
      
      f.pack();

      final WindowListener wndCloser = new WindowAdapter() {
         @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      };

      f.addWindowListener(wndCloser);
      f.setVisible(true);

   }

}
 
Super danke 🙂
Bin Anfänger was GridBagLayout angeht.
Habe aber noch ne Frage:
also bei new GridBagConstraintr, da sind die ersten beiden Parameter die Positionen der Elemente und die anderen?

f.add( canvas, new GridBagConstraints( 0, 0, 1, 5, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0 ));


setPreferredSize(), was ist der Unterschied zu setSize()?
 
Ich bins nochmal , Mit der Animation in der Gui klappt es sehr gut, nun will ich aber dass drei Baelle fliegen sollen und zwar zufällig (Random). Also eim Klick auf 3 balls sollen 3 Baelle fliegen. Bei start nur ein Ball und bei Stop hält es an. Ich habe versucht mit ner for-schleife in paint drei Baelle zu erstellen aber sie überlappten, und habe es bei run versucht aber tut auch nichts ... 🙁
Code:
import java.awt.*;
import java.awt.event.*;

public class Gui2 extends Frame {
	Button Start, Stop, b;

	boolean again = true;

	AnimationCanvas canvas = new AnimationCanvas();

	public static void main(String[] args) {
		new Gui2();

	}

	public Gui2() {

		Frame f = new Frame();
		f.setTitle("TEST");
		GridBagLayout gridLayout = new GridBagLayout();
		f.setLayout(gridLayout);
		Insets insets = new Insets(0, 0, 0, 0);

		canvas.setPreferredSize(new Dimension(400, 400));
		canvas.setBackground(Color.YELLOW);
		f.add(canvas, new GridBagConstraints(0, 0, 1, 5, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0,
				0));

		Label label1 = new Label("test test");
		f.add(label1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				insets, 0, 0));

		Choice choice = new Choice();
		choice.add("Ball1");
		choice.add("2. Choice");
		f.add(choice, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				insets, 0, 0));

		Start = new Button("Start");
		f.add(Start, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				insets, 0, 0));

		Stop = new Button("Stop");
		f.add(Stop, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				insets, 0, 0));

		b = new Button("3 Balls");
		f.add(b, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				insets, 0, 0));

		f.pack();
		b.addActionListener(actionList);
		Start.addActionListener(actionList);
		Stop.addActionListener(actionList);
		addWindowListener(wndCloser);
		setVisible(true);

		f.addWindowListener(wndCloser);
		f.setVisible(true);

	}

	/**
	 * 
	 * Button Befehle Bei Start animiert der Ball bei Stop stoppt er
	 */

	ActionListener actionList = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == Start && again) {
				canvas.start = true;// sagt der Animation Ok
				canvas.startAnimation();// Startet
				again = !again;// So dass man nicht hintereinander drauf
				// klicken darf
			}
			if (e.getSource() == Stop) {

				canvas.stopAnimation();// Sagt Stop
				again = true;// schaltet wieder Frei um auf start zu klicken
			}

		}

	};

	/**
	 * Schliesst das Fenster
	 * 
	 */
	WindowListener wndCloser = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	};

}




Code:
import java.awt.*;

public class AnimationCanvas extends Canvas implements Runnable {
	int x = 100, dx = 10, y = 10, dy = 10;
	boolean start = true;

	public void startAnimation() {
		Thread thread = new Thread(this);
		thread.start();
	}

	public void stopAnimation() {
		start = false;
	}

	public void run() {
		while (start) {

			repaint();
			
				try {

				Thread.sleep(40);

			} catch (InterruptedException e) {

			}

		}

	}	

	public void paint(Graphics g) {

		for (int i = 0; i < 1; i++) {
			x += dx;
			if (x > getWidth() - 50 || x < 0)
				dx = -dx;
			y += dy;
			if (y > getHeight() - 50 || y < 0)
				dy = -dy;
			g.fillOval(x, y, 20, 20);

		}
	}
}
 
Anonymous hat gesagt.:
also bei new GridBagConstraintr, da sind die ersten beiden Parameter die Positionen der Elemente und die anderen?
wheightx,y: "Gewicht" der Componenten im Vergleich zu den anderen. Je wichtiger eine Componente, desto mehr Platz kriegt sie.
anchor: Wenn die Gitter-Zelle nicht ganz von der Componente ausgefüllt wird, sagt dieser Parameter, in welcher Ecke die Componenten zu liegen kommt.
fill: In welche Richtungen eine Componente gestreckt werden soll.
insets: Wieviel Platz es um eine Componente geben muss.
ipadx,y: Die minimale Grösse einer Zelle ist = minimale Grösse Component + ipadx/y.

setPreferredSize(), was ist der Unterschied zu setSize()?
"setPreferredSize" sagt einem LayoutManager, wiegross eine Componente gerne sein möchte. Das Layout wird dann versuchen "setSize" mit möglichst passenden Werten aufzurufen.

Zur Animation:
Das mit den Bällen hab ich nicht ganz kappiert. Du has keine Kollisionserkennung der Bälle, wieso sollten sie sich nicht überlappen?

P.S. wäre ein Ball-Objekt nicht einfacher, als all das Zeugs im Canvas zu speichern? :wink:
 
Beni hat gesagt.:
Zur Animation:
Das mit den Bällen hab ich nicht ganz kappiert. Du has keine Kollisionserkennung der Bälle, wieso sollten sie sich nicht überlappen?

P.S. wäre ein Ball-Objekt nicht einfacher, als all das Zeugs im Canvas zu speichern? :wink:

Also das mit einem Ball funktioniert sehr gut. Wenn ich z.B 2 oder 3 Bälle gleichzeitig (aber an veschiedenen Positionen mit Random-Werten)los hoppsen lasse, geht es nicht, die sind dann übereinander und laufen genau zur zelben Zeit. Ich will das schon mit Canvas machen, aber wenn das nicht anders geht?


hier z.B hier springt der eine Ball korrekt durch die Gegend. Meine Frage war, mache ich das mit den 3 Bällen direkt in der paint methode oder in der run Methode?

Code:
 public void paint(Graphics g) { 

      for (int i = 0; i < 1; i++) { 
         x += dx; 
         if (x > getWidth() - 50 || x < 0) 
            dx = -dx; 
         y += dy; 
         if (y > getHeight() - 50 || y < 0) 
            dy = -dy; 
         g.fillOval(x, y, 20, 20); 

      } 
   }
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben