0% found this document useful (0 votes)
8 views4 pages

Graphics Programming with GFace Class

This document contains code examples for creating and manipulating graphical objects in Java. It includes classes for drawing a face with different features, displaying faces when the user clicks, tracking mouse location, and allowing the user to drag and recolor graphical objects. The examples demonstrate using graphics, events, and mouse/keyboard interactions.

Uploaded by

Nitish Pathak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Graphics Programming with GFace Class

This document contains code examples for creating and manipulating graphical objects in Java. It includes classes for drawing a face with different features, displaying faces when the user clicks, tracking mouse location, and allowing the user to drag and recolor graphical objects. The examples demonstrate using graphics, events, and mouse/keyboard interactions.

Uploaded by

Nitish Pathak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link]

com/

Mehran Sahami CS 106A

Graphics and Events Examples

Handout #22 October 17, 2007

File: [Link]

Based on examples by Eric Roberts.

/* * File: [Link] * This class implements a face as a GCompound. */ // Note: only need [Link] since this is not // actually a program, but just a class using graphics. import [Link].*; 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(); [Link](0, -height / 2); [Link](width / 2, height / 2); [Link](-width / 2, height / 2); return poly; } /* Private instance variables */ private GOval head; private GOval leftEye, rightEye; private GPolygon nose; private GRect mouth;

[Link]
2

File: [Link]

/* * File: [Link] * ------------------* This program draws a GFace in the middle of the screen. */ import [Link].*; import [Link].*; 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: [Link]

/* * File: [Link] * ----------------------* This program displays a face in every location the user * clicks on. It is an example of an event-driven program. */ import [Link].*; import [Link].*; import [Link].*; 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 starts public void init() { // Must call this method to be able to get mouse events addMouseListeners(); } // This method is called everytime user clicks mouse public void mouseClicked(MouseEvent e) { GFace face = new GFace(FACE_DIAM, FACE_DIAM); add(face, [Link](), [Link]()); }

[Link]
3

File: [Link]

/* * File: [Link] * ----------------------* This program displays the (x, y) location of the mouse. */ import [Link].*; import [Link].*; import [Link].*; public class MouseTracker extends GraphicsProgram { public void run() { label = new GLabel(""); [Link]("Times New Roman-36"); add(label, 50, 50); // Must call this method to be able to get mouse events addMouseListeners();

// This method is called everytime user moves mouse public void mouseMoved(MouseEvent e) { [Link]("Mouse: (" + [Link]() + ", " + [Link]() + ")"); } /* Private instance variable */ private GLabel label;

[Link]
4

File: [Link]

/* * File: [Link] * ---------------------* 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. */ import import import import import [Link].*; [Link].*; [Link].*; [Link].*; [Link].*;

/** This class displays a mouse-draggable rectangle and oval */ public class DragObjects extends GraphicsProgram { // Initializes the program public void init() { GRect rect = new GRect(100, 100, 150, 100); [Link](true); add(rect); GOval oval = new GOval(50, 50, 150, 100); [Link](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 coordinate last = new GPoint([Link]()); gobj = getElementAt(last); } // Called on mouse drag to reposition the object public void mouseDragged(MouseEvent e) { if (gobj != null) { [Link]([Link]() - [Link](), [Link]() - [Link]()); last = new GPoint([Link]()); } } // Change color of last object dragged public void keyTyped(KeyEvent e) { if (gobj != null) { [Link]([Link]()); } } /* Private instance variables */ private GObject gobj; /* The object being dragged */ private GPoint last; /* The last mouse position */ private RandomGenerator rgen = [Link]();

You might also like