使用 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();
        }
    }
}
相关推荐
dying_man4 分钟前
LeetCode--24.两两交换链表中的结点
算法·leetcode
yours_Gabriel4 分钟前
【力扣】2434.使用机器人打印字典序最小的字符串
算法·leetcode·贪心算法
草莓熊Lotso1 小时前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
KyollBM1 小时前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
CV点灯大师1 小时前
C++算法训练营 Day10 栈与队列(1)
c++·redis·算法
GGBondlctrl1 小时前
【leetcode】递归,回溯思想 + 巧妙解法-解决“N皇后”,以及“解数独”题目
算法·leetcode·n皇后·有效的数独·解数独·映射思想·数学思想
武子康2 小时前
大数据-276 Spark MLib - 基础介绍 机器学习算法 Bagging和Boosting区别 GBDT梯度提升树
大数据·人工智能·算法·机器学习·语言模型·spark-ml·boosting
武子康2 小时前
大数据-277 Spark MLib - 基础介绍 机器学习算法 Gradient Boosting GBDT算法原理 高效实现
大数据·人工智能·算法·机器学习·ai·spark-ml·boosting
Andrew_Xzw2 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法