import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.font.*; public class ClipBarMeter extends BarMeter { protected Shape clipShape; protected int clipShapeNumber = OVAL; public static final int OVAL = 0; public static final int ROUND_RECTANGLE = 1; public static final int TEXT = 2; public static final int COMP_SHAPE = 3; public static final int GENERAL_PATH = 4; public Dimension getMinimumSize(){ return new Dimension(200, 200); } public void setClipShape(int shapeNo){ this.clipShapeNumber = shapeNo; clipShape = createClipShape(shapeNo); } protected Shape createClipShape(int shapeNo){ Shape shape; switch(shapeNo){ case OVAL: shape = new Ellipse2D.Double(0.0, 0.0, (double)width, (double)height); break; case ROUND_RECTANGLE: shape = new RoundRectangle2D.Double(0.0, 0.0, (double)width, (double)height, (double)width/5.0, (double)height/5.0); break; case TEXT: TextLayout text = new TextLayout("あj", new Font("Serif", Font.BOLD, height), new FontRenderContext(null, false, false)); AffineTransform trans = new AffineTransform(); trans.translate(0, text.getAscent()-text.getDescent()); shape = text.getOutline(trans); break; case COMP_SHAPE: Area area1 = new Area(new RoundRectangle2D.Double( 0.0, (double)height*2.0/3.0, (double)width, (double)height/3.0, (double)width/10.0, (double)height/10.0)); Area area2 = new Area(new RoundRectangle2D.Double( (double)width/6.0, 0.0, (double)width*2.0/3.0, (double)height/3.0, (double)width/20.0, (double)height/20.0)); Area area3 = new Area(new Rectangle2D.Double( (double)width/4.0, (double)height/3.0, (double)width/2.0, (double)height/3.0)); Area area4 = new Area(new Rectangle2D.Double( (double)width/3.0, (double)height/3.0, (double)width/3.0, (double)height/3.0)); area1.add(area2); area1.add(area3); area1.subtract(area4); shape = area1; break; case GENERAL_PATH: GeneralPath path = new GeneralPath(); path.moveTo(0.0f, (float)height); path.curveTo(0.0f, (float)height*0.2f, (float)width*0.9f, (float)height*0.7f, (float)width*0.9f, 0.0f); path.lineTo((float)width, 0.0f); path.curveTo((float)width, (float)height*0.8f, (float)width*0.1f, (float)height*0.3f, (float)width*0.1f, (float)height); path.closePath(); shape = path; break; default: shape = new Rectangle2D.Double(0.0, 0.0, (double)width, (double)height); break; } return shape; } // バーの描画 protected void drawBar(Graphics g, double value){ Graphics2D g2D = (Graphics2D)g; // 指示値の相対値を算出 double val = (value - minimum)/ (maximum - minimum); // グラフ上の高さに変換 val *= height; int y = height - (int)val; int h = (int)val; g2D.clip(clipShape); g2D.setColor(this.getForeground()); g2D.fillRect(0, y, width, h); g2D.setColor(this.getBackground()); g2D.fillRect(0, 0, width, y); } public void createBufferImage(){ // アプレットのサイズをえる Dimension size = this.getSize(); width = size.width; height = size.height; if(width != 0 && height != 0){ // イメージの生成 image = this.createImage(width, height); graphics = image.getGraphics(); clipShape = createClipShape(clipShapeNumber); } } public void paint(Graphics g){ // ダブルバッファリング用のイメージがなければ、 // 生成し、グラフィックコンテキストをえる。 if(image == null || resizeFlag){ createBufferImage(); resizeFlag = false; } graphics.setClip(null); // 背景色で塗りつぶす graphics.setColor(getParent().getBackground()); graphics.fillRect(0, 0, width, height); // バーの描画 drawBar(graphics, value); // イメージバッファの描画 g.drawImage(image, 0, 0, width, height, this); } }