当前位置:首页 > Java

java 如何画圆

2026-03-03 11:57:52Java

使用 Graphics 类绘制圆

在 Java 中,可以通过 Graphics 类的 drawOvalfillOval 方法绘制圆。这些方法通常用于 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 绘制更灵活的圆

Graphics2DGraphics 的子类,提供更多绘图选项,例如设置线条粗细和抗锯齿。

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 环境下,可以通过 BufferedImageGraphics2D 绘制圆并保存为图片。

java 如何画圆

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"));
    }
}

标签: java
分享给朋友:

相关文章

如何删除java

如何删除java

卸载 Java 的步骤 Windows 系统: 打开控制面板,选择“程序和功能”或“卸载程序”,在列表中找到 Java 相关条目(如“Java Runtime Environment”或“Java D…

如何学习java

如何学习java

学习Java的步骤 理解基础知识 从Java的基本语法开始,包括变量、数据类型、运算符、控制流(if-else、循环)等。掌握这些基础概念是后续学习的基石。 学习面向对象编程(OOP) Java是一…

java如何调用接口

java如何调用接口

调用接口的基本方法 在Java中调用接口通常涉及实现接口或使用接口引用对象。以下是几种常见场景的示例: 定义接口 public interface MyInterface { void d…

java如何创建项目

java如何创建项目

使用IDE创建Java项目(以IntelliJ IDEA为例) 打开IntelliJ IDEA,选择“New Project”。 在左侧菜单中选择“Java”,确保已配置JDK(若无需手动添加)。 勾…

java中如何获取当前时间

java中如何获取当前时间

获取当前时间的几种方法 使用 java.time 包(Java 8及以上推荐) import java.time.LocalDateTime; LocalDateTime currentTime =…

java如何实现多继承

java如何实现多继承

在Java中,由于语言设计本身不支持多继承(即一个类不能直接继承多个父类),但可以通过以下方式间接实现类似多继承的效果: 使用接口实现多继承 接口允许一个类实现多个接口,从而继承多个抽象行为。接口中…