- 5
- 0
- 约2.14千字
- 约 14页
- 2018-01-25 发布于浙江
- 举报
ch13 - 图形 Java语言程序设计基础篇课件
YANGLIN@BNUITC Java程序设计 第13章 图形 学习目标 理解Java的坐标系 使用Graphics类中的方法画图 使用paintComponent方法在面板中绘制图形 Java坐标系 (0,0) x y (x,y) (0,0) x y (x,y) 传统坐标系 Java坐标系 组件内部以组件左上角为坐标原点。 组件外部以组件所在容器的左上角为坐标原点。 java.awt.Graphics 可以利用Graphics类中的方法绘制线段、矩形、椭圆、圆弧、多边形。 draw3DRect, drawArc, drawImage, drawLine, drawOval, drawPolygon, drawPolyline, drawRect, drawRoundRect, drawString fill3DRect, fillArc, fillOval, fillPolygon, fillRect, fillRoundRect get/setFont, get/setColor 使用Graphics对象 调用JComponent类的getGraphics()方法可以获取Graphics对象。 覆盖Jcomponent类的paintComponent方法。该方法在组件第一次显示或需要重绘时,由Java虚拟机自动调用。 protected void paintComponent(Graphics g) 通常在JPanel中进行绘图。 Example: TestGetGraphics.java, TestPaintComponent.java, TestPanelDrawing.java 绘制字符串 drawString (String str, int x, int y); (x,y) 绘制线段 drawLine(int x1, int y1, int x2, int y2); (x1,y1) (x2,y2) 绘制矩形 (x,y) w h (x,y) w h drawRect(int x, int y, int w, int h); fillRect(int x, int y, int w, int h); (x,y) w h fillRoundRect(int x, int y, int w, int h, int aw, int ah); ah/2 aw/2 (x,y) w h draw3DRect(int x, int y, int w, int h Boolean raise); Example:DrawRectangles.java 绘制圆形 drawOval(int x, int y, int w, int h); fillOval(int x, int y, int w, int h); (x,y) w h Example:DrawOvals.java 绘制圆弧 drawArc(int x, int y, int w, int h, int angle1, int angle2); fillArc(int x, int y, int w, int h, int angle1, int angle2); (x,y) w h angle2 angle1 Example:DrawArcs.java 绘制多边形 int[] x = {25, 216, 62, 121, 180}; int[] y = {80, 80, 191, 11, 191}; g.drawPolygon(x, y, x.length); g.drawPolyline(x, y, x.length); (x[0],y[0]) (x[3],y[3]) (x[1],y[1]) (x[4],y[4]) (x[2],y[2]) (x[0],y[0]) (x[3],y[3]) (x[1],y[1]) (x[4],y[4]) (x[2],y[2]) 使用Polygon绘制多边形 Polygon polygon = new Polygon(); polygon.addPoint(25, 80); polygon.addPoint(216, 80); polygon.addPoint(62, 191); polygon.addPoint(121, 11); polygon.addPoint(180, 191); g.drawPolygon(polygon); Example:DrawPolygon.java 例 绘制时钟 编写程序,绘制时钟 12 6 9 3 θ (cx,cy) (x,y) r x = cx + r * sin(θ) y = cy - r * con(θ) Example:DisplayClock.jav
您可能关注的文档
- 9饮食文化9俄语语言国情学.ppt
- A Brief Outline of History of American Literature 美国文学史课件.ppt
- a city tour说课稿 英语公开课第一册lesson13.ppt
- A Friend for Life(一生的朋友) 如何做好初高中英语衔接课件.ppt
- A letter of complaint 实用写作.ppt
- A Clean, ...新ppt 高等教育出版社 美国文学选读课件.ppt
- A Sample Cover Letter 求职英语 Job-hunting English 教学课件.ppt
- A way to lead to more effective writing. 英语写作 课件.ppt
- A n' B cultures1-4 Britain and American culture 英美文化教学课件.ppt
- A-6、第四章 开天辟地大事变(baiban) 中国近现代史 教学课件.ppt
原创力文档

文档评论(0)