import java.awt.*; import java.awt.event.*; import java.awt.image.*; public class TranslucentBarMeter extends Component { // Double Buffering 用イメージ transient protected BufferedImage image = null; transient protected Graphics2D graphics = null; // 透明度 protected int alpha = 128; // 背景/前景の色に透明度を施したもの protected Color backColor; protected Color foreColor; // 画面の大きさに関連したプロパティ protected int width = 0; protected int height = 0; // リサイズのチェック用フラグ protected boolean resizeFlag = false; // メータの最小/最大値 protected double minimum = 0; protected double maximum = 100; // メータが指す値 protected double value = 0; public TranslucentBarMeter(){ super(); enableEvents(AWTEvent.COMPONENT_EVENT_MASK); } public void processComponentEvent(ComponentEvent e){ if(e.getID() == ComponentEvent.COMPONENT_RESIZED){ resizeFlag = true; repaint(); } super.processComponentEvent(e); } public Dimension getPreferredSize(){ return getMinimumSize(); } public Dimension getMinimumSize(){ return new Dimension(50, 100); } public void setMinimum(double minimum){ this.minimum = minimum; } public double getMinimum(){ return this.minimum; } public void setMaximum(double maximum){ this.maximum = maximum; } public double getMaximum(){ return this.maximum; } public void setValue(double value){ this.value = value; repaint(); } public void setAlpha(int alpha){ this.alpha = alpha; } public void setBackground(Color color){ // 透明度を加味する this.backColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); } public void setForeground(Color color){ // 透明度を加味する this.foreColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); } // バーの描画 protected void drawBar(Graphics2D g, double value){ // 指示値の相対値を算出 double val = (value - minimum)/ (maximum - minimum); // グラフ上の高さに変換 val *= height; int y = height - (int)val; int h = (int)val; // 透明色で塗りつぶす g.setBackground(new Color(255, 255, 255, 0)); g.clearRect(0, 0, width, height); // バーを描画 g.setPaint(backColor); g.fillRect(0, 0, width, y); g.setPaint(foreColor); g.fillRect(0, y, width, h); } public void update(Graphics g){ paint(g); } public void createBufferImage(){ // アプレットのサイズをえる Dimension size = this.getSize(); width = size.width; height = size.height; if(width != 0 && height != 0){ // アルファ付のイメージを生成 image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); graphics = (Graphics2D)image.getGraphics(); } } public void paint(Graphics g){ // ダブルバッファリング用のイメージがなければ、 // 生成し、グラフィックコンテキストをえる。 // また、リサイズされたときもバッファイメージを生成する if(image == null || resizeFlag){ createBufferImage(); resizeFlag = false; } // バーの描画 drawBar(graphics, value); // イメージバッファの描画 g.drawImage(image, 0, 0, width, height, this); } }