java 如何画圆
使用 Graphics 类绘制圆
在 Java 中,可以通过 Graphics 类的 drawOval 或 fillOval 方法绘制圆。这些方法通常用于 Swing 或 AWT 的绘图组件中。
import javax.swing.*;
import java.awt.*;
public class DrawCircle extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawOval(50, 50, 100, 100); // 绘制空心圆 (x, y, width, height)
g.fillOval(200, 50, 100, 100); // 绘制实心圆
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Circle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawCircle());
frame.setSize(400, 200);
frame.setVisible(true);
}
}
使用 Graphics2D 绘制更灵活的圆
Graphics2D 是 Graphics 的子类,提供更多绘图选项,例如设置线条粗细和抗锯齿。

import javax.swing.*;
import java.awt.*;
public class DrawCircleWithGraphics2D extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
g2d.setStroke(new BasicStroke(3)); // 设置线条粗细
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
g2d.drawOval(50, 50, 100, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Circle with Graphics2D");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawCircleWithGraphics2D());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
使用 JavaFX 绘制圆
在 JavaFX 中,可以使用 Circle 类绘制圆。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class DrawCircleFX extends Application {
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(100, 100, 50); // (centerX, centerY, radius)
circle.setFill(Color.TRANSPARENT);
circle.setStroke(Color.GREEN);
circle.setStrokeWidth(2);
Pane pane = new Pane(circle);
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("JavaFX Circle");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
使用数学公式绘制圆
如果需要通过数学公式计算圆的点并绘制,可以使用以下方法(以 Swing 为例):
import javax.swing.*;
import java.awt.*;
public class DrawCircleWithMath extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int centerX = 100, centerY = 100, radius = 50;
g.setColor(Color.BLACK);
for (int angle = 0; angle < 360; angle++) {
double radians = Math.toRadians(angle);
int x = (int) (centerX + radius * Math.cos(radians));
int y = (int) (centerY + radius * Math.sin(radians));
g.drawLine(x, y, x, y); // 绘制单个点
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Circle with Math");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawCircleWithMath());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
使用 BufferedImage 绘制圆
在非 GUI 环境下,可以通过 BufferedImage 和 Graphics2D 绘制圆并保存为图片。
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class DrawCircleToImage {
public static void main(String[] args) throws Exception {
BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.ORANGE);
g2d.fillOval(50, 50, 100, 100);
g2d.dispose();
ImageIO.write(image, "PNG", new File("circle.png"));
}
}






