(1) تحديد الفصل أولاً المراد تنفيذه ، أحدد أولاً فئة مجردة
نسخة الكود كما يلي:
// الرسومات
شكل فئة مجردة {
int x ، y ؛
int x1 ، y1 ؛
لون اللون
الرسومات ز ؛
نوع البايت ؛
رسم باطل تجريدي عام (رسومات ز) ؛
}
// خط مستقيم
يمتد Lineshape Class الشكل {
lineshape العامة (int x ، int y ، int x1 ، int y1 ، لون اللون) {
this.x = x ؛
this.y = y ؛
this.x1 = x1 ؛
this.y1 = y1 ؛
this.color = اللون ؛
this.type = 0 ؛
}
@تجاوز
رسم باطل عام (رسومات ز) {
G.SetColor (اللون) ؛
G.Drawline (x ، y ، x1 ، y1) ؛
}
}
// دائرة فئة
يمتد فئة Ovalshape الشكل {
Ovalshape العامة (int x ، int y ، int x1 ، int y1 ، لون اللون) {
this.x = x ؛
this.y = y ؛
this.x1 = x1 ؛
this.y1 = y1 ؛
this.color = اللون ؛
this.type = 1 ؛
}
@تجاوز
رسم باطل عام (رسومات ز) {
G.SetColor (اللون) ؛
g.drawoval ((x+x1)/2 ، (y+y1)/2 ، (x1-x)/2 ، (y1-y)/2) ؛
}
}
// الصور
صورة الفئة تمتد الشكل {
int a ؛
int b ؛
int cl [] [] ؛
الصورة العامة (int a ، int b ، int cl [] []) {
this.type = 2 ؛
this.a = a ؛
this.b = b ؛
this.cl = cl ؛
}
رسم باطل عام (رسومات ز) {
لـ (int k = 0 ؛ k <a ؛ k ++) {
لـ (int j = 0 ؛ j <b ؛ j ++) {
اللون c = لون جديد (cl [k] [j]) ؛
G.SetColor (C) ؛
G.Drawline (K+100 ، J+100 ، K+100 ، J+100) ؛
}
}
}
}
(2) فئة الطريقة الكلية
نسخة الكود كما يلي:
الطبقة العامة BMPSAVESTART {
الرسومات ز ؛
الفراغ الثابت العام الرئيسي (سلسلة [] args) {
New Bmpsavestart () .ui () ؛
}
public void ui () {
Jframe JF = New JFrame () ؛
Jf.Setsize (600،600) ؛
jf.settitle ("تخزين الصورة") ؛
jf.setbackground (color.hite) ؛
Jmenubar Bar = New Jmenubar () ؛
jmenu menu1 = new jmenu ("الخيار") ؛
JMenuitem M1 = New JMenuitem ("ارسم دائرة") ؛
JMenuitem M2 = New JMenuitem ("Draw Line") ؛
JMenuitem M3 = New JMenuitem ("التخزين") ؛
JMenuitem M4 = New JMenuitem ("Open") ؛
JMenuitem M5 = New JMenuitem ("Picture") ؛
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 (صواب) ؛
PaintDemo p = new PaintDemo (g ، jf) ؛
m1.setActionCommand ("ارسم دائرة") ؛
M2.SetActionCommand ("خط السحب") ؛
M3.SetActionCommand ("التخزين") ؛
M4.SetActionCommand ("Open") ؛
m5.SetActionCommand ("الصورة") ؛
M1.addActionListener (P) ؛
M2.AddActionListener (P) ؛
M3.addActionListener (P) ؛
M4.AddActionListener (P) ؛
M5.AddActionListener (P) ؛
}
}
(3) فئة المراقبة
نسخة الكود كما يلي:
Class PaintDemo ينفذ Mouselistener ، ActionListener {
ImageIcon IMG = New ImageIcon ("Images/100.gif") ؛
BufferedImage bufimage = جديد bufferedImage (img.geticonwidth () ، img.geticonheight () ، bufferedImage.type_int_rgb) ؛
int x ، y ؛
int x1 ، y1 ؛
Jframe JF ؛
الرسومات ز ؛
سلسلة شارع
اللون ج.
int cr ؛
int cl [] [] = new int [1000] [1000] ؛
ArrayList <Phary> الأشكال = جديد ArrayList <Phary> () ؛
عشوائي عشوائي = جديد عشوائي () ؛
int r ؛
int g ؛
int b ؛
paintdemo العامة (الرسومات G ، Jframe JF) {
this.jf = jf ؛
this.g = jf.getGraphics () ؛
}
@تجاوز
الفراغ العام mouseclicked (mouseevent e) {
// TODO METHOTION METTOD COBS
}
@تجاوز
الفراغ العام mousepressed (mouseevent e) {
x = e.getx () ؛
y = e.gety () ؛
}
@تجاوز
الفراغ العام mouserelest (mouseevent e) {
x1 = e.getx () ؛
y1 = e.gety () ؛
setColor () ؛
if (str.equals ("ارسم دائرة") {
// paintoval () ؛
شكل شكل = Ovalshape جديد (x ، y ، x1 ، y1 ، c) ؛
شكل. draw (g) ؛
الأشكال.
}
if (str.equals ("draw line") {
// paintline () ؛
شكل الشكل = lineshape جديد (x ، y ، x1 ، y1 ، c) ؛
شكل. draw (g) ؛
الأشكال.
/*file f = ملف جديد ("d: //test.txt") ؛
if (f == null) {
يحاول {
f.createNewFile () ؛
} catch (استثناء e1) {
// TODO AUTO CATCH BLOCK
e1.printstacktrace () ؛
}
}*/
}
}
void public saveFile (مسار السلسلة ، ArrayList <Phary> أشكال) {
يحاول{
// دفق إخراج الملف
fileOutputStream fos = new FileOutputStream (path) ؛
// لف دفق إخراج الملف في دفق نوع أساسي قابل للكتابة
DataOutputStream DOS = جديد DataOutputStream (FOS) ؛
dos.writeint (Shapes.size ()) ؛ // عدد الأرقام المكتوبة في قائمة الانتظار
// قراءة قائمة الانتظار
لـ (int i = 0 ؛ i <task.size () ؛ i ++) {
// أخرج الشكل
شكل الشكل = الأشكال. get (i) ؛
نوع البايت = الشكل.
if (type == 0) {// upple type type type ، إذا كان خطًا مستقيمًا
System.out.println ("بدء تخزين الخط") ؛
dos.writeByte (type) ؛
خط lineshape = (lineshape) الشكل ؛ // تحويله إلى فئة خطية
// كتابة البيانات على خطوط مستقيمة ؛
int x = line.x ؛
int y = line.y ؛
int x1 = line.x1 ؛
int y1 = line.y1 ؛
لون اللون = line.color ؛
cr = color.getrgb () ؛
dos.writeint (x) ؛
dos.writeint (y) ؛
dos.writeint (x1) ؛
dos.writeint (y1) ؛
dos.writeint (cr) ؛
} آخر إذا (type == 1) {
dos.writeByte (type) ؛
System.out.println ("بدء تخزين الدائرة") ؛
Ovalshape Oval = (Ovalshape) الشكل ؛ // تحويل الأسر إلى فئة الدائرة
// كتابة البيانات على خطوط مستقيمة ؛
int x = oval.x ؛
int y = oval.y ؛
int x1 = oval.x1 ؛
int y1 = oval.y1 ؛
لون اللون = oval.color ؛
cr = color.getrgb () ؛
dos.writeint (x) ؛
dos.writeint (y) ؛
dos.writeint (x1) ؛
dos.writeint (y1) ؛
dos.writeint (cr) ؛
} آخر إذا (type == 2) {
dos.writeByte (type) ؛
صورة PL = (صورة) الشكل ؛
System.out.println ("ابدأ تخزين الصور") ؛
عرض int = pl.a ؛
ارتفاع int = pl.b ؛
dos.writeint (العرض) ؛
dos.writeint (الارتفاع) ؛
لـ (int k = 0 ؛ k <width ؛ k ++) {
لـ (int j = 0 ؛ j <height ؛ j ++) {
int t = pl.cl [k] [j] ؛
dos.writeint (t) ؛
}
}
}
}
dos.flush () ؛
fos.close () ؛
} catch (استثناء e) {
E.PrintStackTrace () ؛
}
}
ArrayList Public ArrayList <Mape> readfile (مسار السلسلة) {
يحاول{
// إنشاء دفق إدخال كائن الملف
FileInputStream FIS = جديد fileInputStream (path) ؛
// لف دفق إدخال الملف في دفق من الأنواع الأساسية القابلة للقراءة
DatainputStream dis = جديد DatainputStream (FIS) ؛
// اقرأ طول الملف أولاً ، أي العدد الإجمالي للأشكال
int len = dis.readint () ؛
لـ (int i = 0 ؛ i <len ؛ i ++) {
نوع البايت = dis.readbyte () ؛
if (type == 0) {
int a = dis.readint () ؛
int b = dis.readint () ؛
int c = dis.readint () ؛
int d = dis.readint () ؛
اللون f = لون جديد (dis.readint ()) ؛
Lineshape Line = new lineshape (a ، b ، c ، d ، f) ؛
// اقرأ إعدادات الخطوط المستقيمة
// احفظ الخطوط المستقيمة في قائمة الانتظار
الأشكال.
} آخر إذا (type == 1) {
int a = dis.readint () ؛
int b = dis.readint () ؛
int c = dis.readint () ؛
int d = dis.readint () ؛
اللون f = لون جديد (dis.readint ()) ؛
System.out.println ("ابدأ القراءة دائرة") ؛
Ovalshape Oval = Ovalshape جديد (A ، B ، C ، D ، F) ؛
الأشكال.
} آخر إذا (type == 2) {
int a = dis.readint () ؛
int b = dis.readint () ؛
لـ (int k = 0 ؛ k <a ؛ k ++) {
لـ (int j = 0 ؛ j <b ؛ j ++) {
cl [k] [j] = dis.readint () ؛
}
}
الصورة الموافقة المسبقة عن علم = صورة جديدة (A ، B ، CL) ؛
الأشكال. add (pic) ؛
}
}} catch (استثناء e) {
E.PrintStackTrace () ؛
}
أشكال العودة
}
@تجاوز
public void mouseentered (mouseevent e) {
// TODO METHOTION METTOD COBS
}
@تجاوز
public void mouseexited (mouseevent e) {
// TODO METHOTION METTOD COBS
}
setColor void public () {
r = random.nextint (255) ؛
g = random.nextint (255) ؛
ب = عشوائي. nextint (255) ؛
C = لون جديد (R ، G ، B) ؛
}
/* Public Void Paintline () {
setColor () ؛
G.Drawline (x ، y ، x1 ، y1) ؛
}
paintoval paintoval () {
setColor () ؛
g.drawoval ((x+x1)/2 ، (y+y1)/2 ، (x1-x)/2 ، (y1-y)/2) ؛
}
*/
@تجاوز
الفراغ العام actionperformed (Actionevent e) {
str = e.getActionCommand () ؛
jf.addmouselistener (هذا) ؛
if (str.equals ("التخزين")) {
SaveFile ("d: //test.txt" ، الأشكال) ؛
}
if (str.equals ("Open")) {
ReadFile ("d: //test.txt") ؛
لـ (int i = 0 ؛ i <task.size () ؛ i ++) {
شكل الشكل = الأشكال. get (i) ؛
if (lape.type == 0) {
System.out.println ("حدد أنه نوع دائرة") ؛
خط lineshape = (lineshape) الشكل ؛
line.draw (g) ؛
} آخر إذا (lape.type == 1) {
System.out.println ("حدد أنه نوع دائرة") ؛
Ovalshape Oval = (Ovalshape) الشكل ؛
oval.draw (g) ؛
} آخر إذا (lape.type == 2) {
System.out.println ("تأكد من أنه نوع الصورة") ؛
صورة PL = (صورة) الشكل ؛
pl.draw (g) ؛
}
}
}
if (str.equals ("صورة")) {
الموضوع Th = موضوع جديد (جديد Runnable () {
تشغيل الفراغ العام () {
بينما (صحيح) {
يحاول{
thread.sleep (30) ؛
الرسومات g1 = bufimage.getGraphics () ؛
g1.drawImage (img.getimage () ، 0،0 ، null) ؛
jf.getGraphics (). DrawImage (bufimage ، 100،100 ، null) ؛
int width = bufimage.getWidth () ؛
ارتفاع int = bufimage.getheight () ؛
لـ (int k = 0 ؛ k <width ؛ k ++) {
لـ (int j = 0 ؛ j <height ؛ j ++) {
int p = bufimage.getrgb (k ، j) ؛
Cl [k] [j] = p ؛
}
}
الصورة pe = صورة جديدة (العرض ، الارتفاع ، cl) ؛
الأشكال. add (pe) ؛
} catch (استثناء e) {
E.PrintStackTrace () ؛
}
}
}}) ؛
th.start () ؛
}
}
}