Java建立坐标画图

概述

在科学研究、数据分析、图形展示等领域中,经常需要使用图形表示数据。Java作为一门强大的编程语言,提供了丰富的图形库和绘图功能,可以方便地建立坐标系并进行画图操作。

在本文中,我们将介绍如何使用Java建立坐标并画出饼状图(Pie Chart)。我们将使用Java的Swing库来进行图形绘制,以及使用第三方库Apache Commons Math来进行数据计算。

准备工作

在开始之前,我们需要准备以下工作:

  1. 安装Java开发环境(JDK):请确保您的计算机已经安装了Java Development Kit(JDK)。

  2. 导入第三方库:我们将使用Apache Commons Math库来进行数据计算。您可以从官方网站下载并导入该库。

建立坐标

在Java中,我们可以使用Swing库来创建图形界面并进行绘图操作。下面是一个简单的示例代码,用于创建一个具有坐标系的窗口:

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;

public class CoordinateGraph extends JPanel {
    private int width;
    private int height;
  
    public CoordinateGraph(int width, int height) {
        this.width = width;
        this.height = height;
    }
  
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        // 绘制坐标轴
        g.setColor(Color.BLACK);
        g.drawLine(50, height - 50, 50, 50); // y轴
        g.drawLine(50, height - 50, width - 50, height - 50); // x轴
        
        // 绘制坐标轴刻度
        int interval = (width - 100) / 10;
        for (int i = 0; i <= 10; i++) {
            int x = 50 + i * interval;
            g.drawLine(x, height - 45, x, height - 55); // x轴刻度
            g.drawString(Integer.toString(i), x - 3, height - 30); // x轴刻度值
        }
    }
  
    public static void main(String[] args) {
        int width = 600;
        int height = 400;
        
        JFrame frame = new JFrame("Coordinate Graph");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
  
        CoordinateGraph graph = new CoordinateGraph(width, height);
        frame.add(graph);
        frame.setVisible(true);
    }
}

代码解释:

  • 我们创建了一个名为CoordinateGraph的类,继承自JPanel,用于绘制坐标系。
  • 在paintComponent()方法中,我们通过Graphics对象进行绘制操作。首先绘制了坐标轴,然后绘制了坐标轴的刻度。
  • 在main()方法中,我们创建了一个JFrame窗口,并将CoordinateGraph对象添加到窗口中进行显示。

画饼状图

接下来,我们将使用Apache Commons Math库来计算数据,并以饼状图的形式展示数据。下面是一个示例代码,显示了如何绘制饼状图:

import org.apache.commons.math3.util.Precision;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import java.util.List;

public class PieChart extends JPanel {
    private List<Double> data;
  
    public PieChart(List<Double> data) {
        this.data = data;
    }
  
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        double total = 0;
        for (double value : data) {
            total += value;
        }
        
        double startAngle = 0;
        int x = 100;
        int y = 100;
        int width = 200;
        int height = 200;
        
        for (double value : data) {
            double extent = value / total * 360;
            g.setColor(Color.getHSBColor((float)Math.random(), 1, 1));
            g.fillArc(x, y, width, height, (int)startAngle, (int)extent);
            startAngle += extent;
        }
    }
  
    public static void main(String[] args) {
        List<Double> data = new ArrayList<>();
        data.add(45.0);
        data.add(30.0);
        data.add(25.