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
[0] =
Integer.
parseInt(st.
nextToken());
yf
[0] =
Integer.
parseInt(st.
nextToken());
xf
[1] =
Integer.
parseInt(st.
nextToken());
yf
[1] =
Integer.
parseInt(st.
nextToken());
xf
[2] =
Integer.
parseInt(st.
nextToken());
yf
[2] =
Integer.
parseInt(st.
nextToken());
Dreieck dr = new Dreieck (xf,yf,3);
dr.paint(mycan.getGraphics());
landschaft.add(current_to_save);
}
System.
out.
println("Figur wurde korrekt gezeichnet :-)");
}
Sie haben bei der Eingabe gepatzt.(Falsche Form!)");}
Sie haben zuwenig Parameter eingeben.");}
}
public void safe_landschaft(){
try {
for (int i=0; i<landschaft.size(); i++){
pw.
println((String)(landschaft.
elementAt(i
)));
}
pw.close();
}
catch (IOException io
) { System.
err.
println("Mann, Sie haben ein Fehler beim speichern verusacht, grrr !");
} }
public void load_and_paint(){
try {
while ((zeile = br.readLine()) != null) {
//while (br.ready()) {
//zeile = br.readLine(); // ginge auch !!
String figur = st.
nextToken();
parameter = (parameter+st.nextToken()); // Name mit ":" abgetrennt , Parameter mit ","
System.
out.
println("Gefunden: "+figur+
":"+parameter
);
parse_and_paint(parameter,figur);
}
br.close();
System.
out.
println("Erfolgreich geladen, Datei wieder geschlossen.");
}
catch (IOException io
) { System.
err.
println("Mann, Sie haben ein Fehler beim laden verusacht, grrr !");
} }
// Versprechen an ItemListener
if (ie.
getStateChange() ==
ItemEvent.
SELECTED){ if (ie.getSource() == schwarz)
mycan.
getGraphics().
setColor(Color.
black);
else if (ie.getSource() == blau)
mycan.
getGraphics().
setColor(Color.
blue);
// Kein Effekt ? else if (ie.getSource() == rot) // getItem wird ja gar ned erfüllt !
System.
out.
println("ItemEvent: Farbe ROT !!!!! zefix");
//mycan.getGraphics().setColor(Color.red);
else if (ie.getSource() == gruen)
mycan.
getGraphics().
setColor(Color.
green);
else if (ie.getSource() == gelb)
mycan.
getGraphics().
setColor(Color.
yellow);
System.
out.
println("ItemEvent: Farbe geaendert.");
// (erfolglose) tests zur Problemfindung
mycan.
getGraphics().
setColor( Color.
blue );
mycan.getGraphics().drawRect(50,50,70,70);
}
}
// Versprechen an AcionListener
if (e.getSource()== zeichnen)
//myDialog1 dia1 = new myDialog1(this,"Figur wählen",true); // SOWAS GEHT NICHT ???????
new myDialog1(this,"Figur wählen",true);
else if (e.getSource()== speichern){
safe_landschaft();
System.
out.
println("ActionEvent: Datei landschaft.txt erflolgreich erstellt.");
}
else if (e.getSource()== lade_und_male){
System.
out.
println("ActionEvent: Lade landschaft.txt");
load_and_paint();
}
}
// Versprechen an MouseMotionListener
long z = 0;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
public void mouseDragged
(MouseEvent e
) { // Spielerei g = mycan.getGraphics();
z = z+1;
//System.out.println ("mouseMoved " + e);
if (z%2 == 0){
x2 = e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
}
else {
x1 = e.getX();
y1 = e.getY();
g.drawLine(x2, y2, x1, y1);
}
}
public static void main
(String []args
){ x_mas_nr5 mein_programmObjekt = new x_mas_nr5();
}
}