javapackage 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(); } } }
使用 java画图。
laocooon5238578862024-10-15 20:42
相关推荐
来自天蝎座的孙孙9 分钟前
洛谷P1595讲解(加强版)+错排讲解GawynKing29 分钟前
图论(5)最小生成树算法试剂界的爱马仕34 分钟前
胶质母细胞瘤对化疗的敏感性由磷脂酰肌醇3-激酶β选择性调控打不了嗝 ᥬ᭄1 小时前
Linux 信号张子夜 iiii1 小时前
机器学习算法系列专栏:主成分分析(PCA)降维算法(初学者)一匹电信狗1 小时前
【C++】异常详解(万字解读)sp422 小时前
白话 LRU 缓存及链表的数据结构讲解(二)PineappleCoder2 小时前
为什么说发布 - 订阅是代码的 “万能胶水”?解耦逻辑全解析墨染点香2 小时前
LeetCode 刷题【43. 字符串相乘】weixin_307779132 小时前
VS Code配置MinGW64编译Ipopt库