使用 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();
        }
    }
}
相关推荐
对方正在长头发丿7 分钟前
LETTERS(DFS)
c++·笔记·算法·深度优先·图论
Qian_ShouYi8 分钟前
MATLAB 代码学习
学习·算法·matlab
lovebugs1 小时前
K8s面试第一篇:初识Kubernetes——核心概念与组件详解
后端·算法·面试
HelloDam1 小时前
基于元素小组的归并排序算法
后端·算法·排序算法
HelloDam1 小时前
基于连贯性算法的多边形扫描线生成(适用于凸多边形和凹多边形)【原理+java实现】
算法
uhakadotcom2 小时前
Apache Airflow入门指南:数据管道的强大工具
算法·面试·github
跳跳糖炒酸奶3 小时前
第四章、Isaacsim在GUI中构建机器人(2):组装一个简单的机器人
人工智能·python·算法·ubuntu·机器人
绵绵细雨中的乡音3 小时前
动态规划-第六篇
算法·动态规划
程序员黄同学3 小时前
动态规划,如何应用动态规划解决实际问题?
算法·动态规划
march_birds4 小时前
FreeRTOS 与 RT-Thread 事件组对比分析
c语言·单片机·算法·系统架构