(1) Primeiro defina a classe a ser implementada, primeiro defino uma classe abstrata
A cópia do código é a seguinte:
// gráficos
classe abstrata forma {
int x, y;
int x1, y1;
Cor cor;
Gráficos G;
tipo de byte;
Public Resumo Void Draw (Graphics G);
}
// linha reta
classe LineShape estende a forma {
Public LineShape (int x, int y, int x1, int y1, cor de cor) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = cor;
this.Type = 0;
}
@Override
public void Draw (Graphics G) {
g.setColor (cor);
G.Drawline (X, Y, X1, Y1);
}
}
// Classe Class
classe OvalShape estende a forma {
public ovalShape (int x, int y, int x1, int y1, cor de cor) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = cor;
this.Type = 1;
}
@Override
public void Draw (Graphics G) {
g.setColor (cor);
G.Drawoval ((x+x1)/2, (y+y1)/2, (x1-x)/2, (y1-y)/2);
}
}
//Fotos
classe imagem estende a forma {
int a;
int b;
int cl [] [];
foto pública (int a, int b, int cl [] []) {
this.Type = 2;
this.a = a;
this.b = b;
this.cl = cl;
}
public void Draw (Graphics G) {
for (int k = 0; k <a; k ++) {
for (int j = 0; j <b; j ++) {
Cor c = nova cor (cl [k] [j]);
g.setColor (c);
G.Drawline (K+100, J+100, K+100, J+100);
}
}
}
}
(2) Classe de método total
A cópia do código é a seguinte:
classe pública bmpsavestart {
Gráficos G;
public static void main (string [] args) {
novo bmpsavestart () .ui ();
}
public void ui () {
Jframe jf = new jframe ();
JF.SetSize (600.600);
jf.settitle ("armazenamento da imagem");
jf.setbackground (color.white);
Jmenubar Bar = New Jmenubar ();
Jmenu menu1 = novo jmenu ("option");
Jmenuitem M1 = New Jmenuitem ("Draw A Circle");
Jmenuitem m2 = new jmenuitem ("linha de desenho");
Jmenuitem m3 = novo jmenuitem ("armazenamento");
Jmenuitem m4 = novo jmenuitem ("aberto");
Jmenuitem m5 = novo jmenuitem ("imagem");
menu1.add (m1);
menu1.add (m2);
menu1.add (m3);
menu1.add (m4);
menu1.add (m5);
bar.add (menu1);
jf.setjmenubar (bar);
jf.setDefaultCloseoperation (3);
jf.setVisible (true);
PaintDemo P = novo PaintDemo (G, JF);
m1.setActionCommand ("Draw A Circle");
m2.SetActionCommand ("Draw Line");
M3.SetActionCommand ("Storage");
m4.SetActionCommand ("Open");
M5.SetActionCommand ("Picture");
M1.AddactionListener (P);
M2.AddactionListener (P);
M3.AddactionListener (P);
M4.AddactionListener (P);
M5.AddactionListener (P);
}
}
(3) Classe de monitoramento
A cópia do código é a seguinte:
classe Paintdemo implementa MouseListener, ActionListener {
Imageicon img = new imageicon ("imagens/100.gif");
BufferImage bufimage = new bufferImage (img.geticonwidth (), img.geticonheight (), bufferiMage.type_int_rgb);
int x, y;
int x1, y1;
Jframe jf;
Gráficos G;
String str;
Cor c;
int cr;
int cl [] [] = new int [1000] [1000];
ArrayList <Shaple> Shapes = new ArrayList <Shaple> ();
Aleatório aleatório = novo aleatório ();
int r;
int g;
int b;
public Paintdemo (Gráfico G, Jframe JF) {
this.jf = jf;
this.g = jf.getgraphics ();
}
@Override
public void mouseclicked (mouseevent e) {
// TODO Method Stub
}
@Override
public void MousePressed (mouseevent e) {
x = e.getx ();
y = e.gety ();
}
@Override
public void mousereleden (mouseevent e) {
x1 = e.getx ();
y1 = e.gety ();
setColor ();
if (str.equals ("desenhe um círculo") {
// pintoval ();
Forma da forma = novo OvalShape (x, y, x1, y1, c);
shap.draw (g);
shapes.add (forma);
}
if (str.equals ("draw line") {
// TIMPLINE ();
Forma da forma = nova forma de linhas (x, y, x1, y1, c);
shap.draw (g);
shapes.add (forma);
/*Arquivo f = novo arquivo ("d: //test.txt");
if (f == null) {
tentar {
F.CreateNewFile ();
} catch (Exceção E1) {
// TODO BLOCO DE CAPAGEM AUTOMAGEM
e1.printStackTrace ();
}
}*/
}
}
Public void SaveFile (String Path, ArrayList <Shape> Shapes) {
tentar{
// Stream de saída de arquivo
FileOutputStream fos = new FileOutputStream (caminho);
// Enrole o fluxo de saída de arquivo em um fluxo de tipo básico gravável
DataOutputStream DOS = new DataOutputStream (FOS);
dos.writeint (shapes.size ()); // o número de figuras escritas na fila
// Leia a fila
for (int i = 0; i <shapes.size (); i ++) {
// Tire uma forma
Forma da forma = shapes.get (i);
byte type = shape.type;
if (type == 0) {// julgue o tipo de acordo com o tipo, se for uma linha reta
System.out.println ("Start Storing Line");
Dos.WriteByte (tipo);
Linha de linha de linhas = forma (lineshape); // converter cativeiro em classe linear
// escreve dados em linhas retas;
int x = line.x;
int y = line.y;
int x1 = line.x1;
int y1 = line.y1;
Cor cor = 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.WriteByte (tipo);
System.out.println ("Start Storing Circle");
OvalShape oval = (Ovalshape)
// escreve dados em linhas retas;
int x = oval.x;
int y = oval.y;
int x1 = oval.x1;
int y1 = oval.y1;
Cor cor = 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 (tipo);
imagem pl = (imagem) forma;
System.out.println ("Comece a armazenar fotos");
int width = pl.a;
int altura = pl.b;
Dos.WriteInt (largura);
Dos.WriteInt (altura);
for (int k = 0; k <largura; k ++) {
for (int j = 0; j <altura; j ++) {
int t = pl.cl [k] [j];
Dos.WriteInt (T);
}
}
}
}
Dos.flush ();
fos.close ();
} catch (Exceção e) {
E.PrintStackTrace ();
}
}
public ArrayList <Shaple> ReadFile (String Path) {
tentar{
// Crie o fluxo de entrada do objeto de arquivo
FileInputStream fis = new FileInputStream (caminho);
// Enrole o fluxo de entrada do arquivo em um fluxo de tipos básicos legíveis
DatainputStream DIS = new DatainputStream (FIS);
// Leia primeiro o comprimento do arquivo, ou seja, o 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 ();
Cor f = nova cor (Dis.ReadInT ());
Linha de linha de linha = nova forma de linhas (A, B, C, D, F);
// Leia as configurações de linhas retas
// Salve linhas retas na fila
shapes.add (linha);
} else if (type == 1) {
int a = dis.readInt ();
int b = dis.readInt ();
int c = Dis.readInt ();
int d = Dis.readInt ();
Cor f = nova cor (Dis.ReadInT ());
System.out.println ("Start Reading Circle");
OvalShape oval = novo OvalShape (A, B, C, D, F);
shapes.add (oval);
} else if (type == 2) {
int a = dis.readInt ();
int b = dis.readInt ();
for (int k = 0; k <a; k ++) {
for (int j = 0; j <b; j ++) {
cl [k] [j] = Dis.ReadInt ();
}
}
foto foto = nova imagem (a, b, cl);
shapes.add (pic);
}
}} catch (Exceção e) {
E.PrintStackTrace ();
}
retornar formas;
}
@Override
public void MouseEntered (Mouseevent e) {
// TODO Method Stub
}
@Override
public void mouseexited (mouseevent e) {
// TODO Method Stub
}
public void setColor () {
R = Random.NextInt (255);
G = Random.NextInt (255);
B = Random.NextInt (255);
c = nova cor (r, g, b);
}
/* Public void TIPINE () {
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 ActionPormed (ActionEvent E) {
str = e.getActionCommand ();
jf.addmouselistener (isto);
if (str.equals ("armazenamento")) {
savefile ("d: //test.txt", formas);
}
if (str.equals ("aberto")) {
readfile ("d: //test.txt");
for (int i = 0; i <shapes.size (); i ++) {
Forma da forma = shapes.get (i);
if (shape.type == 0) {
System.out.println ("Determine que é um tipo de círculo");
Linha de linha de linha = forma (lineshape);
line.Draw (G);
} else if (shape.type == 1) {
System.out.println ("Determine que é um tipo de círculo");
OvalShape oval = (OvalShape) da forma;
oval.draw (g);
} else if (shape.type == 2) {
System.out.println ("Confirme que é o tipo de imagem");
imagem pl = (imagem) forma;
pl.draw (g);
}
}
}
if (str.equals ("picture")) {
Thread th = novo thread (novo runnable () {
public void run () {
while (true) {
tentar{
Thread.sleep (30);
Gráficos g1 = bufimage.getGraphics ();
g1.drawimage (img.getImage (), 0,0, nulo);
jf.getgraphics (). drawimage (bufimage, 100.100, nulo);
int width = bufimage.getWidth ();
int height = bufimage.getHeight ();
for (int k = 0; k <largura; k ++) {
for (int j = 0; j <altura; j ++) {
int p = bufimage.getRgb (k, j);
cl [k] [j] = p;
}
}
imagem pe = nova imagem (largura, altura, cl);
shapes.add (PE);
} catch (Exceção e) {
E.PrintStackTrace ();
}
}
}});
th.start ();
}
}
}