Ich habe folgendes Beispiel...
Nun habe ich allerdings noch ne Klasse die von Elements erbt... Aber wie mache ich das, das von der Klasse Question die Paint auch aufgrerufen wird...
Danke...schon im vorraus
Java:
public class Main extends JFrame implements Runnable {
public Element[][] elements;
public Main() {
super();
elements = new Element[10][10];
thread = new Thread(this);
thread.start();
this.setTitle("...");
this.setSize(size, size);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation((getToolkit().getScreenSize().width-size)/2, (getToolkit().getScreenSize().height-size)/2);
this.getContentPane().setLayout(new GridLayout(10, 10));
for(int i = 0; i < elements.length; i++) {
for(int j = 0; j < elements[i].length; j++) {
Question e = new Question(this, text, j, i);
this.getContentPane().add(e);
elements[i][j] = e;
}
}
this.setVisible(true);
}
public static void main(String[] arguments) {
Main main = new Main();
}
public void run() {
while (true) {
try {
thread.sleep(200);
} catch(InterruptedException e) {
System.out.println("ERROR: " + e.toString());
}
repaint();
}
}
}
Java:
public class Element extends JPanel {
public Main main;
private int x, y;
public Element(Main main, int x, int y) {
super();
this.main = main;
this.x = x;
this.y = y;
this.setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
super.paintComponent(g2d);
g2d.draw(new Rectangle2D.Double(1, 1, this.getWidth()-2, this.getHeight()-2));
}
}
Nun habe ich allerdings noch ne Klasse die von Elements erbt... Aber wie mache ich das, das von der Klasse Question die Paint auch aufgrerufen wird...
Java:
public class Question extends Element {
private String text;
public Question(Main main, String text, int x, int y) {
super(main, x, y);
this.text = text;
}
public void paint() {
g2d.drawString(.....);
}
}
Danke...schon im vorraus
Zuletzt bearbeitet: