Hallo,
für ein aktuelles Projekt, will ich mehrere RoundRectangles zeichnen.
Ich habe bereits ein JFrame und eine Menüleiste, die super funktionieren. Wenn ich im Menü auf "Create Sphere" klicke, erscheint ein Dialog, wo man mit dem ColorChooser die Farbe auswählen kann. Dann muss man Namen, width&height und x-/y-Koordinaten angeben. Nachdem ich die Methode aufrufe, erhalte ich auch korrekterweise mein RoundedReactangle:

Wenn ich ein zweites erstellen will, klappt es auch, aber das erste verschwindet nun:

Wenn ich das Fenster mir der Maus resize, dann taucht das erste wieder auf und das zweite verschwindet.
[CODE lang="java" title="JFrame:"] public void createWindow() {
/** Frame */
this.setTitle("Diagram");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
}[/CODE]
[CODE lang="java" title="Die createSphere-Methode, welche durch das MenuItem aufgerufen wird:"]public void createSphere(String name, int width, int height, int x, int y, Color color) {
this.add(new Sphere(name, width, height, x, y, color));
this.validate();
}[/CODE]
[CODE lang="java" title="Meine Sphere-Klasse"]public class Sphere extends JPanel {
private String name;
private int width;
private int height;
private int xPos;
private int yPos;
private Color color;
public Sphere(String name, int width, int height, int xPos, int yPos, Color color) {
this.name = name;
this.width = width;
this.height = height;
this.xPos = xPos;
this.yPos = yPos;
this.color = color;
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.drawRoundRect(this.xPos,this.yPos,this.width,this.height, 15, 15);
g.setColor(this.color);
g.fillRoundRect(this.xPos,this.yPos,this.width,this.height, 15, 15);
}[/CODE]
Ich will mit dem Programm mehrere Rechtecke zeichnen können. Ich bin seit gestern Nachmittag auf Fehlersuche, aber leider klappt für mich nichts.
für ein aktuelles Projekt, will ich mehrere RoundRectangles zeichnen.
Ich habe bereits ein JFrame und eine Menüleiste, die super funktionieren. Wenn ich im Menü auf "Create Sphere" klicke, erscheint ein Dialog, wo man mit dem ColorChooser die Farbe auswählen kann. Dann muss man Namen, width&height und x-/y-Koordinaten angeben. Nachdem ich die Methode aufrufe, erhalte ich auch korrekterweise mein RoundedReactangle:

Wenn ich ein zweites erstellen will, klappt es auch, aber das erste verschwindet nun:

Wenn ich das Fenster mir der Maus resize, dann taucht das erste wieder auf und das zweite verschwindet.
[CODE lang="java" title="JFrame:"] public void createWindow() {
/** Frame */
this.setTitle("Diagram");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
}[/CODE]
[CODE lang="java" title="Die createSphere-Methode, welche durch das MenuItem aufgerufen wird:"]public void createSphere(String name, int width, int height, int x, int y, Color color) {
this.add(new Sphere(name, width, height, x, y, color));
this.validate();
}[/CODE]
[CODE lang="java" title="Meine Sphere-Klasse"]public class Sphere extends JPanel {
private String name;
private int width;
private int height;
private int xPos;
private int yPos;
private Color color;
public Sphere(String name, int width, int height, int xPos, int yPos, Color color) {
this.name = name;
this.width = width;
this.height = height;
this.xPos = xPos;
this.yPos = yPos;
this.color = color;
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.drawRoundRect(this.xPos,this.yPos,this.width,this.height, 15, 15);
g.setColor(this.color);
g.fillRoundRect(this.xPos,this.yPos,this.width,this.height, 15, 15);
}[/CODE]
Ich will mit dem Programm mehrere Rechtecke zeichnen können. Ich bin seit gestern Nachmittag auf Fehlersuche, aber leider klappt für mich nichts.