《斯坦福大学开放课程:编程方法》讲义#22.doc_第1页
《斯坦福大学开放课程:编程方法》讲义#22.doc_第2页
《斯坦福大学开放课程:编程方法》讲义#22.doc_第3页
《斯坦福大学开放课程:编程方法》讲义#22.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Mehran SahamiCS 106AHandout #22October 17, 2007Graphics and Events ExamplesBased on examples by Eric Roberts.File: GFace.java/* File: GFace.java* This class implements a face as a GCompound.*/ Note: only need acm.graphics since this is not/ actually a program, but just a class using graphics.import acm.graphics.*;public class GFace extends GCompound /* Constants specifying feature size as a fraction of head size */private static final double EYE_WIDTH = 0.15;private static final double EYE_HEIGHT = 0.15;private static final double NOSE_WIDTH = 0.15;private static final double NOSE_HEIGHT = 0.10;private static final double MOUTH_WIDTH = 0.50;private static final double MOUTH_HEIGHT = 0.03;/* Creates a new GFace object with the specified dimensions */public GFace(double width, double height) head = new GOval(width, height);leftEye = new GOval(EYE_WIDTH * width, EYE_HEIGHT * height);rightEye = new GOval(EYE_WIDTH * width, EYE_HEIGHT * height);nose = createNose(NOSE_WIDTH * width, NOSE_HEIGHT * height);mouth = new GRect(MOUTH_WIDTH * width, MOUTH_HEIGHT * height);add(head, 0, 0);add(leftEye, 0.25 * width - EYE_WIDTH * width / 2,0.25 * height - EYE_HEIGHT * height / 2);add(rightEye, 0.75 * width - EYE_WIDTH * width / 2,0.25 * height - EYE_HEIGHT * height / 2);add(nose, 0.50 * width, 0.50 * height);add(mouth, 0.50 * width - MOUTH_WIDTH * width / 2,0.75 * height - MOUTH_HEIGHT * height / 2);/* Creates a triangle for the nose */private GPolygon createNose(double width, double height) GPolygon poly = new GPolygon();poly.addVertex(0, -height / 2);poly.addVertex(width / 2, height / 2);poly.addVertex(-width / 2, height / 2);return poly;/* Private instance variables */private GOval head;private GOval leftEye, rightEye;private GPolygon nose;private GRect mouth; 2 File: DrawFace.java/* File: DrawFace.java* -* This program draws a GFace in the middle of the screen.*/import gram.*;import acm.graphics.*;public class DrawFace extends GraphicsProgram /* Width of face */private static final int FACE_WIDTH = 100;/* Height of face */private static final int FACE_HEIGHT = 200;public void run() GFace face = new GFace(FACE_WIDTH, FACE_HEIGHT);add(face, (getWidth() - FACE_WIDTH) / 2,(getHeight() - FACE_HEIGHT) / 2);File: ClickForFace.java/* File: ClickForFace.java* -* This program displays a face in every location the user* clicks on. It is an example of an event-driven program.*/import gram.*;import acm.graphics.*;import java.awt.event.*;public class ClickForFace extends GraphicsProgram /* Private constants */private static final double FACE_DIAM = 30;/ Note: no run() method is this program/ init() method is called when program startspublic void init() / Must call this method to be able to get mouse eventsaddMouseListeners();/ This method is called everytime user clicks mousepublic void mouseClicked(MouseEvent e) GFace face = new GFace(FACE_DIAM, FACE_DIAM);add(face, e.getX(), e.getY(); 3 File: MouseTracker.java/* File: MouseTracker.java* -* This program displays the (x, y) location of the mouse.*/import gram.*;import acm.graphics.*;import java.awt.event.*;public class MouseTracker extends GraphicsProgram public void run() label = new GLabel();label.setFont(Times New Roman-36);add(label, 50, 50);/ Must call this method to be able to get mouse eventsaddMouseListeners();/ This method is called everytime user moves mousepublic void mouseMoved(MouseEvent e) label.setLabel(Mouse: ( + e.getX() + , + e.getY() + );/* Private instance variable */private GLabel label; 4 File: DragObjects.java/* File: DragObjects.java* -* Example program to show mouse and keyboard interactions.* This program allows us to move objects on the screen* by dragging then with the mouse. We can also change the* color of the last object moved to a random color by typing a key.*/importimportimportimportimportacm.graphics.*;gram.*;acm.util.*;java.awt.*;java.awt.event.*;/* This class displays a mouse-draggable rectangle and oval */public class DragObjects extends GraphicsProgram / Initializes the programpublic void init() GRect rect = new GRect(100, 100, 150, 100);rect.setFilled(true);add(rect);GOval oval = new GOval(50, 50, 150, 100);oval.setFilled(true);add(oval);addMouseListeners();addKeyListeners();/ Called on mouse press to record the coordinates of the click */public void mousePressed(MouseEvent e) / GPoint has X and Y coordinatelast = new GPoint(e.getPoint();gobj = getElementAt(last);/ Called on mouse drag to reposition the objectpublic void mouseDragged(MouseEvent e) if (gobj != null) gobj.move(e.getX() - last.getX(), e.getY() - last.getY();last = new GPoint(e.getPoint();/ Change color of last object draggedpublic void keyTyped(KeyEvent

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论