(1) Primero defina la clase a implementar, primero defino una clase abstracta
La copia del código es la siguiente:
//Gráficos
forma de clase abstracta {
int x, y;
int x1, y1;
Color color;
Gráficos G;
tipo de byte;
Public Abstract Void Draw (Graphics G);
}
// Línea recta
La clase Lineshape extiende la forma {
Public Lineshape (int x, int y, int x1, int y1, color color) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.type = 0;
}
@Anular
public void Draw (Graphics g) {
G.SetColor (color);
G.Drawline (x, y, x1, y1);
}
}
// Clase de círculo
La clase OvalShape extiende la forma {
public ovalshape (int x, int y, int x1, int y1, color color) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.type = 1;
}
@Anular
public void Draw (Graphics g) {
G.SetColor (color);
G.Drawoval ((x+x1)/2, (y+y1)/2, (x1-x)/2, (y1-y)/2);
}
}
//Fotos
La imagen de clase extiende la forma {
int a;
int b;
int cl [] [];
imagen pública (int a, int b, int cl [] []) {
this.type = 2;
esto.a = a;
this.b = b;
this.cl = cl;
}
public void Draw (Graphics g) {
para (int k = 0; k <a; k ++) {
para (int j = 0; j <b; j ++) {
Color c = nuevo color (cl [k] [j]);
G.SetColor (C);
G.Drawline (K+100, J+100, K+100, J+100);
}
}
}
}
(2) clase de método total
La copia del código es la siguiente:
clase pública BMPSAVESTART {
Gráficos G;
public static void main (string [] args) {
nuevo BMPSAVESTART () .ui ();
}
public void ui () {
Jframe jf = new Jframe ();
JF.SetSize (600,600);
jf.settitle ("Almacenamiento de la imagen");
jf.setbackground (color.white);
Jmenubar bar = new JMenubar ();
JMenu menú1 = new JMenu ("Opción");
JMenuitem M1 = new JMenuitem ("Draw a Circle");
JMenuitem m2 = new JMenuitem ("Línea de dibujo");
JMenuitem M3 = new JMenuitem ("Storage");
JMenuitem M4 = new JMenuitem ("Open");
JMenuitem M5 = new JMenuitem ("Imagen");
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 (verdadero);
PaintDemo P = New PaintDemo (G, JF);
m1.SetActionCommand ("dibujar un círculo");
m2.SetActionCommand ("Línea de dibujo");
m3.SetActionCommand ("almacenamiento");
m4.SetActionCommand ("abierto");
m5.SetActionCommand ("Imagen");
m1.addactionListener (p);
M2.AddactionListener (P);
M3.AddactionListener (P);
M4.AddactionListener (P);
M5.AddactionListener (P);
}
}
(3) Clase de monitoreo
La copia del código es la siguiente:
Clase PaintDemo implementa Mouselistener, ActionListener {
Imageicon img = new Imageicon ("Images/100.gif");
BufferedImage bufimage = new BufferedImage (img.geticonwidth (), img.geticonheight (), bufferedImage.type_int_rgb);
int x, y;
int x1, y1;
Jframe JF;
Gráficos G;
Cuerda str;
Color c;
int cr;
int cl [] [] = nuevo int [1000] [1000];
ArrayList <sape> formas = new ArrayList <sape> ();
Aleatorio aleatorio = new Random ();
int r;
int g;
int b;
Public PaintDemo (Graphics G, Jframe JF) {
this.jf = jf;
this.g = jf.getgraphics ();
}
@Anular
public void mouseclicked (mouseevent e) {
// TODO STUB de método generado automático
}
@Anular
public void mousepressed (mouseevent e) {
x = e.getx ();
y = E.gety ();
}
@Anular
Public Void Mousereleaded (Mouseevent E) {
x1 = e.getx ();
y1 = e.gety ();
setColor ();
if (str.equals ("dibujar un círculo") {
// pintura ();
Forma forma = nueva ovalshape (x, y, x1, y1, c);
forma.draw (g);
formas.add (forma);
}
if (str.equals ("dibujar línea") {
// Paintline ();
Forma forma = nueva lineshape (x, y, x1, y1, c);
forma.draw (g);
formas.add (forma);
/*Archivo f = nuevo archivo ("d: //test.txt");
if (f == null) {
intentar {
F.CreateNewFile ();
} capt (excepción e1) {
// bloque de captura generado automático
e1.printstackTrace ();
}
}*/
}
}
public void SaveFile (string ruta, arrayList <sape> formas) {
intentar{
// flujo de salida de archivo
FileOutputStream fos = new FileOutputStream (ruta);
// Envuelve la transmisión de salida del archivo en una secuencia de tipo básico de escritura
DataOutputStream dos = new DataOutputStream (FOS);
dos.WriteInt (formas.size ()); // El número de cifras escritas en la cola
// leer cola
para (int i = 0; i <formas.size (); i ++) {
// saca una forma
Forma forma = formas.get (i);
byte type = shape.type;
if (type == 0) {// juzga el tipo de acuerdo con el tipo, si es una línea recta
System.out.println ("Comenzar la línea de almacenamiento");
dos.writyte (tipo);
Lineshape Line = (Lineshape) Forma; // Convertir cautivamente a clase lineal
// Escribir datos en líneas rectas;
int x = line.x;
int y = line.y;
int x1 = line.x1;
int y1 = line.y1;
Color color = line.color;
cr = color.getRgb ();
dos.Writeint (x);
dos.writeint (y);
dos.Writeint (x1);
dos.Writeint (Y1);
dos.Writeint (CR);
} else if (type == 1) {
dos.writyte (tipo);
System.out.println ("Comenzar al círculo de almacenamiento");
Ovalshape oval = (ovalshape) forma; // convertir cautivamente a clase de círculo
// Escribir datos en líneas rectas;
int x = oval.x;
int y = oval.y;
int x1 = oval.x1;
int y1 = oval.y1;
Color color = 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.writyte (tipo);
imagen pl = (imagen) forma;
System.out.println ("Comience a almacenar imágenes");
int ancho = pl.a;
int altura = pl.b;
dos.Writeint (ancho);
dos.writeint (altura);
para (int k = 0; k <width; k ++) {
para (int j = 0; j <altura; j ++) {
int t = pl.cl [k] [j];
dos.writeint (t);
}
}
}
}
dos.flush ();
fos.close ();
} Catch (Exception e) {
E.PrintStackTrace ();
}
}
Public ArrayList <Sape> ReadFile (ruta de cadena) {
intentar{
// Crear la secuencia de entrada del objeto de archivo
FileInputStream fis = new FileInputStream (ruta);
// Envuelve la transmisión de entrada de archivo en una secuencia de tipos básicos legibles
DataInputStream dis = new DataInputStream (FIS);
// Lea primero la longitud del archivo, es decir, el número total de formas
int len = dis.readInt ();
para (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 ();
Color f = nuevo color (dis.readint ());
Línea de enlace = nueva lineshape (a, b, c, d, f);
// Lea la configuración de las líneas rectas
// Guardar líneas rectas en la cola
formas.add (línea);
} else if (type == 1) {
int a = dis.readInt ();
int b = dis.readInt ();
int c = dis.readInt ();
int d = dis.readInt ();
Color f = nuevo color (dis.readint ());
System.out.println ("Comenzar el círculo de lectura");
Ovalshape Oval = nueva Ovalshape (A, B, C, D, F);
formas.add (ovalado);
} else if (type == 2) {
int a = dis.readInt ();
int b = dis.readInt ();
para (int k = 0; k <a; k ++) {
para (int j = 0; j <b; j ++) {
cl [k] [j] = dis.readInt ();
}
}
imagen foto = nueva imagen (a, b, cl);
formas.add (foto);
}
}} Catch (Exception e) {
E.PrintStackTrace ();
}
formas de retorno;
}
@Anular
public void mouseentered (mouseevent e) {
// TODO STUB de método generado automático
}
@Anular
public void mouseExited (mouseevent e) {
// TODO STUB de método generado automático
}
public void setColor () {
R = Random.NextInt (255);
G = Random.NextInt (255);
B = Random.NextInt (255);
c = nuevo color (r, g, b);
}
/* public void Paintline () {
setColor ();
G.Drawline (x, y, x1, y1);
}
public void pintarval () {
setColor ();
G.Drawoval ((x+x1)/2, (y+y1)/2, (x1-x)/2, (y1-y)/2);
}
*/
@Anular
public void Actionperformed (ActionEvent E) {
str = e.getActionCommand ();
jf.addmouselistener (esto);
if (str.equals ("almacenamiento")) {
saveFile ("d: //test.txt", formas);
}
if (str.equals ("abierto")) {
readfile ("d: //test.txt");
para (int i = 0; i <formas.size (); i ++) {
Forma forma = formas.get (i);
if (shape.type == 0) {
System.out.println ("Determine que es un tipo de círculo");
Línea de lineshape = (lineshape);
line.draw (g);
} else if (shape.type == 1) {
System.out.println ("Determine que es un tipo de círculo");
Ovalshape ovalada = (ovalshape) forma;
oval.draw (g);
} else if (shape.type == 2) {
System.out.println ("Confirme que es el tipo de imagen");
imagen pl = (imagen) forma;
pl.draw (g);
}
}
}
if (str.equals ("imagen")) {
Thread th = new Thread (new Runnable () {
public void run () {
while (verdadero) {
intentar{
Thread.sleep (30);
Gráficos g1 = bufimage.getGraphics ();
G1.DrawImage (img.getImage (), 0,0, nulo);
jf.getGraphics (). DrawImage (Bufimage, 100,100, NULL);
int width = bufimage.getWidth ();
int hight = bufimage.getheight ();
para (int k = 0; k <width; k ++) {
para (int j = 0; j <altura; j ++) {
int p = bufimage.getRgb (k, j);
cl [k] [j] = p;
}
}
imagen PE = nueva imagen (ancho, altura, cl);
formas.add (PE);
} Catch (Exception e) {
E.PrintStackTrace ();
}
}
}});
th.start ();
}
}
}