Problem mit repaint()

  • Themenstarter Themenstarter Equilibrium
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
E

Equilibrium

Gast
Guten Abend,
ich habe ein Problem mit der Funktion repaint bei einer Canvas.
Mein Code sieht so aus:

Code:
public void paint(Graphics g) {

		if (Option.equals("Quadraht")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;

			g.setColor(Color.black);

			g.drawLine(x - 60, y - 60, x + 60, y - 60);
			g.drawLine(x + 60, y - 60, x + 60, y + 60);
			g.drawLine(x + 60, y + 60, x - 60, y + 60);
			g.drawLine(x - 60, y + 60, x - 60, y - 60);

			g.drawString("a", x, y - 62);
			g.drawString("a", x, y + 70);
			g.drawString("a", x - 70, y);
			g.drawString("a", x + 62, y);

			g.setColor(Color.red);
			g.drawLine(x - 60, y - 60, x + 60, y + 60);

			g.drawString("e", x + 1, y);
		}
		if (Option.equals("Rechteck")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;
			
			g.setColor(Color.gray);
			g.drawRect(0, 0, 190, 271);

			g.setColor(Color.black);

			g.drawLine(x - 80, y - 40, x + 80, y - 40);
			g.drawLine(x + 80, y - 40, x + 80, y + 40);
			g.drawLine(x + 80, y + 40, x - 80, y + 40);
			g.drawLine(x - 80, y + 40, x - 80, y - 40);
		}
	}

Wenn ich jedoch jetzt repaint() aufrufe nachdem ich Option="Rechteck" gemacht habe, ändert sich nichts. Kann mir einer sagen was ich da falsch mache?.
 
'Quadraht' ist ja gut geschrieben 😉

------

wie wärs wenn dir das Programm das sagt, oder du zumindest sagst, was das Programm sagt?

Code:
public void paint(Graphics g) {

System.out.println("paint aufgerufen, Option: "+..);

if (quadrat) {
System.out.println("Quadrat");
....
} else if (rechteck) {
System.out.println("Rechteck");
...
} else {
System.out.println("unbekannte Option");

}

}
 
Das mit dem Quadra(h)t werde ich warscheinlich nie hinbekommen, wie oft habe ich das schon verbessert :lol:

Nach dem ich es so probiert habe, wie du es geraten hast, ist alles noch unklarer als zuvor. Option wird zwar wie gewünscht auf Rechteck geändert, jedoch scheint das die paint Methode ziemlich kalt zu lassen.

Code:
public void paint(Graphics g) {
		
		System.out.println("Ich male jetzt etwas!");
		
		if (Option.equals("Quadraht")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;

			g.setColor(Color.black);

			g.drawLine(x - 60, y - 60, x + 60, y - 60);
			g.drawLine(x + 60, y - 60, x + 60, y + 60);
			g.drawLine(x + 60, y + 60, x - 60, y + 60);
			g.drawLine(x - 60, y + 60, x - 60, y - 60);

			g.drawString("a", x, y - 62);
			g.drawString("a", x, y + 70);
			g.drawString("a", x - 70, y);
			g.drawString("a", x + 62, y);

			g.setColor(Color.red);
			g.drawLine(x - 60, y - 60, x + 60, y + 60);

			g.drawString("e", x + 1, y);
			System.out.println("Ich male ein Quadrat");
		}
		if (Option.equals("Rechteck")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;

			g.setColor(Color.gray);
			g.drawRect(0, 0, 190, 271);

			g.setColor(Color.red);

			g.drawLine(x - 80, y - 40, x + 80, y - 40);
			g.drawLine(x + 80, y - 40, x + 80, y + 40);
			g.drawLine(x + 80, y + 40, x - 80, y + 40);
			g.drawLine(x - 80, y + 40, x - 80, y - 40);
			System.out.println("Ich male ein Rechteck");
		}
	}

	public String getMethod() {
		return Option;
	}

Ausgegeben wird:
Aktuelle Methode: Rechteck
Ich male jetzt etwas!

