This example creates a frame and draws an oval within the frame.
//导入awt和swing包,用于创建图形界面
import java.awt.*;
import javax.swing.*;
class BasicDraw extends JComponent {
public void paint(Graphics g) {
//创建一个Graphics2D对象
Graphics2D g2d = (Graphics2D)g;
//画椭圆
g2d.drawOval(0, 0, getSize().width-1,
getSize().height-1);
}
public static void main(String[] args) {
//创建主窗口
JFrame frame = new JFrame();
//添加一个匿名BasicDraw对象
frame.getContentPane().add(new BasicDraw());
int frameWidth = 300;
int frameHeight = 300;
frame.setSize(frameWidth, frameHeight);
frame.setVisible(true);
}
}