Hi,
ich wollte mich mal ein wenig mit Java Swing beschäftigen, habe jedoch zurzeit folgendes Problem.
Ich wollte eine Klasse Grafik erstellen, mithilfe welcher ich universell ein Fenster mit mehreren Formen zeichnen lassen will.
Meine Grafikklasse sieht dabei wie folgt aus:
Aufgerufen wird das ganze hier:
Wenn ich so wie oben nur ein KreisPanel hinzufügen will, funktioniert das ganze auch.
Wenn ich jetzt jedoch außerdem noch ein LiniePanel hinzufügen will, dann sehe ich gar nicht mehr, weder den Kreis noch die Linie
Das ganze sah dann so aus:
ich wollte mich mal ein wenig mit Java Swing beschäftigen, habe jedoch zurzeit folgendes Problem.
Ich wollte eine Klasse Grafik erstellen, mithilfe welcher ich universell ein Fenster mit mehreren Formen zeichnen lassen will.
Meine Grafikklasse sieht dabei wie folgt aus:
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class Grafik extends JPanel {
public Grafik() {
}
public static JFrame neuesFenster() {
return neuesFenster("standard", 400, 400);
}
public static JFrame neuesFenster(String titel, int breite, int hoehe) {
JFrame jFrame = new JFrame(titel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setResizable(false);
jFrame.setLocationRelativeTo(null);
jFrame.setSize(breite, hoehe);
return jFrame;
}
public static void sichtbarMachen(JFrame jFrame) {
jFrame.setVisible(true);
}
public static JPanel zeichneKreis(int x, int y, int r) {
KreisPanel kreisPanel = new KreisPanel(x, y, r);
JPanel panel = (JPanel) kreisPanel;
return panel;
}
public static JPanel zeichneLinie(int x1, int y1, int x2, int y2) {
LiniePanel liniePanel = new LiniePanel(x1, y1, x2, y2);
JPanel panel = (JPanel) liniePanel;
return panel;
}
public static void panelHinzufuegen(JFrame frame, JPanel panel) {
frame.add(panel);
}
}
class KreisPanel extends JPanel {
int x;
int y;
int r;
public KreisPanel(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
@Override
public void paintComponents(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.setColor(Color.BLACK);
Ellipse2D.Double kreis = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
g2d.draw(kreis);
}
}
class LiniePanel extends JPanel {
int x1;
int y1;
int x2;
int y2;
public LiniePanel(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.setColor(Color.BLACK);
Line2D linie = new Line2D.Double(x1, y1, x2, y2);
g2d.draw(linie);
}
}
Aufgerufen wird das ganze hier:
Java:
import javax.swing.*;
public class Main {
public static void Main(String[] args) {
JFrame frame = Grafik.neuesFenster("Test", 1000, 600);
JPanel kreis = Grafik.zeichneKreis(10, 10, 10);
Grafik.panelHinzufuegen(frame, kreis);
Grafik.sichtbarMachen(frame);
}
}
Wenn ich so wie oben nur ein KreisPanel hinzufügen will, funktioniert das ganze auch.
Wenn ich jetzt jedoch außerdem noch ein LiniePanel hinzufügen will, dann sehe ich gar nicht mehr, weder den Kreis noch die Linie
Das ganze sah dann so aus:
Java:
import javax.swing.*;
public class Main {
public static void Main(String[] args) {
JFrame frame = Grafik.neuesFenster("Test", 1000, 600);
JPanel kreis = Grafik.zeichneKreis(10, 10, 10);
JPanel linie = Grafik.zeichneLinie(10, 10, 50, 50);
Grafik.panelHinzufuegen(frame, kreis);
Grafik.panelHinzufuegen(frame, linie);
Grafik.sichtbarMachen(frame);
}
}