使用 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();
        }
    }
}
相关推荐
洛水水4 分钟前
【力扣100题】48.乘积最大子数组
算法·leetcode·职场和发展
小小de风呀4 分钟前
de风——【从零开始学C++】(七):string类详解
开发语言·c++·算法
YL200404264 分钟前
042将有序数组转换为二叉搜索树
数据结构·算法·leetcode
qq_296553279 分钟前
矩阵对角线遍历:从暴力到最优的优雅解法
数据结构·线性代数·算法·青少年编程·矩阵·深度优先遍历
洛水水9 分钟前
【力扣100题】50.最长有效括号
算法·leetcode·职场和发展
数智工坊10 分钟前
【BLIP论文阅读】:统一视觉语言理解与生成的自举式预训练范式
论文阅读·人工智能·深度学习·算法·transformer
yyy(十一月限定版)11 分钟前
问题解决策略搜索训练3
算法
吃好睡好便好13 分钟前
在Matlab中绘制圆锥三维曲面图
开发语言·人工智能·学习·算法·matlab·信息可视化
兰令水14 分钟前
topcode【随机算法题】【2026.5.15打卡-java版本】
java·算法·leetcode
洛水水15 分钟前
【力扣100题】44.完全平方数
算法·leetcode·职场和发展