import java.awt.event.*;
import java.awt.*;
import java.awt.Polygon;
import java.util.*;
import java.io.*;
interface GeometrieObjekt {
public void paint
(Graphics g
);
// Malen public boolean contains (int x, int y); // Enthält das Objekt den Punkt (x, y)?
public String getName
();
// Was bist Du? }
class Kreis implements GeometrieObjekt {
int r; // r = Radius
int mx, my; // mx = x Mittelpunkt, my = y Mittelpunkt
public Kreis (int r, int x, int y) {
this.r = r; mx = x; my = y;
}
// Hart rein funzt es , also der Grafikkontext stimmt
/*g = mycan.getGraphics();
g.setColor( Color.blue );*/
g.drawArc (mx-r, my-r, 2*r, 2*r, 0, 360);
}
public boolean contains (int x, int y) {
return (mx-x)*(mx-x)+(my-y)*(my-y) <= r*r;
}
public String getName
() { return "Kreis";
} }
class RechtEck implements GeometrieObjekt {
int x, y, b, h; // Linke obere Ecke, Breite, Höhe
public RechtEck (int x, int y, int b, int h) {
this.x = x; this.y = y;
this.b = b; this.h = h;
}
g.drawRect (x, y, b, h);
}
public boolean contains (int x, int y) {
return this.x <= x && x <= this.x+b &&
this.y <= y && y <= this.y+h;
}
public String getName
() { return "Rechteck";
} }
class Ellipse implements GeometrieObjekt {
int x, y, b, h;
public Ellipse (int x, int y, int b, int h) {
this.x = x; this.y = y;
this.b = b; this.h = h;
}
g.drawOval(x,y,b,h);
}
public boolean contains (int x, int y) {
return ((x*x/b*b)+(y*y/h*h) != 1 );
}
public String getName
() { return "Ellipse";
} }
class Dreieck implements GeometrieObjekt {
int x[], y[];
int n;
public Dreieck (int []x, int []y,int n){
this.x = x;
this.y = y;
this.n = n;
}
g.drawPolygon(x, y, n);
}
public boolean contains (int x, int y) {
return polygon.contains(x, y);
}
public String getName
() { return "Dreieck";
} }
private java.
awt.
List myList =
new java.
awt.
List(6,
false);
protected String[] geos = {"Kreis","Ellipse","Rechteck","Dreieck"};
// Für Dialog 2
public myDialog1
(Dialog drueber,
String titel,
boolean modal
){ super(drueber,titel,modal);
}
public myDialog1
(Frame parent,
String titel,
boolean modal
){ super(parent, titel, modal);
this.parent = parent;
for (int i=0; i<geos.length; i++)
myList.add(geos[i]);
add(myList);
myList.addItemListener(this);
add("South", back);
back.addActionListener(this);
pack();
setVisible(true);
}
dispose();
new myDialog2(this,myList.getSelectedItem(),parent);
}
if (e.getSource() == back)
dispose();
}
}
super(drueber, "Parameter eingeben (Wichtig:Auf die Form achten !)",true);
this.parent = parent;
this.figur = figur;
if (figur.equals("Kreis"))
vorbelegung = "radius,X-Koordinate von M,Y-Kooradinate von M";
if (figur.equals("Rechteck"))
vorbelegung = ("X-Start,Y-Start;Breite,Höhe");
if (figur.equals("Ellipse"))
vorbelegung = ("X-Start,Y-Start,Breite,Höhe");
if (figur.equals("Dreieck"))
vorbelegung = ("x1,y1,x2,y2,x3,y3");
add(tf);
add("West", back);
add("East", go);
go.addActionListener(this);
back.addActionListener(this);
pack();
setVisible(true);
}
if (e.getSource() == back)
dispose();
if (e.getSource() == go){
System.
out.
println("Ihre Eingabe: "+tf.
getText());
((x_mas_nr5)parent).parse_and_paint(tf.getText(), figur);
dispose();
}
}
}
private int []xf = new int[3];
private int []yf = new int[3];
private int x, y, breite, hoehe, radius;
String current_to_save;
// hat bereits das korrekte Format
// Main-Frame Konstruktor
public x_mas_nr5() {
super("Der Wunschzeichner");
add("South", zeichnen);
add(mycan);
zeichnen.addActionListener(this);
mycan.addMouseMotionListener (this);
initMenu();
setBounds(200, 200, 640,480);
setVisible(true);
System.
err.
println("Schon am Ende ?? Na ok. Bye.");System.
exit (0);
}});
}
public void initMenu(){
myBar.add(myMenu);
myMenu =
new Menu ("Landschafts-Optionen");
myBar.add(myMenu);
myMenu.
add(lade_und_male =
new MenuItem ("Landschaft laden und malen"));
myMenu.
add(speichern =
new MenuItem ("Landschaft speichern"));
schwarz.addItemListener(this);
blau.addItemListener(this);
rot.addItemListener(this);
gruen.addItemListener(this);
gelb.addItemListener(this);
lade_und_male.addActionListener(this);
speichern.addActionListener(this);
setMenuBar(myBar);
}
// Jede gezeichnete Figur wird dem Vector hinzugefügt
try{
current_to_save = (figur+":"+p);
if (figur.equals("Kreis")){
radius =
Integer.
parseInt(st.
nextToken());
x =
Integer.
parseInt(st.
nextToken());
y =
Integer.
parseInt(st.
nextToken());
Kreis k = new Kreis (radius,x,y);
k.paint(mycan.getGraphics()); // wieso geht ned "g" ?
landschaft.add(current_to_save);
}
if (figur.equals("Rechteck")) {
x =
Integer.
parseInt(st.
nextToken());
y =
Integer.
parseInt(st.
nextToken());
breite =
Integer.
parseInt(st.
nextToken());
hoehe =
Integer.
parseInt(st.
nextToken());
RechtEck re = new RechtEck(x,y,breite,hoehe);
re.paint(mycan.getGraphics());
landschaft.add(current_to_save);
}
if (figur.equals("Ellipse")) {
x =
Integer.
parseInt(st.
nextToken());
y =
Integer.
parseInt(st.
nextToken());
breite =
Integer.
parseInt(st.
nextToken());
hoehe =
Integer.
parseInt(st.
nextToken());
Ellipse el = new Ellipse (x,y,breite,hoehe);
el.paint(mycan.getGraphics());
landschaft.add(current_to_save);
}
if (figur.equals("Dreieck")){
xf&#
91;
0&#
93; =
Integer.
parseInt(st.
nextToken());
yf&#
91;
0&#
93; =
Integer.
parseInt(st.
nextToken());
xf&#
91;
1&#
93; =
Integer.
parseInt(st.
nextToken());