eclipse 制作静态柱状图

package zijimoni;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.*;

public class Histogram extends JPanel {
 
 private int[] count;
 
 private int x;
 private static final int length = 100;
 int i;
 int barHeight;
 int interval ;

 public Histogram() {
  // setCount();//不再进行随机数生成,使用相同的数组
  count = new int[] { 5, 6, 7, 57, 38, 8, 3, 9, 1, 48, 49, 50, 10, 90,
    91, 92, 28, 29, 30, 11, 12, 4, 13, 14, 15, 16, 17, 81, 82, 18,
    19, 20, 21, 22, 25, 26, 95, 96, 27, 31, 32, 40, 41, 33, 34, 35,
    36, 37, 42, 43, 47, 51, 52, 53, 55, 56, 58, 59, 60, 86, 87, 64,
    65, 66, 77, 68, 69, 70, 71, 72, 54, 73, 78, 75, 67, 76, 79, 80,
    83, 2, 84, 85, 74, 88, 89, 93, 39, 94, 97, 44, 45, 46, 98, 61,
    62, 63, 99, 23, 24, 100 };
  
  
 }

  
 protected void paintComponent(Graphics g) {
  int height = getHeight();
  int width=getWidth();
     int x=30;
  
      g.setColor(Color.green);
       for (int i = 0; i < 100; i++) {
         barHeight =
       (int)(((double)count[i]/(double)100)*(height-55));
       int interval = (width - 40)/length;
   g.fillRect(x, height - barHeight, interval, barHeight);
   x += interval; 
   
  }
       x=-1;
  
 }

 public static void main(String[] args) {
  

  Histogram q = new Histogram();
  JFrame jf=new JFrame();
  jf.setTitle("静态柱状图");
  jf.add(q); 
  jf.setVisible(true);
  jf.setSize(1000, 500);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jf.setResizable(false);
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值