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

📄 chart.java

📁 一本jsp自学教材
💻 JAVA
字号:
package test;
import java.io.*; 
import com.sun.image.codec.jpeg.*; 
import java.awt.image.*; 
import java.awt.*; 

public class Chart
{
BufferedImage image; 
public Chart(){}
public void createImage(String fileLocation)
{
try{
 //构建输出流
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
}
catch(Exception e)
{
 System.out.println(e);
}
}
public void graphicsGeneration(int h[],String path)
{ 
final int X=20;
//画布的宽度
int imageWidth = 320; 
//画布的高度  
int imageHeight = 320;
int columnWidth=20;  //柱的宽度 
int columnHeight=280;  //柱的最大高度 

image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); 
Graphics graphics =image.getGraphics();
  //画布颜色
graphics.setColor(Color.white); 
graphics.fillRect(0,0,imageWidth,imageHeight);
  //设置画笔的颜色 
graphics.setColor(Color.red); 
  //画长方形柱
for(int i=1;i<=h.length;i++)
 {
   graphics.fillRect(X+i*columnWidth, columnHeight-h[i-1], columnWidth, h[i-1]);  
 }
graphics.setColor(Color.black); 
graphics.drawRect(X+18, columnHeight, 250, 2);
graphics.drawRect(X+18, 40, 2, columnHeight-40);
  //画柱的边框
for(int n=1;n<=h.length;n++)
 {
   graphics.drawRect(X+n*columnWidth, columnHeight-h[n-1], columnWidth, h[n-1]);
 }
graphics.setColor(Color.blue); 
 //画横坐标
for(int u=0;u<=h.length;u++)
{
String n=new Integer(u).toString();
graphics.drawString(n,25+u*20,columnHeight+20);
}
 //画纵坐标
for(int q=1;q<=10;q++)
{
String qq=new Integer(q).toString();
graphics.drawString(qq,25,columnHeight+3-q*20);
}
for(int j=1;j<=10;j++)
 {
   graphics.drawRect(X+18, columnHeight-j*20, 250,0);
 }
  //设置文字字体
 Font font = new Font("黑体", Font.BOLD, 18);
 graphics.setFont(font);
 //设置要插入的文字
 graphics.drawString("全年各月同比增长率",80,40);
 //按指定路径产生图片
this.createImage(path); 
}


}

⌨️ 快捷键说明

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