[an error occurred while processing this directive] [an error occurred while processing this directive]

(1) 画像をロードして、表示する

一枚の画像をWebサーバからロードして、アプレット上に表示します。 アプレットなので、画像はWebサーバにアップロードしたものを取ってこないといけません。 画像の URL はソースコードに決め打ちです。 Ivylet の上に IvyPanel が載り、その上に Icon が一つ載っています。 システムは IvyPanel の paintComponent() に描画の指令を送るので、この中で Icon を描画するように、Icon クラスの draw メソッドを呼び出しています。

Ivylet: メインのアプレットのクラス

まずはアプレットのメインのクラスです。 色々な部品は IvyPanel というクラスにのせてしまったので、IvyPanel を作って乗せる以外特に何もしていません。
public class Ivylet extends JApplet {
    public void init() {
        IvyPanel tp = new IvyPanel();
        setContentPane(tp);
    }
}

IvyPanel: メインのパネル

Icon クラスの部品が一つだけ載っている、メインのパネルです。icon には画像を一つロードします。 画像の URL はhttp://funini.com/kei/ivy/narita/00.jpgです。 あと、描画をする paintComponent() メソッドがあります。これは自動的に適宜呼び出されます。
class IvyPanel extends JPanel {
    Icon icon;
    
    public IvyPanel() {
        /** コンストラクタ 引数は画像が入っているパス */
        setBackground(new Color(0x33, 0x33, 0x33)); // 背景色設定
        setPreferredSize(new Dimension(300, 300)); // サイズ設定
        icon = new Icon("http://funini.com/kei/ivy/narita/00.jpg");
        icon.setPosition(new int[]{0, 0, 400, 300});
    }


    /** 描画を行う */
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // ウィンドウを表示
        Graphics2D g2 = (Graphics2D) g; // アンチエイリアスの設定など
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        icon.draw(g2); // 各アイコンを表示
    }
}

Rect: パネルに置く色んな部品の基本クラス

このページのソースでは、部品はアイコン一つしかありませんが、今後もっと色々な部品(ボタンとかバーとか)が出来たときに備えて、 これらの部品に共通な処理を Rect というクラスにまとめておきます。書いてある処理は、初期化・画像のロード・外部からの位置設定です。
画像ロードについては、下に出てくる ImageLoader クラスを用いています。
abstract class Rect {
    int[]   P;     // X,Y座標, 幅、高さ
    Image   img;
    int     imgW, imgH; // 画像の幅、高さ

    public Rect(int x, int y, int w, int h) {
        P = new int[] { x, y, w, h };
    }

    /** 画像をロード。path がhttp:// で始まる場合、HTTPで取得する */
    public void loadImage(String path){ 
        img = ImageLoader.load(path); 
        imgW = img.getWidth(null);
        imgH = img.getHeight(null);
    }
    
    public void setPosition(int[] P){
        for(int i = 0; i < 4; i++)
            this.P[i] = P[i];
    }
}

Icon: アイコンクラス

アイコン(写真)一枚を保持するクラスです。Rect を継承しているので、位置(x,y)、幅・高さ(w, h)についての変数は定義しなくても大丈夫です。 Rect のメソッドを呼び出すときには、super を用います。
class Icon extends Rect {
    int[] dest, orig; // 移動先と移動元のx, y, w, hを持つ配列
    public Icon(String imgPath) {
        super(0, 0, 0, 0);
        loadImage(imgPath);
    }

    /** アイコンを描画する */
    void draw(Graphics2D g) {
        g.drawImage(img, P[0], P[1], P[0] + P[2],
                P[1] + P[3], 0, 0, imgW, imgH, null);
    }
}

ImageLoader: 画像を読み込むためのクラス

画像を取得するルーチンは、ImageLoader クラスにしてみました。 load というメソッドで、指定された文字列の画像を取ってきて、Image として返すことができます。
static メソッドにしたので、インスタンスを作らなくても実行できます。つまり、
ImageLoader il = new ImageLoader();
Image img = il.load("c:/hoge.jpg");
のように書かなくても、
Image img = ImageLoader.load("c:/hoge.jpg");
のように簡単に書くことが出来ます。
class ImageLoader {
    /** 画像をロード。path がhttp:// で始まる場合、HTTPで取得する */
    public static Image load(String path) {
        try {
            if(path.startsWith("http://")){
                URLConnection urlc = new URL(path).openConnection();
                return ImageIO.read(urlc.getInputStream());
            }
            return ImageIO.read(new File(path));
        } catch (IOException ie) { ie.printStackTrace();}
        return null;
    }
}
[an error occurred while processing this directive]