Aber es wird weder das Rechteck gezeichnet, noch der Text augegeben.[/quote]
 
System.out.println("Ich male jetzt etwas!");
ändern in
System.out.println("Ich male jetzt etwas, nämlich ein '"+Option+"'");

Vielleicht hilft's...
 
Nein @Marco. Er hat die Ausgaben schon richtig gemacht
(genauer den Quelltext lesen 😎 )

Code:
if (Option.equals("Rechteck")) {

Bist du sicher, daß in Option (variablennamen klein schreiben)
auch wirklich "Rechteck" steht?

Gib' am Anfang der Methode (vor den Abfragen) auch mal Option aus

Edit: Ach so @Marco. hab dich wohl falsch verstanden; hast so auch Recht.
 
Mal eine Grundsätzliche Frage, repaint() ruft nur erneut die paint() Methode auf, oder macht das noch etwas anderes? Weil irgendwie scheint es mir, als würde die Variable Option immer sobald repaint aufgerufen wird zurückgesetzt?
 
repaint und weitere Hintergrundoperationen machen
intern vielleicht noch einiges anderes in Richtung Zeichnen/ Verwalten,
ganz bestimmt aber nichts bzgl deiner Logik/ Exemplarvariablen
 
Dein Problem scheint nicht an der paint-Methode zu liegen. Ich habe die eben mal in einen JFrame reingepackt und alles funktionierte problemlos. Je nachdem, ob ich die Option auf Quadraht 😉 oder Rechteck gesetzt hatte, wurde entsprechend gezeichnet.
Vielleicht solltest Du mal ein bißchen mehr Code posten.


<edit>
Ach ja, pack doch mal ein
Code:
		super.paint(g);

als ersten Befehl in Deine paint-Methode.

</edit>
 
Gut, dann mal ein bisschen mehr Code.

Zeichenflaeche
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Canvas;

class Zeichenflaeche extends Canvas {

	private static final long serialVersionUID = 1L;

	public String Option = "";

	public Zeichenflaeche() {

		super();
	}

	// Überschreiben der Methode paint() von Canvas

	public void paint(Graphics g) {

		super.paint(g);

		this.setBackground(Color.GRAY);

		System.out.println("Ich male jetzt: " + Option);

		if (Option.equals("Quadraht")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;

			g.setColor(Color.black);

			g.drawLine(x - 60, y - 60, x + 60, y - 60);
			g.drawLine(x + 60, y - 60, x + 60, y + 60);
			g.drawLine(x + 60, y + 60, x - 60, y + 60);
			g.drawLine(x - 60, y + 60, x - 60, y - 60);

			g.drawString("a", x, y - 62);
			g.drawString("a", x, y + 70);
			g.drawString("a", x - 70, y);
			g.drawString("a", x + 62, y);

			g.setColor(Color.red);
			g.drawLine(x - 60, y - 60, x + 60, y + 60);

			g.drawString("e", x + 1, y);
			System.out.println("Quadrat gemalt");
		}
		if (Option.equalsIgnoreCase("Rechteck")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;

			g.setColor(Color.gray);
			g.drawRect(0, 0, 190, 271);

			g.setColor(Color.red);

			g.drawLine(x - 80, y - 40, x + 80, y - 40);
			g.drawLine(x + 80, y - 40, x + 80, y + 40);
			g.drawLine(x + 80, y + 40, x - 80, y + 40);
			g.drawLine(x - 80, y + 40, x - 80, y - 40);
			System.out.println("Rechteck gemalt");
		}

	}

	public String getMethod() {
		return Option;
	}

	public void setMethod(String Method) {
		this.Option = Method;
	}
}


Panel4
Code:
class Panel4 {

	private JPanel JP_Links;

	public static Zeichenflaeche canvas;

	JPanel createPanel() {
		
		canvas = new Zeichenflaeche();
		canvas.setBounds(5, 15, 190, 271);

		JP_Links.add(canvas);

		JP_Links = new JPanel();
		JP_Links.setLayout(null);
		JP_Links.setVisible(true);
		JP_Links.setBorder(BorderFactory.createTitledBorder("Skizze"));
		JP_Links.setBounds(300, 350, 200, 290);


		return JP_Links;
	}

	public static void main(String[] args) {
		canvas.setMethod("Rechteck");
	}

	public void update() {
		canvas.repaint();
		System.out.println("Update Erfolgreich");
	}

}


Geometrie
Code:
public class Geometrie {

	private static Zeichenflaeche zeichenflaeche;

	public static void main(String[] args) {

		GUI Gui = new GUI();
		Menu m = new Menu();

		Gui.init();
		Gui.setJMenuBar(m);

		final Panel1 panel1 = new Panel1();
		final Panel2 panel2 = new Panel2();
		final Panel3 panel3 = new Panel3();
		final Panel4 panel4 = new Panel4();

		final JPanel P1 = panel1.createPanel(1);
		final JPanel P2 = panel2.createPanel(2);
		final JPanel P3 = panel3.createPanel(3);
		final JPanel P4 = panel4.createPanel();

		Gui.add(P1);
		Gui.add(P2);
		Gui.add(P3);
		Gui.add(P4);
		Gui.add(zeichenflaeche);

		Gui.setVisible(true);

		m.JMI_TQuadraht.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				P1.setVisible(true);
				P1.setBorder(BorderFactory.createTitledBorder("Diagonale"));
				P2.setVisible(true);
				P2.setBorder(BorderFactory.createTitledBorder("Flächeninhalt"));
				P3.setVisible(true);
				P3.setBorder(BorderFactory.createTitledBorder("Umfang"));

				panel1.JTF_Zahl1.setVisible(true);
				panel1.JTF_Zahl2.setVisible(false);
				panel1.JTF_Zahl3.setVisible(false);
				panel1.JTF_Zahl4.setVisible(false);

				panel1.JL_Zahl1.setText("Seite A");

				panel1.JL_Zahl1.setVisible(true);
				panel1.JL_Zahl2.setVisible(false);
				panel1.JL_Zahl3.setVisible(false);
				panel1.JL_Zahl4.setVisible(false);

				panel2.JTF_Zahl1.setVisible(true);
				panel2.JTF_Zahl2.setVisible(false);
				panel2.JTF_Zahl3.setVisible(false);
				panel2.JTF_Zahl4.setVisible(false);

				panel2.JL_Zahl1.setText("Seite A");

				panel2.JL_Zahl1.setVisible(true);
				panel2.JL_Zahl2.setVisible(false);
				panel2.JL_Zahl3.setVisible(false);
				panel2.JL_Zahl4.setVisible(false);

				panel3.JTF_Zahl1.setVisible(true);
				panel3.JTF_Zahl2.setVisible(false);
				panel3.JTF_Zahl3.setVisible(false);
				panel3.JTF_Zahl4.setVisible(false);

				panel3.JL_Zahl1.setText("Seite A");

				panel3.JL_Zahl1.setVisible(true);
				panel3.JL_Zahl2.setVisible(false);
				panel3.JL_Zahl3.setVisible(false);
				panel3.JL_Zahl4.setVisible(false);

				panel1.Option = "Quadraht_Diagonale";
				panel2.Option = "Quadraht_Flächeninhalt";
				panel3.Option = "Quadraht_Umfang";
				zeichenflaeche.Option = "Quadraht";
			}
		});

		m.JMI_TRechteck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				P1.setVisible(true);
				P1.setBorder(BorderFactory.createTitledBorder("Flächeninhalt"));
				P2.setVisible(true);
				P2.setBorder(BorderFactory.createTitledBorder("Umfang"));
				P3.setVisible(true);
				P3.setBorder(BorderFactory.createTitledBorder("Diagonale"));

				panel1.JTF_Zahl1.setVisible(true);
				panel1.JTF_Zahl2.setVisible(true);
				panel1.JTF_Zahl3.setVisible(false);
				panel1.JTF_Zahl4.setVisible(false);

				panel1.JL_Zahl1.setText("Seite A");
				panel1.JL_Zahl2.setText("Seite B");

				panel1.JL_Zahl1.setVisible(true);
				panel1.JL_Zahl2.setVisible(true);
				panel1.JL_Zahl3.setVisible(false);
				panel1.JL_Zahl4.setVisible(false);

				panel2.JTF_Zahl1.setVisible(true);
				panel2.JTF_Zahl2.setVisible(true);
				panel2.JTF_Zahl3.setVisible(false);
				panel2.JTF_Zahl4.setVisible(false);

				panel2.JL_Zahl1.setText("Seite A");
				panel1.JL_Zahl2.setText("Seite B");

				panel2.JL_Zahl1.setVisible(true);
				panel2.JL_Zahl2.setVisible(true);
				panel2.JL_Zahl3.setVisible(false);
				panel2.JL_Zahl4.setVisible(false);

				panel3.JTF_Zahl1.setVisible(true);
				panel3.JTF_Zahl2.setVisible(true);
				panel3.JTF_Zahl3.setVisible(false);
				panel3.JTF_Zahl4.setVisible(false);

				panel3.JL_Zahl1.setText("Seite A");
				panel1.JL_Zahl2.setText("Seite B");

				panel3.JL_Zahl1.setVisible(true);
				panel3.JL_Zahl2.setVisible(true);
				panel3.JL_Zahl3.setVisible(false);
				panel3.JL_Zahl4.setVisible(false);

				zeichenflaeche.setMethod("Rechteck");
				System.out.println("Aktuelle Methode: "+zeichenflaeche.getMethod());
				zeichenflaeche.repaint();
			}
		});

	}
}
 
