Java:D2D实现绘制简单的直方图片

本文介绍了使用JavaAWT和Swing库创建一个折线图生成器类LineChartMaker,通过实例化对象并添加直方图数据,展示高中科目成绩的柱状图和折线图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

class Bar {
   
    String name;
    int value;

    public Bar(String name,int value) {
   
        this.name=name;
        this.value=value;
    }
}
class Point{
   
    public Point(float x,float y) {
   
        this.x=x;
        this.y=y;
    }
    float x;
    float y;
}
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.imageio.ImageIO;


/****
 *** author:lao
 *** name:LineChartMaker
 *** date:2024/1/30  16:29
 *** filename:Main
 *** desc:折线图生成器
 ***/
public class LineChartMaker {
   
    // 图片宽度
    private int width;
    // 图片高度
    private int height;
    // img对象
    private BufferedImage img;
    // 绘图环境
    private Graphics2D g2d;
    // 垂直方向起点
    private int yStart;
    // 垂直方向终点
    private int yEnd;
    // 直方图数据列表
    private List<Bar> bars;

    // 构造函数
    public LineChartMaker(int width, int height, int yStart, int yEnd){
   
        this.width = width;
        this.height = height;
        this.img = new BufferedImage(this.width,this.height,BufferedImage.TYPE_INT_RGB);
        this.g2d = (Graphics2D)img.getGraphics();
        this.yStart = yStart;
        this.yEnd = yEnd;
    }

    // 添加一项直方图数据
    public void addBar(String name, int value) {
   
        if(bars == null) {
   
            bars = new ArrayList<Bar>();
        }

        bars.add(new Bar(name, value));
    }

    // 重置屏幕坐标系为笛卡尔坐标系
    private void resetCoordinate() {
   
        AffineTransform trans = new AffineTransform();
        trans
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值