今からはじめるプログラミング39

はたしてナンバリングはあっているか?

 

前回の続き。

 

「花をついか」

 

前回、目を追加したので、
今回は花を追加する。
というわけで、ほぼ同じことをするので、
ちょっと共通化、abstractというクラスを使う。

粒度のコントロールというのがいまいちピンとこないので、前回と今回のクラス図?をつくってみた。(関係が記載されてないのでクラス図になってないが、どんなクラスがあるかぐらいはわかるだろう。)

 

f:id:yo2an:20210930195737p:plain

クラス図


名前もなんかそぐわなかったので、変更した。

例1。抽象かクラス(ModImage)

------------------------------------------------

package sample21;

import java.awt.image.BufferedImage;

public abstract class  ModImage {
    
    protected abstract boolean loadImage() ;
    
    protected abstract boolean modImage() ;
    protected abstract boolean outImage() ;
    protected BufferedImage img = null;
    public boolean execute() {
        if(this.loadImage()) {
            if(this.modImage()) {
                if(this.outImage()) {
                    return true;
                }else {
                    return false;
                }
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

}


------------------------------------------------
そして、抽象かクラスを継承する形にした目を追加するクラス
例2。目を追加するクラス(AddEye)
------------------------------------------------
package sample21;

import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AddEye extends ModImage  {

    protected boolean loadImage() {
        System.out.println("loadImage()を実行");
        //画像を読み込む。
        try {
            this.img = ImageIO.read(new File("./img.png"));
        } catch (IOException e) {
            System.out.println("画像を読み込めませんでした。");
            return false;
        }
    
        return true;
    }
    
    protected boolean modImage() {
        System.out.println("modImage()を実行");
        //目をつければキャラクターっぽくなる?
        Graphics2D g = this.img.createGraphics();
        g.setColor(Color.WHITE);
        g.drawString("●", 50, 100);
        g.drawString("●", 100, 100);
        return true;
    }
    protected boolean outImage() {
        System.out.println("outImage()を実行");
        // 出力
        try {
            ImageIO.write(this.img, "png", new File("./out.png"));
        } catch (IOException e) {
            System.out.println("画像を出力できませんでした。");
            return false;
        }
        return true;
    }

    
    public static void main(String arguments) {
        AddEye createI = new AddEye();
        
        if(createI.execute()) {
            System.out.println("全て正常に実行できた");
        }else {
            System.out.println("なんかしっぱいした");
        }
    }
}
------------------------------------------------
さらに花を追加するクラスも同じ感じ。
例3。花を追加するクラス(AddNouse)
------------------------------------------------
package sample21;

import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AddNouse extends ModImage {


    @Override
    protected boolean loadImage() {
        //画像を読み込む。
        try {
            this.img = ImageIO.read(new File("./out.png"));
        } catch (IOException e) {
            System.out.println("画像を読み込めませんでした。");
            return false;
        }
    
        return true;
    }

    @Override
    protected boolean modImage() {
        System.out.println("modImage()を実行");
        //目をつければキャラクターっぽくなる?
        Graphics2D g = this.img.createGraphics();
        g.setColor(Color.WHITE);
        g.drawString("花", 75, 150);
        return true;
    }

    @Override
    protected boolean outImage() {
        System.out.println("outImage()を実行");
        // 出力
        try {
            ImageIO.write(this.img, "png", new File("./add_nouse.png"));
        } catch (IOException e) {
            System.out.println("画像を出力できませんでした。");
            return false;
        }
        return true;
    }
    

    public static void main(String arguments) {
        AddNouse modImage = new AddNouse();
        
        if(modImage.execute()) {
            System.out.println("全て正常に実行できた");
        }else {
            System.out.println("なんかしっぱいした");
        }
    }
}
------------------------------------------------
実行結果の画像たち。

もと

f:id:yo2an:20210930200255p:plain

もともと


目追加

f:id:yo2an:20210930200316p:plain

めがとんでます




花追加

f:id:yo2an:20210930200334p:plain

ばかですいません

 

 

ポイントとしては、呼び出し元としていたexecute()メソッドを、抽象かクラスに移動した感じですね。
あと画像の変数(BufferedImage)
protectedというのはアクセス可能な範囲が親から子供という感じですかね。

 

まだまだ続きます。