Ich hab's kurz überflogen - wildes Teil 😉
Diese JPanel, die ein Canvas reingewürgt kriegen solltest Du wieder ändern, das geht auf Dauer nicht gut. Stattdessen kannst Du auch direkt in JPanel zeichnen, hat auch den Vorteil, daß die Dinger doppelt gebuffert sind.
Außerdem hat mehr als eine Klasse eine main-Methode - welche wird denn nun aufgerufen?

Noch etwas zum Mix von Canvas und JPanel: Die update-Methode, die Du in Panel4 überschrieben hast, würde ich mal rausschmeißen. Lt. API macht nämlich ein repaint() folgendes:

If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.

Somit könnte durch das Mischen von Canvas und JPanel hier durchaus eine Schleife entstehen, wenn Dein repaint nur wieder ein update aufruft.

Ich mag um die Uhrzeit so wilde Programme nicht mehr ganz und gar aufdröseln. Mein Rat: Probiere es zunächst im Kleinen und wende es dann auf Dein Programm an.

Hier mal spaßeshalber das kleine Programm, mit dem ich Deine Paint-Methode getestet habe:



Code:
import java.awt.*;
import javax.swing.JFrame;

public class Test extends JFrame {

	String options = "Rechteck";
	
	public static void main(String[] args) {
		new Test();
	}

