Graphics2D & Shape
来源:互联网 发布:图片剪裁软件 编辑:程序博客网 时间:2024/05/22 06:57
来源:http://blog.csdn.net/rexih/article/details/46445225
Graphics2D类包含绘制直线,矩形,椭圆等方法
Component的paintComponent方法会自动获取一个Graphics2D对象,但是参数列表定义的是Graphics对象,通过强转来使用
Graphics2D g2d=(Graphics2D)g;
绘制具体图形的类 : Line2D,Rectangle2D,Ellipse2D,Point2D
绘制图形的步骤:
1.获取一个Graphics2D对象
2.创建一个实现Shape接口的图形对象,设置好其属性
3.调用Graphics2D的draw 方法
像素坐标的类型:
1.最初是int型,表示屏幕上的像素坐标
2.Java 2D库采用浮点型
a.这样可以转换成更有意义的单位,比如微米,英寸
b.屏幕使用单精度即可,而且float存储空间小,计算速度可能比double快
3.为了方便通常使用Double。 float在赋值时需要手动添加f,double型的返回值需要强转成float
所以,图形类通常有两个静态内部类作为子类Float和Double,比如Rectangle2D.Float/Rectangle2D.Double
对于这两个子类:
1.子类只覆盖了超类的方法,所以可以用超类的对象变量直接引用。Rectangle2D rect=new Rectangle2D.Double(...);
2.Rectangle2D的方法返回值都是double,引用的对象是Rectangle2D.Float的话,调用方法,比如getWidth,返回值也是double
常用(特殊)的构造图形的情况:
1.矩形 知道对角点
使用rect.setFrameFromDiagonal(px,py,qx,qy);
或者参数为2个Point2D对象 rect.setFrameFromDiagonal(p,q);
2.矩形 知道中心点和对角点
使用rect.setFrameFromCenter(px,py,qx,qy);rect.setFrameFromCenter(p,q);
3.椭圆 知道中心点和长 宽
ellipse.setFrameFromCenter(cx,cy,cx-width/2,cy-height/2);
画圆:ellipse.setFrameFromCenter(cx,cy,cx+radius,cy+radius);
4.知道椭圆的矩形框架
ellipse.setFrame(rect);
5.画直线
new Line2D.Double(sx,sy,ex,ey);
new Line2D.Double(p,q);
- Graphics2D & Shape
- Graphics2D & Shape - 绘制2D图形
- Graphics2D渲染
- Graphics2D渲染
- Graphics2D学习
- Graphics2D渲染(Rendering with Graphics2D)
- Graphics2D渲染(Rendering with Graphics2D)上
- Graphics2D渲染(Rendering with Graphics2D)上
- Graphics2D渲染(Rendering with Graphics2D)下
- shape
- Shape
- shape
- shape
- Shape
- shape
- shape
- shape
- shape
- 第三周周四总结
- 毕设笔记 arduino+esp8266环境
- android 模拟器root
- PAT-乙级-1017. A除以B
- POJ1936-All in All(纯水题)
- Graphics2D & Shape
- python机器学习(2.3)-----聚类
- Java-静态static用法
- 记录我的小网站之旅
- Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。
- 第一次上课:特征值特征向量的几何直观
- 简单程序
- 【CUGBACM15级BC第四场 A】hdu 4931 Happy Three Friends
- eclipse中透过search打开第二个文件时第一个文件就自己关闭了