// JavaのGraphicsクラスの描画メソッド // 2008年6月1日 後 保範(東京工芸大学) import java.awt.*; import javax.swing.*; public class Grow extends JPanel{ public Grow(){ setBackground(Color.white); //背景を白に setPreferredSize(new Dimension(700, 700)); } protected void paintComponent(Graphics g){ //描画の指示 super.paintComponent(g); //背景を描く // データの設定 String s1[] = {"a=100,b=2","a=30,b=1","a=100,b=1"}; int a[] = {100, 30, 100}; int b[] = {2, 1, 1}; int c[][] = {{255,0,0},{0,255,0},{0,0,255}}; // 座標軸を描く g.drawLine(100,600, 650,600); // X-co. g.drawLine(600,600, 600,610); // X-co.-- g.drawLine(100,600, 100,50); // Y-co. g.drawLine(100,100, 90,100); // Y-co.-- // タイトルを描く g.setFont(new Font("Serif",Font.PLAIN,22)); g.drawString("図1. 成長曲線の比較",250,675); g.setFont(new Font("Serif",Font.PLAIN,18)); // x-y座標を描く g.drawString("10.0",580,625); // 10.0 g.drawString("1.0",60,105); // 1.0 g.drawString("x",350,630); // x g.drawString("f(x)",50,350); // f(x) // 3種類の成長曲線を描く for (int k=0; k<3; k++) { g.setColor(new Color(c[k][0],c[k][1],c[k][2])); //色の設定 g.drawString(s1[k],280,140*k+120); //曲線の説明を描く int X = 100; int Y=600; // xの値を0.0から10.0まで0.02刻みに変化 for (double x=0.0; x<=10.0; x+=0.02) { double y=1.0/(1.0+a[k]*Math.exp(-b[k]*x)); //y=f(x)の計算 int X1 = (int)(50*x + 100); //xを描く座標Xに変換 int Y1 = (int)(-500*y + 600); //yを描く座標Yに変換 g.drawLine(X,Y, X1,Y1); //成長曲線を描く X = X1; Y = Y1; } //一つ前の値に } } public static void main(String[] args){ JFrame frame = new JFrame("Draw Line"); //JFrame作成 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //xで終了設定 Grow h = new Grow(); //Graph1のオブジェクトを生成 frame.add(h, BorderLayout.CENTER); //フレームにGraphを追加 frame.pack(); //フレームを指定の大きさにする frame.setVisible(true); //フレームに画面を見せる } }