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
相关推荐
Hello:CodeWorld29 分钟前
C 风格变参 vs C++ 变参模板:核心区别与选型指南8Qi82 小时前
LeetCode 516:最长回文子序列youngerwang3 小时前
【从搬运工到协处理器:网卡芯片架构、算法、验证与边缘演进深度剖析】KaMeidebaby3 小时前
卡梅德生物技术快报|纯化重组蛋白实操详解手写码匠4 小时前
从零实现 Prompt 工程引擎:结构化提示、自动优化与多轮自省体系无限码力4 小时前
阿里算法岗 0530笔试真题 - 多约束条件下的元素匹配统计lqqjuly5 小时前
MLA — 多头潜在注意力深度解析吴可可1235 小时前
SolidWorks草图转三维DWG技巧redaijufeng5 小时前
C++雾中风景7:闭包小欣加油6 小时前
leetcode287寻找重复数