(1) 먼저 구현 될 클래스를 정의하고, 먼저 추상 클래스를 정의합니다.
코드 사본은 다음과 같습니다.
//제도법
추상 클래스 모양 {
int x, y;
int x1, y1;
색상;
그래픽 g;
바이트 유형;
공개 초록 void draw (그래픽 g);
}
//일직선
클래스 라인 샤프는 모양 {
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 = 색상;
this.type = 0;
}
@보수
공개 무효 드로우 (그래픽 g) {
G. 세트 컬러 (색);
G.DrawLine (X, Y, X1, Y1);
}
}
// 서클 클래스
클래스 타원형 형태는 모양 {
public 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) {
G. 세트 컬러 (색);
g.sudoval ((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) {
for (int k = 0; k <a; k ++) {
for (int j = 0; j <b; j ++) {
색상 C = 새로운 색상 (cl [k] [j]);
G. 세트 콜러 (c);
G.DrawLine (K+100, J+100, K+100, J+100);
}
}
}
}
(2) 총 방법 클래스
코드 사본은 다음과 같습니다.
공개 클래스 BMPSAVESTART {
그래픽 g;
public static void main (String [] args) {
new bmpsavestart () .ui ();
}
공개 void ui () {
jframe jf = new jframe ();
jf.setsize (600,600);
jf.settitle ( "그림 저장");
jf. 세트 백 그라운드 (Color.white);
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 ( "그림");
메뉴 1.add (m1);
메뉴 1.add (m2);
메뉴 1.add (m3);
메뉴 1.add (m4);
메뉴 1.add (m5);
bar.add (메뉴 1);
jf.setjmenubar (bar);
JF.SetDefaultCloseOperation (3);
jf.set -Visible (true);
Paintdemo P = 새로운 페인트 모 (G, JF);
m1.setactionCommand ( "원을 그리는");
M2.SetActionCommand ( "Draw Line");
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 = new bufferedImage (img.geticonwidth (), img.geticonheight (), bufferedimage.type_int_rgb);
int x, y;
int x1, y1;
JFrame JF;
그래픽 g;
문자열 str;
컬러 C;
int cr;
int cl [] [] = 새로운 int [1000] [1000];
ArrayList <Shape> Shapes = New ArrayList <모양> ();
랜덤 random = new random ();
int r;
int g;
int b;
Public Paintdo (Graphics G, Jframe JF) {
this.jf = jf;
this.g = jf.getGraphics ();
}
@보수
공공 void mouseclicked (mouseevent e) {
// TODO 자동 생성 메소드 스텁
}
@보수
공공 보이드 무성 (mouseevent e) {
x = e.getx ();
y = e.gety ();
}
@보수
공공 void mouserEleled (mouseevent e) {
x1 = e.getx ();
y1 = e.gety ();
setColor ();
if (str.equals ( "원 그리기") {
// paintoval ();
모양 형태 = 새로운 Ovalshape (x, y, x1, y1, c);
Shape.Draw (G);
shapes.add (모양);
}
if (str.equals ( "draw line") {
// Paintline ();
모양 형태 = 새로운 라인 샤프 (x, y, x1, y1, c);
Shape.Draw (G);
shapes.add (모양);
/*파일 f = 새 파일 ( "d : //test.txt");
if (f == null) {
노력하다 {
f.createnewfile ();
} catch (예외 e1) {
// TODO 자동 생성 캐치 블록
e1.printstacktrace ();
}
}*/
}
}
public void savefile (String path, arraylist <shape> shapes) {
노력하다{
// 파일 출력 스트림
fileoutputStream fos = 새 FileOutputStream (path);
// 파일 출력 스트림을 쓰기 가능한 기본 유형 스트림으로 래핑합니다.
DataOutputStream dos = 새로운 DataOutputStream (FOS);
dos.writeint (shapes.size ()); // 대기열에 쓴 그림 수입니다
// 큐를 읽습니다
for (int i = 0; i <shapes.size (); i ++) {
// 모양을 꺼냅니다
모양 모양 = shapes.get (i);
바이트 타입 = 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);
} else if (type == 1) {
dos.writeByte (유형);
System.out.println ( "원 스토링 시작");
Ovalshape Oval = (Ovalshape) 모양; // 원형 클래스로 포괄적으로 변환
// 직선에 데이터를 작성합니다.
int x = Oval.x;
int 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);
} else if (type == 2) {
dos.writeByte (유형);
그림 pl = (그림) 모양;
System.out.println ( "사진 저장 시작");
int width = pl.a;
int height = pl.b;
dos.writeint (너비);
dos.writeint (높이);
for (int k = 0; k <width; k ++) {
for (int j = 0; j <height; j ++) {
int t = pl.cl [k] [J];
dos.writeint (t);
}
}
}
}
dos.flush ();
fos.close ();
} catch (예외 e) {
e.printstacktrace ();
}
}
public arraylist <모양> readfile (문자열 경로) {
노력하다{
// 파일 객체의 입력 스트림을 만듭니다
fileInputStream fis = 새로운 fileInputStream (Path);
// 파일 입력 스트림을 읽을 수있는 기본 유형의 스트림으로 래핑합니다.
datainputStream dis = new DatainputStream (FIS);
// 먼저 파일 길이, 즉 총 모양의 수를 읽습니다.
int len = dis.readint ();
for (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 = 새로운 Lineshape (a, b, c, d, f);
// 직선의 설정을 읽습니다
// 직선을 대기열에 저장합니다
shapes.add (line);
} else if (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 = New Ovalshape (A, B, C, D, F);
shapes.add (타원형);
} 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 ();
}
}
그림 그림 = 새 그림 (A, B, CL);
shapes.add (pic);
}
}} catch (예외 e) {
e.printstacktrace ();
}
반환 모양;
}
@보수
공개 무효 마우스 센터 (Mouseevent e) {
// TODO 자동 생성 메소드 스텁
}
@보수
공공 void mouseexited (mouseevent e) {
// TODO 자동 생성 메소드 스텁
}
public void 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.sudoval ((x+x1)/2, (y+y1)/2, (x1-x)/2, (y1-y)/2);
}
*/
@보수
public void actionperformed (ActionEvent e) {
str = e.getActionCommand ();
jf.addmouseListener (this);
if (str.equals ( "Storage")) {
savefile ( "d : //test.txt", Shapes);
}
if (str.equals ( "Open")) {
readfile ( "d : //test.txt");
for (int i = 0; i <shapes.size (); i ++) {
모양 모양 = shapes.get (i);
if (shape.type == 0) {
System.out.println ( "원형 유형인지 결정");
Lineshape line = (Lineshape) 모양;
line.draw (g);
} else if (shape.type == 1) {
System.out.println ( "원형 유형인지 결정");
Ovalshape Oval = (Ovalshape) 모양;
Oval.Draw (g);
} else if (shape.type == 2) {
System.out.println ( "이미지 유형인지 확인");
그림 pl = (그림) 모양;
pl.Draw (g);
}
}
}
if (str.equals ( "picture")) {
스레드 th = 새 스레드 (new Runnable () {
public void run () {
while (true) {
노력하다{
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 height = bufimage.getheight ();
for (int k = 0; k <width; k ++) {
for (int j = 0; j <height; j ++) {
int p = bufimage.getrgb (k, j);
cl [k] [j] = p;
}
}
그림 pe = 새로운 그림 (너비, 높이, Cl);
shapes.add (pe);
} catch (예외 e) {
e.printstacktrace ();
}
}
}});
th.start ();
}
}
}