Hi,
mal folgender Beispielcode:
Ich möchte die Daten von k1, k2 und k3 in einem Array Speichern (muss später noch feststellen können ob ein Mausklick in einem der Kreise lag und wenn ja in welchem Kreis)
und mit dem Array möchte ich die 3 Objekte auf erstellen(for-Schleife oder so).
hat Jemand eine Idee wie man das elegant lösen kann?
mal folgender Beispielcode:
Code:
import java.applet.Applet;
import java.awt.*;
public class MyApplet extends Applet {
Kreis k1, k2, k3;
Viereck v;
public void start() {
k1 = new Kreis(10, 10, 30, 30, Color.RED);
k2 = new Kreis(45, 10, 30, 30, Color.BLUE);
k3 = new Kreis(80, 10, 30, 30, Color.ORANGE);
v = new Viereck(10, 50, 20, 40, Color.GRAY);
}
public void paint(Graphics g) {
k1.paint(g);
k2.paint(g);
k3.paint(g);
v.paint(g);
}
}
Code:
public class Kreis {
private int x = 0;
private int y = 0;
private int breite = 0;
private int hoehe = 0;
private java.awt.Color farbe;
public Kreis(int x, int y, int breite, int hoehe, java.awt.Color farbe) {
this.x = x;
this.y = y;
this.breite = breite;
this.hoehe = hoehe;
this.farbe = farbe;
}
public void paint(java.awt.Graphics g) {
g.setColor(this.farbe);
g.fillOval(this.x, this.y, this.breite, this.hoehe);
}
}
Code:
public class Viereck {
private int x = 0;
private int y = 0;
private int breite = 0;
private int hoehe = 0;
private java.awt.Color farbe;
public Viereck(int x, int y, int breite, int hoehe, java.awt.Color farbe) {
this.x = x;
this.y = y;
this.breite = breite;
this.hoehe = hoehe;
this.farbe = farbe;
}
public void paint(java.awt.Graphics g) {
g.setColor(this.farbe);
g.fillRect(this.x, this.y, this.breite, this.hoehe);
}
}
Ich möchte die Daten von k1, k2 und k3 in einem Array Speichern (muss später noch feststellen können ob ein Mausklick in einem der Kreise lag und wenn ja in welchem Kreis)
und mit dem Array möchte ich die 3 Objekte auf erstellen(for-Schleife oder so).
hat Jemand eine Idee wie man das elegant lösen kann?