	public Test() {
		super("test");
		setLocation(100,100);
		setSize(600,600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setVisible(true);
	}

	public void paint(Graphics g) {
		
		super.paint(g);

		if (options.equals("Quadraht")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;

			g.setColor(Color.black);

			g.drawLine(x - 60, y - 60, x + 60, y - 60);
			g.drawLine(x + 60, y - 60, x + 60, y + 60);
			g.drawLine(x + 60, y + 60, x - 60, y + 60);
			g.drawLine(x - 60, y + 60, x - 60, y - 60);

			g.drawString("a", x, y - 62);
			g.drawString("a", x, y + 70);
			g.drawString("a", x - 70, y);
			g.drawString("a", x + 62, y);

			g.setColor(Color.red);
			g.drawLine(x - 60, y - 60, x + 60, y + 60);

			g.drawString("e", x + 1, y);
		}
		if (options.equals("Rechteck")) {

			int x = getSize().width / 2;
			int y = getSize().height / 2;

			g.setColor(Color.gray);
			g.fillRect(0, 0, 190, 271);

			g.setColor(Color.black);

			g.drawLine(x - 80, y - 40, x + 80, y - 40);
			g.drawLine(x + 80, y - 40, x + 80, y + 40);
			g.drawLine(x + 80, y + 40, x - 80, y + 40);
			g.drawLine(x - 80, y + 40, x - 80, y - 40);
		}
	}
}
 
Ich habe das einfach mal lauffähig gemacht, damit man sieht, worüber man spricht.
Nur, was ist jetzt dein Problem nochmal?
Oder hat es sich hier schon aufgelöst:
Code:
package geo;
/*
* Geometrie.java
*/
//import basics.GUI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Geometrie {
    private static Zeichenflaeche zeichenflaeche;
    public static void main(String[] args) {
        GUI gui = new GUI();
        Menu m = new Menu("Menu");
        JMenuBar bar = new JMenuBar();
        bar.add(m);
        gui.init();
        gui.setJMenuBar(bar);
        final Panel1 panel1 = new Panel1();
        final Panel2 panel2 = new Panel2();
        final Panel3 panel3 = new Panel3();
        final Panel4 panel4 = new Panel4();
        final JPanel p1 = panel1.createPanel(1);
        final JPanel p2 = panel2.createPanel(2);
        final JPanel p3 = panel3.createPanel(3);
        final JPanel p4 = panel4.createPanel();
        gui.add(p1);
        gui.add(p2);
        gui.add(p3);
        gui.add(p4);
        zeichenflaeche = new Zeichenflaeche();
        gui.add(zeichenflaeche);
        gui.setVisible(true);
        m.jmi_TQuadraht.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                p1.setVisible(true);
                p1.setBorder(BorderFactory.createTitledBorder("Diagonale"));
                p2.setVisible(true);
                p2.setBorder(BorderFactory.createTitledBorder("Flächeninhalt"));
                p3.setVisible(true);
                p3.setBorder(BorderFactory.createTitledBorder("Umfang"));
                panel1.jtf_Zahl1.setVisible(true);
                panel1.jtf_Zahl2.setVisible(false);
                panel1.jtf_Zahl3.setVisible(false);
                panel1.jtf_Zahl4.setVisible(false);
                panel1.jl_Zahl1.setText("Seite A");
                panel1.jl_Zahl1.setVisible(true);
                panel1.jl_Zahl2.setVisible(false);
                panel1.jl_Zahl3.setVisible(false);
                panel1.jl_Zahl4.setVisible(false);
                panel2.jtf_Zahl1.setVisible(true);
                panel2.jtf_Zahl2.setVisible(false);
                panel2.jtf_Zahl3.setVisible(false);
                panel2.jtf_Zahl4.setVisible(false);
                panel2.jl_Zahl1.setText("Seite A");
                panel2.jl_Zahl1.setVisible(true);
                panel2.jl_Zahl2.setVisible(false);
                panel2.jl_Zahl3.setVisible(false);
                panel2.jl_Zahl4.setVisible(false);
                panel3.jtf_Zahl1.setVisible(true);
                panel3.jtf_Zahl2.setVisible(false);
                panel3.jtf_Zahl3.setVisible(false);
                panel3.jtf_Zahl4.setVisible(false);
                panel3.jl_Zahl1.setText("Seite A");
                panel3.jl_Zahl1.setVisible(true);
                panel3.jl_Zahl2.setVisible(false);
                panel3.jl_Zahl3.setVisible(false);
                panel3.jl_Zahl4.setVisible(false);
                panel1.option = "Quadraht_Diagonale";
                panel2.option = "Quadraht_Flächeninhalt";
                panel3.option = "Quadraht_Umfang";
                zeichenflaeche.option = "Quadrat";
                zeichenflaeche.repaint();
            }
        });
        m.jmi_TRechteck.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                p1.setVisible(true);
                p1.setBorder(BorderFactory.createTitledBorder("Flächeninhalt"));
                p2.setVisible(true);
                p2.setBorder(BorderFactory.createTitledBorder("Umfang"));
                p3.setVisible(true);
                p3.setBorder(BorderFactory.createTitledBorder("Diagonale"));
                panel1.jtf_Zahl1.setVisible(true);
                panel1.jtf_Zahl2.setVisible(true);
                panel1.jtf_Zahl3.setVisible(false);
                panel1.jtf_Zahl4.setVisible(false);
                panel1.jl_Zahl1.setText("Seite A");
                panel1.jl_Zahl2.setText("Seite B");
                panel1.jl_Zahl1.setVisible(true);
                panel1.jl_Zahl2.setVisible(true);
                panel1.jl_Zahl3.setVisible(false);
                panel1.jl_Zahl4.setVisible(false);
                panel2.jtf_Zahl1.setVisible(true);
                panel2.jtf_Zahl2.setVisible(true);
                panel2.jtf_Zahl3.setVisible(false);
                panel2.jtf_Zahl4.setVisible(false);
                panel2.jl_Zahl1.setText("Seite A");
                panel1.jl_Zahl2.setText("Seite B");
                panel2.jl_Zahl1.setVisible(true);
                panel2.jl_Zahl2.setVisible(true);
                panel2.jl_Zahl3.setVisible(false);
                panel2.jl_Zahl4.setVisible(false);
                panel3.jtf_Zahl1.setVisible(true);
                panel3.jtf_Zahl2.setVisible(true);
                panel3.jtf_Zahl3.setVisible(false);
                panel3.jtf_Zahl4.setVisible(false);
                panel3.jl_Zahl1.setText("Seite A");
                panel1.jl_Zahl2.setText("Seite B");
                panel3.jl_Zahl1.setVisible(true);
                panel3.jl_Zahl2.setVisible(true);
                panel3.jl_Zahl3.setVisible(false);
                panel3.jl_Zahl4.setVisible(false);
                zeichenflaeche.setMethod("Rechteck");
                System.out.println("Aktuelle Methode: "+zeichenflaeche.getMethod());
                zeichenflaeche.repaint();
            }
        });
    }
}
class Panel4 {
    private JPanel jp_Links;
    public static Zeichenflaeche zeichenflaeche;
    JPanel createPanel() {
        zeichenflaeche = new Zeichenflaeche();
        zeichenflaeche.setBounds(5, 15, 190, 271);
        jp_Links = new JPanel();
        jp_Links.add(zeichenflaeche);
        jp_Links.setLayout(null);
        jp_Links.setVisible(true);
        jp_Links.setBorder(BorderFactory.createTitledBorder("Skizze"));
        jp_Links.setBounds(300, 350, 200, 290);
        return jp_Links;
    }
}
class Zeichenflaeche extends JPanel {
    private static final long serialVersionUID = 1L;
    public String option = "";
    public Zeichenflaeche() {
        super();
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.GRAY);
        System.out.println("Ich male jetzt: " + option);
        if (option.equals("Quadrat")) {
            int x = getSize().width / 2;
            int y = getSize().height / 2;
            g.setColor(Color.black);
            g.drawLine(x - 60, y - 60, x + 60, y - 60);
            g.drawLine(x + 60, y - 60, x + 60, y + 60);
            g.drawLine(x + 60, y + 60, x - 60, y + 60);
            g.drawLine(x - 60, y + 60, x - 60, y - 60);
            g.drawString("a", x, y - 62);
            g.drawString("a", x, y + 70);
            g.drawString("a", x - 70, y);
            g.drawString("a", x + 62, y);
            g.setColor(Color.red);
            g.drawLine(x - 60, y - 60, x + 60, y + 60);
            g.drawString("e", x + 1, y);
            System.out.println("Quadrat gemalt");
        }
        if (option.equalsIgnoreCase("Rechteck")) {
            int x = getSize().width / 2;
            int y = getSize().height / 2;
            g.setColor(Color.gray);
            g.drawRect(0, 0, 190, 271);
            g.setColor(Color.red);
            g.drawLine(x - 80, y - 40, x + 80, y - 40);
            g.drawLine(x + 80, y - 40, x + 80, y + 40);
            g.drawLine(x + 80, y + 40, x - 80, y + 40);
            g.drawLine(x - 80, y + 40, x - 80, y - 40);
            System.out.println("Rechteck gemalt");
        }
    }
    public String getMethod() {
        return option;
    }
    public void setMethod(String Method) {
        this.option = Method;
    }
}
class Panel1 extends JPanel{
    JTextField jtf_Zahl1 = new JTextField(5);
    JTextField jtf_Zahl2 = new JTextField(5);
    JTextField jtf_Zahl3 = new JTextField(5);
    JTextField jtf_Zahl4 = new JTextField(5);
    JLabel jl_Zahl1 = new JLabel("jl_Zahl1");
    JLabel jl_Zahl2 = new JLabel("jl_Zahl2");
    JLabel jl_Zahl3 = new JLabel("jl_Zahl3");
    JLabel jl_Zahl4 = new JLabel("jl_Zahl4");
    String option;
    public Panel1(){
        add(jl_Zahl1);
        add(jtf_Zahl1);
        add(jl_Zahl2);
        add(jtf_Zahl2);
        add(jl_Zahl3);
        add(jtf_Zahl3);
        add(jl_Zahl4);
        add(jtf_Zahl4);
    }
    public JPanel createPanel(int i) {
        return this;
    }
}
class Panel2 extends JPanel{
    JTextField jtf_Zahl1 = new JTextField(5);
    JTextField jtf_Zahl2 = new JTextField(5);
    JTextField jtf_Zahl3 = new JTextField(5);
    JTextField jtf_Zahl4 = new JTextField(5);
    JLabel jl_Zahl1 = new JLabel("jl_Zahl1");
    JLabel jl_Zahl2 = new JLabel("jl_Zahl2");
    JLabel jl_Zahl3 = new JLabel("jl_Zahl3");
    JLabel jl_Zahl4 = new JLabel("jl_Zahl4");
    String option;
    public Panel2(){
        add(jl_Zahl1);
        add(jtf_Zahl1);
        add(jl_Zahl2);
        add(jtf_Zahl2);
        add(jl_Zahl3);
        add(jtf_Zahl3);
        add(jl_Zahl4);
        add(jtf_Zahl4);
    }
    public JPanel createPanel(int i) {
        return this;
    }
}
class Panel3 extends JPanel{
    JTextField jtf_Zahl1 = new JTextField(5);
    JTextField jtf_Zahl2 = new JTextField(5);
    JTextField jtf_Zahl3 = new JTextField(5);
    JTextField jtf_Zahl4 = new JTextField(5);
    JLabel jl_Zahl1 = new JLabel("jl_Zahl1");
    JLabel jl_Zahl2 = new JLabel("jl_Zahl2");
    JLabel jl_Zahl3 = new JLabel("jl_Zahl3");
    JLabel jl_Zahl4 = new JLabel("jl_Zahl4");
    String option;
    public Panel3(){
        add(jl_Zahl1);
        add(jtf_Zahl1);
        add(jl_Zahl2);
        add(jtf_Zahl2);
        add(jl_Zahl3);
        add(jtf_Zahl3);
        add(jl_Zahl4);
        add(jtf_Zahl4);
    }
    public JPanel createPanel(int i) {
        return this;
    }
}
class GUI extends JFrame{
    public void init() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(800,600);
        setLocationRelativeTo(null);
    }
}
class Menu extends JMenu{
    JMenuItem jmi_TQuadraht = new JMenuItem("Quadrat");
    JMenuItem jmi_TRechteck = new JMenuItem("Rechteck");
    public Menu(String title){
        super(title);
        add(jmi_TQuadraht);
        add(jmi_TRechteck);
    }
}
 
Hey vielen Dank, 😀
in deinem Entwurf hat sich das Problem aufgelöst, jetzt muss ich nur noch den Fehler finden :toll:
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben