Graphics 绘图(为每个GUI组件自动创建Graphics实例)
注:每当组件第一次显示或需要重新显示的时候,paintComponent()方法自动调用
格式:protected void paintComponent (Graphics g){
Super.paintComponent(g); //清理界面,保证界面是干净的
…. //调用Graphics方法绘图
}
需重新显示组件时,可调用repaint() 方法。可在子类覆盖paintComponent()方法,但不要覆盖repaint()。
可使用Component类中的getGraphics()获得某组件的Graphics实例,只有重量级组件或是将轻量级组件加载到重量级组件之后才能获得Graphics对象。
1. setColor() //设置颜色
2. setFont() //设置字体
3. getFontMetrics(Font font) //返回指定字体的字体属性FontMetrics
getFontMetrics() //返回当前字体的字体属性FontMetrics
4. drawString(String string, int x, inty) //x代表首字母横坐标,y代表首字母基线高度
5. drawLine(int x1, int y1, int x2, inty2) //绘制直线
6. drawRect(int x, int y, int width, intheight) //绘制矩形
7. fillRect(int x, int y, int width, int height) //绘制填充矩形
8. drawRoundRect(int x, int y, int width, intheight, int aw, int ah) //绘制圆角矩形,aw弧宽半径,ah弧长半径
9. fillRoundRect(int x, int y, int width, intheight, int aw, int ah) //绘制填充圆角矩形
10. draw3Drect(int x, int y, int width, intheight, int raised) //绘制3D矩形,raised表示从表面凸起还是凹进
11. drawOval(int x, int y, int width, intheight) //绘制椭圆
12. fillOval(int x, int y, int width, intheight) //绘制填充椭圆
13. drawArc(int x, int y, int width, intheight, int StartAngle, int arcAngle) //绘制弧形
弧形也是根据其外接矩形绘制的,x,y,width,height为外接矩形参数,arcAngle为弧形扫描角度。
14. fillArc(int x, int y, int width, intheight, int StartAngle, int arcAngle) //绘制扇形
15. drawpolygon(Polygon) //绘制多边形
drawpolygon(int[]xpoints, int[] ypoints, int npoints ) //绘制多边形
16. fillpolygon(Polygon) //绘制填充多边形
fillpolygon(int[]xpoints, int[] ypoints, int npoints ) //绘制填充多边形
17. drawPolyline() //绘制折现段
18. dispose() //释放图形环境
19. drawImage(Image img,...ImageObserver observer),
observer是指绘画面板对象,基本所有的GUI组件都实现了ImageObserver接口。