⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 histogram.java

📁 一个柱状图的代码....采用了for循环算法实现
💻 JAVA
字号:
// Java core packages
import java.awt.*;
import java.awt.event.*;

// Java extension packages
import javax.swing.*;

public class Histogram extends JFrame
{
	private int[] yield={74,56,76,90,56,67,87,97,54,78,79,70};//原始数据
	private int[] temp=new int[12];//用于存放转换后的数据
   // constructor sets window's title bar string and dimensions
   public Histogram()
   {
      super( "产量直方图" );
      
      setSize(800, 600);
      setVisible(true);
   }

   // draw rectangles and Strings in different colors
   public void paint( Graphics g )
   {
      // call superclass's paint method
      super.paint( g );
      g.setFont(new Font("黑体",Font.BOLD,22));
      g.drawString("月 份 产 量 统 计 表",300, 80);
      
      g.setFont(new Font("华文行楷",Font.CENTER_BASELINE,16));
      g.drawString("制作人:胡冬",600, 100);
      g.drawString("2006-11-17",605, 120);
      //
      g.drawLine(50,150,50,550);
      g.drawLine(50,550,650,550);
      //
      g.drawLine(50,150,45,155);
	  g.drawLine(50,150,55,155);
	  g.drawLine(650,550,645,545);
	  g.drawLine(650,550,645,555);
	  //
	  g.drawString(0+"",42, 566 );
	  g.setFont(new Font("方正姚体",Font.BOLD,14));
	  g.drawString("产量/个",10,146);
	  g.drawString("月份",645,572);
	  //
	  final int a=20;//横坐标图形间距
	  
	  interect();//将原始数据转换成适合显示区域的数据
	  
	  //利用循环画出矩形
	  for(int i=0;i<temp.length;i++)
	  {
	  	g.setColor(new Color(30+18*i,240-15*i,240-20*i));
	  	g.fillRect((50+20*(2*i+1)),(550-temp[i]),a,temp[i]);
	  	
	  	g.setFont(new Font("宋体",Font.BOLD,14));
	  	
	  	g.drawString(yield[i]+"",(50+20*(2*i+1)),(545-temp[i]));
	  	
	  	g.drawString((i+1)+"月",(50+20*(2*i+1)),572);
	  		
	  }
	  	  
   }

	//将原始数据转换成适合显示区域的数据
	public void interect()
	{
		int tp=yield[0];
		for(int i=1;i<yield.length;i++)
		{
			if(tp<yield[i])
				tp=yield[i];
		}
		for(int j=0;j<temp.length;j++)
		{
			temp[j]=yield[j]*400/tp;
		}
	}
   // execute application
   public static void main( String args[] )
   {
      Histogram application = new Histogram();

      application.setDefaultCloseOperation( 
         JFrame.EXIT_ON_CLOSE );
   }

}  // end class ShowColors

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -