package com.luwan;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class DrawingExample extends JFrame {
public DrawingExample() {
setTitle("Java绘制图形示例");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics2D g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
// 绘制直线
g2d.drawLine(50, 50, 200, 50);
// 绘制矩形
g2d.drawRect(50, 100, 150, 100);
// 绘制圆形
g2d.drawOval(50, 250, 100, 100);
// 绘制字符串
g2d.drawString("Hello, Java绘图!", 50, 400);
}
public static void main(String[] args) {
new DrawingExample();
}
}