(1) ก่อนกำหนดคลาสที่จะนำไปใช้ฉันก่อนกำหนดคลาสนามธรรมก่อน
การคัดลอกรหัสมีดังนี้:
// กราฟิก
รูปร่างคลาสนามธรรม {
int x, y;
int x1, y1;
สีสี;
กราฟิก G;
ประเภทไบต์;
การวาดโมฆะนามธรรมสาธารณะ (กราฟิก G);
-
// เส้นตรง
คลาส lineshape ขยายรูปร่าง {
lineshape สาธารณะ (int x, int y, int x1, int y1, สีสี) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.type = 0;
-
@Override
การวาดโมฆะสาธารณะ (กราฟิก g) {
G.SetColor (สี);
G.Drawline (X, Y, X1, Y1);
-
-
// ระดับวงกลม
คลาส Ovalshape ขยายรูปร่าง {
สาธารณะรูปไข่สาธารณะ (int x, int y, int x1, int y1, สีสี) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.type = 1;
-
@Override
การวาดโมฆะสาธารณะ (กราฟิก g) {
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;
-
การวาดโมฆะสาธารณะ (กราฟิก g) {
สำหรับ (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 {
กราฟิก G;
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
ใหม่ bmpsavestart () .ui ();
-
โมฆะสาธารณะ ui () {
jframe jf = new JFrame ();
JF.Setsize (600,600);
jf.settitle ("การจัดเก็บรูปภาพ");
jf.setbackground (color.white);
JMENUBAR BAR = ใหม่ JMENUBAR ();
JMENU เมนู 1 = new JMENU ("ตัวเลือก");
jmenuitem m1 = new JMenuitem ("วาดวงกลม");
JMenuitem M2 = JMENUITEM ใหม่ ("Draw Line");
JMENUITEM M3 = JMENUITEM ใหม่ ("Storage");
JMENUITEM M4 = JMENUITEM ใหม่ ("เปิด");
JMenuitem M5 = new JMenuitem ("Picture");
Menu1.add (M1);
Menu1.add (M2);
Menu1.add (M3);
Menu1.add (M4);
Menu1.add (M5);
bar.add (เมนู 1);
jf.setjmenubar (บาร์);
JF.SetDefaultCloseoperation (3);
jf.setVisible (จริง);
PaintDemo P = ใหม่ PaintDemo (G, JF);
M1.SetActionCommand ("วาดวงกลม");
M2.SetActionCommand ("Draw Line");
M3.SetActionCommand ("Storage");
M4.SetActionCommand ("เปิด");
m5.setActionCommand ("รูปภาพ");
M1.AddactionListener (P);
M2.AddactionListener (P);
M3.AddactionListener (P);
M4.AddactionListener (P);
M5.AddactionListener (P);
-
-
(3) คลาสการตรวจสอบ
การคัดลอกรหัสมีดังนี้:
Class PaintDemo ใช้ Mouselistener, ActionListener {
Imageicon img = ใหม่ imageicon ("images/100.gif");
bufferedImage bufimage = bufferedImage ใหม่ (img.geticonwidth (), img.geticonheight (), bufferedimage.type_int_rgb);
int x, y;
int x1, y1;
jframe jf;
กราฟิก G;
String str;
สี C;
int cr;
int cl [] [] = new int [1000] [1000];
arrayList <mhape> shapes = new ArrayList <Shape> ();
สุ่มสุ่ม = ใหม่สุ่ม ();
int r;
int g;
int b;
Public PaintDemo (กราฟิก G, JFrame JF) {
this.jf = jf;
this.g = jf.getGraphics ();
-
@Override
โมฆะสาธารณะ mouseclicked (MouseEvent E) {
// todo วิธีการที่สร้างขึ้นอัตโนมัติสตับ
-
@Override
โมฆะสาธารณะ mousepressed (MouseEvent E) {
x = e.getx ();
y = e.gety ();
-
@Override
โมฆะสาธารณะ mouseerleased (MouseEvent E) {
x1 = e.getx ();
y1 = e.gety ();
SetColor ();
if (str.equals ("วาดวงกลม") {
// paintoval ();
รูปร่างรูปร่าง = รูปไข่ใหม่ (x, y, x1, y1, c);
shape.draw (g);
รูปร่าง Add (รูปร่าง);
-
if (str.equals ("วาดเส้น") {
// paintline ();
รูปร่างรูปร่าง = lineshape ใหม่ (x, y, x1, y1, c);
shape.draw (g);
รูปร่าง Add (รูปร่าง);
/*ไฟล์ f = ไฟล์ใหม่ ("d: //test.txt");
ถ้า (f == null) {
พยายาม {
f.createnewfile ();
} catch (Exception e1) {
// todo catch block ที่สร้างอัตโนมัติ
e1.printstacktrace ();
-
-
-
-
โมฆะสาธารณะ saveFile (เส้นทางสตริง, arraylist <รูปร่าง> รูปร่าง) {
พยายาม{
// สตรีมเอาต์พุตไฟล์
fileOutputStream fos = ใหม่ fileOutputStream (พา ธ );
// ห่อสตรีมเอาต์พุตไฟล์ลงในสตรีมประเภทพื้นฐานที่เขียนได้
DataOrtputStream DOS = ใหม่ dataOutputStream (FOS);
dos.writeint (shapes.size ()); // จำนวนตัวเลขที่เขียนลงในคิว
// อ่านคิว
สำหรับ (int i = 0; i <shapes.size (); i ++) {
// เอารูปร่างออกไป
รูปร่างรูปร่าง = shapes.get (i);
byte type = shape.type;
if (type == 0) {// ตัดสินประเภทตามประเภทถ้ามันเป็นเส้นตรง
System.out.println ("เริ่มการจัดเก็บบรรทัด");
dos.writeByte (ประเภท);
lineshape line = (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 (ประเภท);
System.out.println ("เริ่มจัดเก็บวงกลม");
รูปไข่รูปไข่ = (รูปไข่) รูปร่าง; // แปลงเป็นคลาสวงกลม
// เขียนข้อมูลเป็นเส้นตรง
int x = roval.x;
int y = รูปไข่;
int x1 = oval.x1;
int y1 = รูปไข่ y1;
สีสี = รูปไข่สี;
cr = color.getRgb ();
dos.writeint (x);
dos.writeint (y);
dos.writeint (x1);
Dos.WriteInt (Y1);
dos.writeint (cr);
} อื่นถ้า (type == 2) {
dos.writeByte (ประเภท);
รูปภาพ pl = (รูปภาพ) รูปร่าง;
System.out.println ("เริ่มจัดเก็บรูปภาพ");
ความกว้าง int = pl.a;
ความสูง int = pl.b;
dos.writeint (ความกว้าง);
dos.writeint (ความสูง);
สำหรับ (int k = 0; k <width; k ++) {
สำหรับ (int j = 0; j <ความสูง; j ++) {
int t = pl.cl [k] [j];
dos.writeint (t);
-
-
-
-
dos.flush ();
fos.close ();
} catch (Exception e) {
E.PrintStackTrace ();
-
-
Public ArrayList <Shape> ReadFile (String Path) {
พยายาม{
// สร้างสตรีมอินพุตของวัตถุไฟล์
FileInputStream FIS = ใหม่ FileInputStream (พา ธ );
// ห่อสตรีมอินพุตไฟล์ลงในสตรีมของประเภทพื้นฐานที่อ่านได้
datainputStream dis = new datainputStream (FIS);
// ก่อนอ่านความยาวไฟล์นั่นคือจำนวนรูปร่างทั้งหมด
int len = dis.readint ();
สำหรับ (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 ();
สี f = สีใหม่ (dis.readint ());
lineshape line = lineshape ใหม่ (a, b, c, d, f);
// อ่านการตั้งค่าของเส้นตรง
// บันทึกเส้นตรงลงในคิว
รูปร่าง Add (บรรทัด);
} อื่นถ้า (type == 1) {
int a = dis.readint ();
int b = dis.readint ();
int c = dis.readint ();
int d = dis.readint ();
สี f = สีใหม่ (dis.readint ());
System.out.println ("เริ่มอ่านวงกลม");
รูปไข่รูปไข่ = รูปไข่ใหม่ (a, b, c, d, f);
รูปร่าง Add (รูปไข่);
} อื่นถ้า (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 (รูป);
-
}} catch (Exception e) {
E.PrintStackTrace ();
-
รูปร่างกลับ;
-
@Override
โมฆะสาธารณะ Mouseentered (MouseEvent E) {
// todo วิธีการที่สร้างขึ้นอัตโนมัติสตับ
-
@Override
โมฆะสาธารณะ mouseexited (MouseEvent E) {
// todo วิธีการที่สร้างขึ้นอัตโนมัติสตับ
-
โมฆะสาธารณะ setColor () {
r = random.nextint (255);
g = random.nextint (255);
b = random.nextint (255);
C = สีใหม่ (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
โมฆะสาธารณะ ActionPerformed (ActionEvent E) {
str = e.getActionCommand ();
jf.addmouselistener (นี่);
if (str.equals ("Storage")) {
SaveFile ("D: //test.txt", รูปร่าง);
-
if (str.equals ("เปิด")) {
ReadFile ("D: //test.txt");
สำหรับ (int i = 0; i <shapes.size (); i ++) {
รูปร่างรูปร่าง = shapes.get (i);
if (shape.type == 0) {
System.out.println ("พิจารณาว่าเป็นประเภทวงกลม");
lineshape line = (lineshape) รูปร่าง;
line.draw (g);
} อื่นถ้า (shape.type == 1) {
System.out.println ("พิจารณาว่าเป็นประเภทวงกลม");
รูปไข่รูปไข่ = (รูปไข่) รูปร่าง;
OVAL.Draw (G);
} อื่นถ้า (shape.type == 2) {
System.out.println ("ยืนยันว่าเป็นประเภทภาพ");
รูปภาพ pl = (รูปภาพ) รูปร่าง;
PL.DAW (G);
-
-
-
if (str.equals ("picture")) {
เธรด 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 <ความสูง; j ++) {
int p = bufimage.getRgb (k, j);
CL [K] [J] = P;
-
-
รูปภาพ PE = ภาพใหม่ (ความกว้าง, ความสูง, CL);
รูปร่าง Add (PE);
} catch (Exception e) {
E.PrintStackTrace ();
-
-
-
Th.Start ();
-
-
-