Graphics2D的基本操作
创建
Graphics2D name;
name一般由强制转换得到,如name = (Graphics2D) g.create();
操作
功能 | 方法 |
---|---|
平移到 | translate(dx,dy) |
按照(sx,sy)缩放 | scale(sx,sy) |
以原点为中心旋转theta弧度 | rotate(theta) |
以设定的点为中心旋转theta弧度 | rotate(theta, x, y) |
注:不懂的自己试试就知道了,不详细介绍
基本元素的绘制
图像类型 | 方法 |
---|---|
指定位置粘贴原图像 | drawImage(Image img, int x, int y, ImageObserver observer) |
指定位置粘贴缩放后的图像 | drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) |
把图像d的部分自动缩放粘贴到位置s上 | drawImage(Image img, int dx1, int dy1, dx2, dy2, int sx1, sy1, sx2, sy2, ImageObserver observer) |
在两点间绘制一根直线 | drawLine(int x1, int y1, int x2, int y2) |
在指定位置绘制文字 | drawString(String s, int x, int y) |
绘制矩形边界 | drawRect(int x, int y, int width, int height) |
绘制矩形区域 | fillRect(int x, int y, int width, int height) |
绘制指定形状边界 | Graphics2D.draw(Shape s) |
填充指定形状 | Graphics2D.fill(Shape s) |
注:
1.当图片不是活动的GIF图像时,ImageObserver observer 可以为null。
2.Shape 是由一系列路径对象描述的任意几何图形。
3.如何设置线条粗细、颜色、文字字体?请看下面。
4.使用paint或repaint方法才能显示图像
设置图形状态
Graphics和Graphics2D有至少下面的方法:
- 前景颜色(Foreground color):getColor(); setColor(Color r):
- 背景颜色(Background color):getBackground(); setBackground(Color r);
- 字体(Font):getFont(); setFont(Font f)
- 笔画单元(Stroke):getStroke(); setStroke(Stroke s);
- 渲染设置(Rendering hint):getRendingHint(RenderingHints.Key key);setRendingHint(RenderingHints.Key key, Object value);
- 复制某图像(来自图像或窗口)到同一个表面的其他区域:Graphics.copyArea(int x, int y, int width, int height, int dx, int dy)
- 显示区域(Clip):
- 图像叠加(Composite):
- 着色(Paint):
- 操作/转换属性(Transform):
注:
1.后面的内容书上没有介绍,就没有抄下来。
2.线条的默认宽度是1像素