import java.awt.*; import java.applet.*; public class ImageBarMeterApplet2 extends BarMeterApplet { // 描画用イメージ protected Image foregroundImage = null; // 背景用イメージ protected Image backgroundImage = 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); // 背景イメージの読み込み filename = getParameter("background"); backgroundImage = getImage(getDocumentBase(), filename); MediaTracker tracker = new MediaTracker(this); tracker.addImage(foregroundImage, 0); tracker.addImage(backgroundImage, 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.clearRect(0, 0, width, height); // イメージを描画 g.drawImage(foregroundImage, 0, 0, this); if(h != 0){ // 背景イメージの描画 Image bimage = this.createImage(width, h); Graphics bg = bimage.getGraphics(); bg.drawImage(backgroundImage, 0, 0, this); g.drawImage(bimage, 0, 0, this); } } 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); } }