(1) Definieren Sie zunächst die zu implementierende Klasse. Ich definiere zunächst eine abstrakte Klasse
Die Codekopie lautet wie folgt:
//Grafik
abstrakte Klassenform {
int x, y;
int x1, y1;
Farbfarbe;
Grafik G;
Byte -Typ;
öffentliche abstrakte Leere (Grafik G);
}
//Gerade
Klasse lineshePes erweitert die Form {
public linesheape (int x, int y, int x1, int y1, Farbe Farbe) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.type = 0;
}
@Override
public void Draw (Grafik g) {
G.SetColor (Farbe);
g.drawline (x, y, x1, y1);
}
}
// Kreisklasse
Klasse ovalshape erweitert die Form {
public ovalshape (int x, int y, int x1, int y1, Farbe Farbe) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.type = 1;
}
@Override
public void Draw (Grafik g) {
G.SetColor (Farbe);
G.Drawoval ((x+x1)/2, (y+y1)/2, (x1-x)/2, (y1-y)/2);
}
}
// Bilder
Das Klassenbild erweitert die Form {
int a;
int b;
Int Cl [] [];
öffentliches Bild (int a, int b, int cl [] []) {
this.type = 2;
this.a = a;
this.b = b;
this.cl = cl;
}
public void Draw (Grafik g) {
für (int k = 0; k <a; k ++) {
für (int j = 0; j <b; j ++) {
Farbe c = neue Farbe (Cl [k] [j]);
G.SetColor (c);
G.Drawline (K+100, J+100, K+100, J+100);
}
}
}
}
(2) Gesamtmethodenklasse
Die Codekopie lautet wie folgt:
öffentliche Klasse bmpsavestart {
Grafik G;
public static void main (String [] args) {
neuer bmpsavestart () .ui ();
}
public void ui () {
JFrame JF = new JFrame ();
Jf.Setsize (600.600);
Jf.Settitle ("Speicher des Bildes");
Jf.Setbackground (color.white);
JMenubar bar = new JMenubar ();
JMenu -Menü1 = new JMenu ("Option");
JMenuitem M1 = neuer JMenuitem ("Zeichnen Sie einen Kreis zeichnen");
JMenuitem M2 = New JMenuitem ("Zeichnenlinie");
JMenuitem M3 = New JMenuitem ("Storage");
JMenuitem M4 = New JMenuitem ("Open");
JMenuitem M5 = New JMenuitem ("Bild");
Menü1.Add (M1);
Menü1.Add (M2);
Menü1.Add (M3);
Menü1.Add (M4);
Menü1.Add (M5);
bar.add (Menü1);
Jf.SetJMenubar (Bar);
Jf.SetDefaultCloseOperation (3);
jf.setvisible (true);
PaintDemo p = neuer PaintDemo (G, JF);
M1.SetactionCommand ("Zeichnen Sie einen Kreis zeichnen");
M2.SetactactionCommand ("Zeichnenlinie");
M3.SetactactionCommand ("Speicher");
M4.SetactactionCommand ("Open");
M5.SetactactionCommand ("Bild");
M1.AddactionListener (P);
M2.AddactionListener (p);
M3.AddactionListener (P);
M4.AddactionListener (p);
M5.AddactionListener (p);
}
}
(3) Überwachungsklasse
Die Codekopie lautet wie folgt:
Klasse PaintDemo implementiert Mouselistener, ActionListener {
ImageCon img = new ImageCon ("Bilder/100.gif");
Bufferedimage bufimage = new bufferedImage (img.geticonwidth (), img.geticonHeight (), bufferedimage.type_int_rgb);
int x, y;
int x1, y1;
JFrame JF;
Grafik G;
String str;
Farbe C;
int cr;
Int Cl [] [] = New int [1000] [1000];
ArrayList <Phape> formes = new ArrayList <Phaper> ();
Random random = new random ();
int r;
int g;
int b;
public PaintDemo (Grafik G, JFrame JF) {
this.jf = jf;
this.g = jf.getGraphics ();
}
@Override
public void mouseclicked (MouseEvent e) {
// Todo automatisch generierte Methode Stub
}
@Override
public void mousePressed (mouseevent e) {
x = e.getX ();
y = e.gety ();
}
@Override
public void mouseerleased (MouseEvent e) {
x1 = e.getX ();
y1 = e.gety ();
setColor ();
if (str.equals ("einen Kreis zeichnen") {
// Paintoval ();
Formform = neuer Ovalshape (x, y, x1, y1, c);
Form.Draw (g);
Formen.Add (Form);
}
if (str.equals ("Zeichnen") {
// paintline ();
Formform = neuer Linesanbuch (x, y, x1, y1, c);
Form.Draw (g);
Formen.Add (Form);
/*Datei f = neue Datei ("d: //test.txt");
if (f == null) {
versuchen {
f.createNewFile ();
} catch (Ausnahme e1) {
// todo automatisch generierter Fangblock
e1.printstacktrace ();
}
}*/
}
}
public void SaveFile (String Path, ArrayList <Phape> Formen) {{
versuchen{
// Dateiausgabe Stream
FileOutputStream fos = new FileOutputStream (Pfad);
// Wickeln Sie den Dateiausgabestrom in einen beschreibbaren Basistypstream ein
DataOutputStream dos = neuer DataOutputStream (FOS);
dos.writeInt (formes.size ()); // die Anzahl der an die Warteschlange geschriebene Zahlen
// Warteschlange lesen
für (int i = 0; i <formes.size (); i ++) {
// eine Form herausnehmen
Formform = Formen.get (i);
byte type = form.type;
if (type == 0) {// beurteilen Sie den Typ nach Typ, wenn es sich um eine gerade Linie handelt
System.out.println ("Start Laging Line");
dos.writebyte (type);
LinesHape -Linie = (LinesHape) Form; // feindlich in die lineare Klasse konvertieren
// Daten in geraden Linien schreiben;
int x = line.x;
int y = line.y;
int x1 = line.x1;
int y1 = line.y1;
Farbe Farbe = Linie.Color;
cr = color.getRGB ();
dos.writeInt (x);
dos.writeInt (y);
dos.writeInt (x1);
dos.writeInt (y1);
dos.writeInt (cr);
} else if (type == 1) {
dos.writebyte (type);
System.out.println ("Start Circle");
Ovalshape oval = (ovalshape) Form; // feindlich in die Kreisklasse konvertieren
// Daten in geraden Linien schreiben;
int x = oval.x;
int y = oval.y;
int x1 = oval.x1;
int y1 = oval.y1;
Farbe Farbe = oval.Color;
cr = color.getRGB ();
dos.writeInt (x);
dos.writeInt (y);
dos.writeInt (x1);
dos.writeInt (y1);
dos.writeInt (cr);
} else if (type == 2) {
dos.writebyte (type);
Bild pl = (Bild) Form;
System.out.println ("Bildern starten");
int width = pl.a;
int Höhe = pl.b;
dos.writeInt (width);
dos.writeInt (Höhe);
für (int k = 0; k <width; k ++) {
für (int j = 0; j <Höhe; j ++) {
int t = pl.cl [k] [j];
dos.writeInt (t);
}
}
}
}
dos.flush ();
fos.close ();
} catch (Ausnahme e) {
E. printstacktrace ();
}
}
public arrayList <Sapary> ReadFile (String -Pfad) {
versuchen{
// Erstellen Sie den Eingabestream des Dateiobjekts
FileInputStream fis = new FileInputStream (Pfad);
// Wickeln Sie den Dateieingangsstrom in einen Strom lesbarer Basistypen ein
DataNputStream dis = new DataNputStream (FIS);
// Lesen Sie zuerst die Dateilänge, dh die Gesamtzahl der Formen
int len = dis.readint ();
für (int i = 0; i <len; i ++) {
byte type = dis.readbyte ();
if (type == 0) {
int a = dis.readint ();
int b = dis.readint ();
int c = dis.readint ();
int d = dis.readint ();
Farbe f = neue Farbe (dis.readint ());
LinesHape -Linie = neuer LineshePe (a, b, c, d, f);
// Lesen Sie die Einstellungen der geraden Linien
// Gerade Linien in die Warteschlange speichern
formes.add (Linie);
} else if (type == 1) {
int a = dis.readint ();
int b = dis.readint ();
int c = dis.readint ();
int d = dis.readint ();
Farbe f = neue Farbe (dis.readint ());
System.out.println ("Start Reading Circle");
Ovalshape oval = neu ovalshape (a, b, c, d, f);
formes.add (oval);
} else if (type == 2) {
int a = dis.readint ();
int b = dis.readint ();
für (int k = 0; k <a; k ++) {
für (int j = 0; j <b; j ++) {
Cl [k] [j] = dis.readint ();
}
}
Bild Bild = neues Bild (a, b, cl);
formes.add (pic);
}
}} catch (Ausnahme e) {
E. printstacktrace ();
}
Returnformen;
}
@Override
public void mausented (Mouseevent e) {
// Todo automatisch generierte Methode Stub
}
@Override
public void mouseexited (mouseevent e) {
// Todo automatisch generierte Methode Stub
}
public void setColor () {
R = random.nextint (255);
G = random.nextint (255);
B = random.nextint (255);
C = neue Farbe (r, g, b);
}
/* public void Paintline () {
setColor ();
g.drawline (x, y, x1, y1);
}
public void Paintoval () {
setColor ();
G.Drawoval ((x+x1)/2, (y+y1)/2, (x1-x)/2, (y1-y)/2);
}
*/
@Override
public void actionperformed (actionEvent e) {
str = e.getActionCommand ();
jf.addmouselistener (dies);
if (str.equals ("speichern")) {
SaveFile ("d: //test.txt", formes);
}
if (str.equals ("open")) {
ReadFile ("d: //test.txt");
für (int i = 0; i <formes.size (); i ++) {
Formform = Formen.get (i);
if (form.type == 0) {
System.out.println ("Bestimmen Sie, dass es sich um einen Kreistyp handelt");
LinesHape Line = (LinesHape) Form;
Linie.Draw (g);
} else if (form.type == 1) {
System.out.println ("Bestimmen Sie, dass es sich um einen Kreistyp handelt");
Ovalshape oval = (ovalshape) Form;
oval.draw (g);
} else if (form.type == 2) {
System.out.println ("Bestätigen Sie, dass es sich um den Bildtyp handelt");
Bild pl = (Bild) Form;
Pl.Draw (g);
}
}
}
if (str.equals ("bild")) {
Thread th = neuer Thread (neuer Runnable () {
public void run () {
while (wahr) {
versuchen{
Thread.sleep (30);
Grafik g1 = bufimage.getGraphics ();
g1.drawimage (img.getimage (), 0,0, null);
Jf.getGraphics (). Drawimage (Bufimage, 100.100, NULL);
int width = bufimage.getWidth ();
int Höhe = bufimage.getheight ();
für (int k = 0; k <width; k ++) {
für (int j = 0; j <Höhe; j ++) {
int p = bufimage.getRGB (k, j);
Cl [k] [j] = p;
}
}
Bild pe = neues Bild (Breite, Höhe, Cl);
forces.add (pe);
} catch (Ausnahme e) {
E. printstacktrace ();
}
}
}});
th.start ();
}
}
}