作ってみたので備忘録&使ってください。
Chart.java
import java.util.*; import java.applet.*; import java.awt.Graphics; import java.awt.Color; import java.lang.NumberFormatException; public class Chart extends Applet { private List colorList; private List valueList; public Chart() //{{{ { } //}}} public static void main(String [] args) //{{{ { System.out.println("This is applet."); } //}}} public void init() //{{{ { colorList = Collections.synchronizedList(new ArrayList()); valueList = Collections.synchronizedList(new ArrayList()); setBackground(new Color(240,240,240)); setForeground(new Color(0,0,0)); if(getParameter("values").length() != 0) { String[] values = getParameter("values").split(","); for( int i = 0; i < values.length; i++ ) { valueList.add(new Double(values[i])); } } if(getParameter("colors").length() != 0) { String[] colors = getParameter("colors").split(","); for( int i = 0; i < colors.length; i++ ) { colorList.add(stringToColor(colors[i])); } } } //}}} public void start() //{{{ { repaint(); } //}}} public void stop() //{{{ { } //}}} public void paint(Graphics g) //{{{ { int height = Integer.valueOf(getParameter("height")).intValue(); int r = (int)(height * 0.8); int x = (int)(height * 0.8 / 4); // center int y = (int)(height * 0.8 / 4); // center double total = 0; for(int i = 0; i < valueList.size(); i++ ) { Double d = (Double)valueList.get(i); total += d.doubleValue(); } int prev = 0; for( int i = 0; i < valueList.size(); i++ ) { g.setColor((Color)colorList.get(i)); Double d = (Double)valueList.get(i); double value = d.doubleValue(); int arc = (int)(value / total * 360); g.fillArc(x,y,r,r,+90+prev,arc); prev += arc; } } //}}} public void destroy() //{{{ { } //}}} public Color stringToColor(String color) //{{{ { if( color.startsWith("#") ) color = color.substring(1); if( color.length() > 6 ) throw new NumberFormatException("parameter error:"+color); return new Color(Integer.parseInt(color, 16)); } //}}} }
public class Chart extends Applet で Applet から派生して、下記のメソッドをオーバーライドする。
- public void init()
- 初期化処理
- データと各色を保存するためのListを作成し、paramタグで指定されたデータを読み込む
- 初期化処理
- public void start()
- アプレットがの実行が開始するときに呼び出される
- public void stop()
- アプレットがの実行が停止するときに呼び出される
- public void paint(Graphics g)
- 描画する必要があるときに呼び出され
- init()で取得したデータから、円グラフを描画する
- 描画する必要があるときに呼び出され
- public void destory()
- アプレットが割り当てられたすべてのリソースを開放する必要があるときに呼び出される
- public Color stringToColor(String color)
- #RRGGBB形式の文字列をColorに変換する
Chart.java をコンパイルして、Chart.classを作成して、htmlからテストする。
呼び出すためのhtmlは下のような感じ。
Chart.html
<html> <head></head> <body> Chart Test<br> <applet name="chart" code="Chart.class" width="600" height="400"> <param name="colors" value="FF0000,0000FF,00FF00"> <param name="values" value="1000,2000,1000"> </applet> </body> </html>
間違ってたり、直したらいいところあったら指摘してください。
# Flash使ったら負けかなと思っている。