import java.awt.*; import java.applet.*; public class BarMeterApplet2 extends Applet { // Double Buffering 用イメージ transient protected Image image = null; transient protected Graphics graphics = null; // 画面の大きさに関連したプロパティ protected int width; protected int height; // メータの最小/最大値 protected double minimum; protected double maximum; // メータが指す値 protected double value = 0; public void init(){ // HTMLから値を取り出す minimum = Double.valueOf(getParameter("minimum")).doubleValue(); maximum = Double.valueOf(getParameter("maximum")).doubleValue(); value = Double.valueOf(getParameter("value")).doubleValue(); } // バーの描画 protected void drawBar(Graphics g, double value){ // 指示値の相対値を算出 double val = (value - minimum)/ (maximum - minimum); // グラフ上の高さに変換 val *= height; int y = height - (int)val; int h = (int)val; // バーの色を設定 g.setColor(Color.yellow); // バーを描画 g.fillRect(0, y, width, h); } public void update(Graphics g){ // 画面のクリアは行わない paint(g); } 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(); } // 背景色で塗りつぶす graphics.setColor(this.getBackground()); graphics.fillRect(0, 0, width, height); // バーの描画 drawBar(graphics, value); // イメージバッファの描画 g.drawImage(image, 0, 0, width, height, this); } }