每个Component都有一个paint(Graphics g)用于实现绘图目的,每次重画该Component时都自动调用paint方法。
Graphics类中提供了很多绘图方法,如:
drawRect(int x,int y,int width,int height)
fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
import java.awt.* ;
public class TestPaint
{
public static void main(String args[]) {
new PaintFrame() ;
}
}
class PaintFrame extends Frame
{
PaintFrame() {
setBounds(300,100,500,400) ;
setVisible(true) ;
}
public void paint(Graphics g) {
Color c = g.getColor() ;
g.setColor(Color.RED) ;
g.fillOval(50,50,100,100) ;
g.setColor(Color.BLUE) ;
g.fillRect(200,200,100,100) ;
g.setColor(c) ;
}
}
本文介绍了一个简单的Java程序示例,演示了如何使用Graphics类中的方法进行基本的图形绘制,包括填充椭圆和矩形。

2540

被折叠的 条评论
为什么被折叠?



