使用 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();
        }
    }
}
相关推荐
yaoh.wang6 小时前
力扣(LeetCode) 13: 罗马数字转整数 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
T1ssy6 小时前
布隆过滤器:用概率换空间的奇妙数据结构
算法·哈希算法
hetao17338377 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
鲨莎分不晴8 小时前
强化学习第五课 —— A2C & A3C:并行化是如何杀死经验回放
网络·算法·机器学习
搞科研的小刘选手9 小时前
【ISSN/ISBN双刊号】第三届电力电子与人工智能国际学术会议(PEAI 2026)
图像处理·人工智能·算法·电力电子·学术会议
拉姆哥的小屋9 小时前
从混沌到秩序:条件扩散模型在图像转换中的哲学与技术革命
人工智能·算法·机器学习
Sammyyyyy9 小时前
DeepSeek v3.2 正式发布,对标 GPT-5
开发语言·人工智能·gpt·算法·servbay
sin_hielo9 小时前
leetcode 2110
数据结构·算法·leetcode
Jay20021119 小时前
【机器学习】33 强化学习 - 连续状态空间(DQN算法)
人工智能·算法·机器学习
panzer_maus10 小时前
归并排序的简单介绍
java·数据结构·算法