(1)最初に実装するクラスを定義し、最初に抽象クラスを定義します
コードコピーは次のとおりです。
//グラフィックス
抽象クラスの形状{
int x、y;
int x1、y1;
色の色;
グラフィックG;
バイトタイプ;
パブリックアブストラクトボイドドロー(グラフィックG);
}
//直線
クラスのLineshapeはシェイプを拡張します{
public lineshape(int x、int y、int x1、int y1、color){
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.type = 0;
}
@オーバーライド
public void draw(グラフィックG){
G.SetColor(色);
G.drawline(x、y、x1、y1);
}
}
//サークルクラス
クラスovalshapeはshapeを拡張します{
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;
}
@オーバーライド
public void draw(グラフィック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;
}
public void draw(グラフィックG){
for(int k = 0; k <a; k ++){
for(int j = 0; j <b; j ++){
Color C = new Color(cl [k] [j]);
G.SetColor(C);
G.Drawline(K+100、J+100、K+100、J+100);
}
}
}
}
(2)総メソッドクラス
コードコピーは次のとおりです。
パブリッククラスbmpsavestart {
グラフィックG;
public static void main(string [] args){
new bmpsavestart().ui();
}
public void ui(){
jframe jf = new JFrame();
jf.setsize(600,600);
jf.settitle( "Picture of Picture");
jf.setbackground(color.white);
jmenubar bar = new jmenubar();
jmenu menu1 = new jmenu( "option");
jmenuitem M1 = new jmenuitem( "draw a circle");
jmenuitem M2 = new JMenuitem( "Draw Line");
jmenuitem m3 = new Jmenuitem( "Storage");
jmenuitem M4 = new JMenuitem( "Open");
jmenuitem m5 = new Jmenuitem( "picture");
メニュー1.Add(M1);
メニュー1.Add(M2);
メニュー1.Add(M3);
メニュー1.Add(M4);
メニュー1.Add(M5);
bar.add(menu1);
jf.setjmenubar(bar);
jf.setDefaultCloseoperation(3);
jf.setVisible(true);
paintdemo p = new 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.AdcriftionListener(P);
M4.AddACTIONLISTENER(P);
M5.AdcriftionListener(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 [] [] = new int [1000] [1000];
arraylist <shape> shapes = new ArrayList <shape>();
RANDOM RANDOM = new Random();
int r;
int g;
int b;
Public Paintdemo(グラフィックG、JFrame JF){
this.jf = jf;
this.g = jf.getgraphics();
}
@オーバーライド
public void mouseclicked(mousevent e){
// TODO自動生成方法スタブ
}
@オーバーライド
public void mousepressed(mousevent e){
x = e.getx();
y = e.gety();
}
@オーバーライド
public void mouseReleased(MouseEvent e){
x1 = e.getx();
y1 = e.gety();
setColor();
if(str.equals( "draw a circle"){
// paintoval();
shape shape = new ovalshape(x、y、x1、y1、c);
shape.draw(g);
shapes.add(shape);
}
if(str.equals( "draw line"){
// paintline();
shape shape = new Lineshape(x、y、x1、y1、c);
shape.draw(g);
shapes.add(shape);
/*ファイルf = new file( "d://test.txt");
if(f == null){
試す {
f.createNewfile();
} catch(例外E1){
// TODO自動生成キャッチブロック
e1.printstacktrace();
}
}*/
}
}
public void savefile(string path、arraylist <shape> shapes){
試す{
//ファイル出力ストリーム
fileoutputStream fos = new fileoutputStream(path);
//ファイル出力ストリームを書き込み可能な基本タイプのストリームにラップします
dataoutputStream dos = new DataOutputStream(FOS);
dos.writeint(shapes.size()); //キューに書かれた数値の数
//キューを読みます
for(int i = 0; i <shapes.size(); i ++){
//形を取ります
shape shape = shapes.get(i);
バイトタイプ= shape.type;
if(type == 0){//タイプに従ってタイプを判断します。
system.out.println( "ラインを保存する");
dos.writebyte(タイプ);
Lineshape Line =(Lineshape)Shape; // CaptivivitiveTivell
//直線にデータを書きます。
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( "Circleの保存を開始");
ovalshape oval =(ovalshape)shape; // captially convertively convertive
//直線にデータを書きます。
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);
} else if(type == 2){
dos.writebyte(タイプ);
画像pl =(picture)shape;
System.out.println( "Picturesの保存を開始");
int width = pl.a;
int height = pl.b;
dos.writeint(width);
dos.writeint(height);
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 <shape> readfile(string path){
試す{
//ファイルオブジェクトの入力ストリームを作成します
fileInputStream fis = new 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();
color f = new Color(dis.readint());
Lineshape Line = new Lineshape(A、B、C、D、F);
//直線の設定を読み取ります
// queueに直線を保存します
shapes.add(line);
} else if(type == 1){
int a = dis.readint();
int b = dis.readint();
int c = dis.readint();
int d = dis.readint();
color f = new Color(dis.readint());
System.out.println( "Circleの読み取りを開始");
ovalshape oval = new 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();
}
}
写真pic = new Picture(a、b、cl);
shapes.add(pic);
}
}} catch(例外e){
e.printstacktrace();
}
形状を返す;
}
@オーバーライド
public void mouseentered(mousevent e){
// TODO自動生成方法スタブ
}
@オーバーライド
public void mouseexited(mousevent e){
// TODO自動生成方法スタブ
}
public void setColor(){
r = random.nextint(255);
g = random.nextint(255);
b = random.nextint(255);
c = new Color(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);
}
*/
@オーバーライド
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 ++){
shape shape = shapes.get(i);
if(shape.type == 0){
system.out.println( "それが円タイプであると判断する");
Lineshape Line =(Lineshape)Shape;
line.draw(g);
} else if(shape.type == 1){
system.out.println( "それが円タイプであると判断する");
ovalshape oval =(ovalshape)shape;
oval.draw(g);
} else if(shape.type == 2){
system.out.println( "それが画像タイプであることを確認");
画像pl =(picture)shape;
pl.draw(g);
}
}
}
if(str.equals( "picture")){
スレッドth = newスレッド(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;
}
}
Picture Pe = new Picture(width、height、cl);
shapes.add(pe);
} catch(例外e){
e.printstacktrace();
}
}
}});
th.start();
}
}
}