棒グラフの課題

課題

プログラムをつくりなさい。

棒グラフの課題

ファイル名 DrawRectG.java

import java.awt.*;
import javax.swing.*;

public class DrawRectG extends JFrame{
    public DrawRectG() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400,300);
        setTitle("グラフ");
        MyPanel mypnl = new MyPanel();
        mypnl.setSize(400,300);
        mypnl.setBackground(Color.white);

        setLayout(new BorderLayout());
        add(mypnl, BorderLayout.CENTER);

        setVisible(true);
    }
    public static void main(String[] args){
        DrawRectG myframe = new DrawRectG();
    }


    public class MyPanel extends JPanel{
      int[] data={10,40,120,60,80};
      int bottom=250;
      int left  =50;
      Color c = new Color(0,0,0);
      int dx = 50;
      public void paintComponent(Graphics myg){
         super.paintComponent(myg);
         
         myg.setColor(c);
         myg.drawLine(x1,y1,x2,y2); //x軸
         myg.drawLine(x1,y1,x2,y2); //y軸
         for(int i=0; data.length>i ; i++){
               myg.setColor(c);
               myg.fillRact(x,y,w,h);
         }
       }
    }
}

paintComponent()内は未完成です。drawLineのx1,y1,x2,y2や fillRactのx,y,w,hは別の値やbottom,left,dx,data[i]を使った式に置き換えなければなりません。

もくじ

聖愛高等学校
http://www.seiai.ed.jp/
Oct.2009