java如何画圆
使用Java绘制圆形的方法
使用Graphics2D绘制圆形
Graphics2D是Java中用于绘制2D图形的类,可以通过以下代码绘制圆形:
import javax.swing.*;
import java.awt.*;
public class DrawCircle extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.drawOval(50, 50, 100, 100); // 绘制空心圆
g2d.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);
}
}
使用JavaFX绘制圆形
JavaFX提供了更现代的图形绘制功能:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class JavaFXCircle extends Application {
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(100, 100, 50);
circle.setFill(Color.BLUE);
circle.setStroke(Color.BLACK);
StackPane root = new StackPane();
root.getChildren().add(circle);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("JavaFX Circle");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
使用AWT绘制圆形
对于简单的AWT应用程序:

import java.awt.*;
import java.awt.event.*;
public class AWTCircle extends Frame {
public AWTCircle() {
setSize(300, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public void paint(Graphics g) {
g.setColor(Color.GREEN);
g.drawOval(100, 100, 100, 100);
}
public static void main(String[] args) {
AWTCircle circle = new AWTCircle();
circle.setVisible(true);
}
}
使用BufferedImage绘制圆形
在内存中创建图像并绘制圆形:
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class BufferedImageCircle {
public static void main(String[] args) {
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();
JLabel label = new JLabel(new ImageIcon(image));
JFrame frame = new JFrame();
frame.add(label);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
使用参数方程绘制圆形
通过数学计算绘制圆形:
import javax.swing.*;
import java.awt.*;
public class ParametricCircle extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.MAGENTA);
int centerX = 150, centerY = 150, radius = 100;
for (double angle = 0; angle < 2 * Math.PI; angle += 0.01) {
int x = (int) (centerX + radius * Math.cos(angle));
int y = (int) (centerY + radius * Math.sin(angle));
g2d.fillRect(x, y, 2, 2);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Parametric Circle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ParametricCircle());
frame.setSize(300, 300);
frame.setVisible(true);
}
}






