使用 java画图。

java 复制代码
package p1008;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class CirclePoints extends JPanel {
    private int n; // Number of points
    private final int radius = 200; // Radius of the circle

    public CirclePoints(int n) {
        this.n = n;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // Calculate the center of the panel
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        // Draw points and connect them
        double angleIncrement = 2 * Math.PI / n;
        int[] xPoints = new int[n];
        int[] yPoints = new int[n];

        for (int i = 0; i < n; i++) {
            double angle = i * angleIncrement;
            int x = (int) (centerX + radius * Math.cos(angle));
            int y = (int) (centerY + radius * Math.sin(angle));
            xPoints[i] = x;
            yPoints[i] = y;

            // Draw the point
            g2d.fillOval(x - 5, y - 5, 10, 10);
        }

        // Connect the points
        for (int i = 0; i < n; i++) {
            for(int j=i+1;j<n;j++){
                g2d.drawLine(xPoints[i], yPoints[i], xPoints[j], yPoints[j]);
            }

        }
    }

    public static void main(String[] args) {
        int n = Integer.parseInt(JOptionPane.showInputDialog("请输入一个5到36之间的整数:"));

        if (n < 5 || n > 36) {
            System.out.println("输入无效,请输入5到36之间的整数。");
            return;
        }

        JFrame frame = new JFrame("Circle Points");
        CirclePoints circlePoints = new CirclePoints(n);
        frame.add(circlePoints);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        // Save the panel as an image
        BufferedImage image = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        circlePoints.paint(g2d);
        g2d.dispose();

        try {
            ImageIO.write(image, "jpg", new File("res.jpg"));
            System.out.println("图片已保存为 res.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
相关推荐
中微极客1 分钟前
降维算法75倍加速:从PCA到稀疏字典学习的工程实践
人工智能·学习·算法
storyseek22 分钟前
前缀和实现Kogge-Stone算法
数据结构·算法
元Y亨H1 小时前
开发者必须掌握的十大核心算法
算法
元Y亨H1 小时前
深度解构:数据结构与算法的理论基石与工程演进
数据结构·算法
元Y亨H1 小时前
数据结构与算法的通俗指南
数据结构·算法
2301_764441331 小时前
用动力学系统(微分方程)为 Kernberg 的客体关系单元提供数学化的操作定义,把“自体—客体“这对心理结构建模成一个二维耦合系统
数据结构·python·算法·数学建模
林泽毅1 小时前
PyTRIO快速入门(二):Datum构建
人工智能·算法·产品
keep intensify2 小时前
最长有效括号
算法·leetcode·动态规划
CoderYanger2 小时前
A.每日一题:1979. 找出数组的最大公约数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
猫头虎2 小时前
什么是ZCode for GLM-5.2?
开发语言·人工智能·python·科技·算法·ai编程·ai写作