import java.awt.*; import java.applet.*; public class ImageBarMeterApplet extends BarMeterApplet { // 描画用イメージ protected Image foregroundImage = null; public void init(){ // HTMLから値を取り出す minimum = Double.valueOf(getParameter("minimum")).doubleValue(); maximum = Double.valueOf(getParameter("maximum")).doubleValue(); // 生成する DataCreator の種類を取り出す dataCreatorName = getParameter("data"); try{ // DataCreator の生成 Class dataCreatorClass = Class.forName(dataCreatorName); dataCreator = (DataCreator)dataCreatorClass.newInstance(); }catch(ClassNotFoundException ex){ System.err.println("Can't found such a class!"); System.exit(1); }catch(InstantiationException ex){ System.err.println("Can't instance such a class!"); System.exit(1); }catch(IllegalAccessException ex){ System.err.println("Can't instance such a class!"); System.exit(1); } // イメージの読み込み String filename = getParameter("image"); foregroundImage = getImage(getDocumentBase(), filename); MediaTracker tracker = new MediaTracker(this); tracker.addImage(foregroundImage, 0); try{ // イメージがロードされるまで待つ tracker.waitForID(0); }catch(InterruptedException e){ System.exit(1); } } // バーの描画 protected void drawBar(Graphics g, double value){ // 指示値の相対値を算出 double val = (value - minimum)/ (maximum - minimum); // グラフ上の高さに変換 val *= height; int h = (int)val; // イメージを描画 g.drawImage(foregroundImage, 0, 0, this); // 背景色でバーを塗りつぶす g.setColor(this.getBackground()); g.fillRect(0, 0, width, h); } public void paint(Graphics g){ // ダブルバッファリング用のイメージがなければ、 // 生成し、グラフィックコンテキストをえる。 if(image == null){ // アプレットのサイズをえる Dimension size = this.getSize(); width = size.width; height = size.height; // イメージの生成 image = this.createImage(width, height); graphics = image.getGraphics(); } // バーの描画 drawBar(graphics, value); // イメージバッファの描画 g.drawImage(image, 0, 0, width, height, this); } }