mat 2170
play

Mat 2170 Week2 Chapter Two: Programming by Example Graphics - PowerPoint PPT Presentation

Mat 2170 Chapter Two: Programming by Example Java Programming Mat 2170 Week2 Chapter Two: Programming by Example Graphics Messages Coordinates Java Programming Hierarchy Colors GLabel Spring 2014 Methods Programs Patterns Classes


  1. Mat 2170 Chapter Two: Programming by Example Java Programming Mat 2170 Week2 Chapter Two: Programming by Example Graphics Messages Coordinates Java Programming Hierarchy Colors GLabel Spring 2014 Methods Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  2. Student Responsibilities Mat 2170 Chapter Two: Programming by Example Reading: Textbook, Chapter 2 Java Programming Assignments Week2 1 Lab 1 : Rose Poem printout, web & sub-mit due at Graphics beginning of lab 2 on Thursday Messages 2 Worksheet 2: Due at beginning of Lab 2 on Thursday Coordinates Hierarchy 3 Lab 2: Due at beginning of Lab 3 next week Colors GLabel Attendance Methods Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  3. Chapter Two Overview Mat 2170 Chapter Two: Programming by Example Programming by Example Java Programming 2.6 Graphical programs (used in Lab 2) Week2 2.1 Parts of a program Graphics Messages 2.2 Programming Perspectives Coordinates Hierarchy 2.3 Add2Integers Colors GLabel 2.4 Programming idioms and patterns Methods Programs 2.5 Classes and objects Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  4. 2.6 Graphical Programs Mat 2170 Chapter Two: Programming by Example Java The GraphicsProgram class makes it possible to create Programming simple pictures on the screen. Week2 Graphics The conceptual model is that of a collage composed of Messages objects on a canvas or a felt board. Coordinates Hierarchy Colors Running a GraphicsProgram creates a window that serves GLabel as the background canvas for the collage. Methods Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  5. Mat 2170 Chapter Two: Programming by Example You cause a picture to appear by creating graphical objects Java Programming of various kinds, and then adding those objects to the canvas. Week2 Graphics Messages We will be learning how to work with labels (textual Coordinates graphics), rectangles, ovals, and lines using the classes Hierarchy GLabel , GRect , GOval , and GLine . Colors GLabel Methods The complete set of graphics classes is discussed in Chap. 9. Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  6. GLabel Objects — Unnamed vs Named In One Step: Mat 2170 Chapter Two: Create and Send Directly to Graphics Window: Programming by Example Java public void run() Programming { add(new GLabel("Hello, World!", 100, 75)); Week2 } Graphics Messages In Two Steps: Coordinates Declare a Named Object (–MyLabel–) of type Hierarchy GLabel , which is then sent to Graphics Window: Colors GLabel public void run() Methods { Programs GLabel MyLabel = new GLabel("Hello, World!", 100, 75); Patterns add(MyLabel); Classes } Java Programming Mat 2170 Chapter Two: Programming by Example

  7. Sending Messages to Objects Mat 2170 Chapter Two: We may wish to change the appearance (color) or location Programming by Example (position) of a graphical object after it’s been created. Java Programming In object–oriented languages such as Java, these changes Week2 are the responsibility of the object . Graphics Messages Coordinates Thus, to change the color of an object, you send a message Hierarchy to it telling it to change color. Colors GLabel Methods At this point in the semester, in order to send a message Programs to an object, it must have been declared with a name Patterns — as the GLabel MyLabel was on the last slide. Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  8. Sending Messages to Objects Mat 2170 Chapter Two: Programming by Example To send a message to an object, Java uses the following Java syntax: Programming receiver.methodName(arguments); Week2 Graphics where: Messages Coordinates receiver is the (named) object to which the message is Hierarchy directed Colors methodName identifies which message is sent GLabel Methods arguments is a list of values used to specify any other Programs information associated with the message Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  9. Sending Messages to a GLabel Mat 2170 This program illustrates sending a message to an object. Note Chapter Two: Programming that the label doesn’t appear until it is added to the canvas. by Example Java Programming public class HelloProgram extends GraphicsProgram { Week2 public void run() Graphics { Messages GLabel MyLabel = new GLabel("Hello",100,75); Coordinates MyLabel.setFont("SansSerif-36"); Hierarchy MyLabel.setColor(Color.RED); Colors add(MyLabel); GLabel } Methods } Programs Patterns MyLabel contents: “Hello”, 100, 75, SansSerif-36, red Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  10. The Java Coordinate System Mat 2170 Chapter Two: Programming by Example Java Programming Positions and distances in a graphics program are measured in terms of pixels , which are the individual dots that cover Week2 the screen. Graphics Messages Coordinates Unlike traditional mathematics, Java defines the origin of Hierarchy the graphics coordinate system to be the upper left corner Colors of the window. GLabel Methods Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  11. Mat 2170 Chapter Two: Programming by Example Java Values for the x coordinate increase from left to right. Programming Week2 Values for the y coordinate increase from top to bottom . Graphics Messages Coordinates Creating a GLabel at a particular x and y position means Hierarchy that the baseline of the first character in the label appears Colors at that point — i.e., the ( x , y ) coordinate is for the lower GLabel left corner of the label. Methods Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  12. The GObject Hierarchy The classes that represent graphical objects form a hierarchy, Mat 2170 Chapter Two: part of which looks like this: Programming by Example Java GObject Programming Week2 Graphics GLabel GRect GOval GLine Messages Coordinates Hierarchy Operations are defined at each level of the hierarchy. Colors Operations that apply to all graphical objects are specified GLabel at the GObject level — where they are inherited by each Methods subclass. Programs Patterns Operations that apply to a particular subclass are specified Classes as part of the definition of that class. Java Programming Mat 2170 Chapter Two: Programming by Example

  13. Operations on the GObject Class Mat 2170 The following operations apply to all GObject s Chapter Two: Programming by Example Java Sets the color of the object to Programming object. setColor (color) the specified color constant Week2 (default is BLACK ) Graphics Messages Changes the location of the Coordinates object. setLocation (x, y) object to the point (x, y) Hierarchy Colors Moves the object on the GLabel object. move (dx, dy) screen by adding the dis- Methods placements dx and dy to its Programs current coordinates Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  14. The java.awt Package Standard Color Names Mat 2170 Chapter Two: Color.BLACK Color.RED Programming by Example Color.DARK GRAY Color.YELLOW Java Programming Color.GRAY Color.GREEN Color.LIGHT GRAY Color.CYAN Week2 Graphics Color.WHITE Color.BLUE Messages Color.MAGENTA Color.ORANGE Coordinates Color.PINK Hierarchy Colors GLabel Methods In order to use these colors, you will need to add: Programs import java.awt.*; Patterns to your program. Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  15. Operations on the GLabel Class Mat 2170 Chapter Two: Programming by Example Java Programming Constructor : Week2 Graphics new GLabel(text, x, y) Messages Creates a label containing the specified text that begins Coordinates at the point (x, y). Hierarchy Colors GLabel Methods Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  16. GLabel Example Mat 2170 Chapter Two: Programming by Example Java Programming public void run() Week2 { Graphics GLabel msg = new GLabel("Hello, World!", 100, 75); Messages Coordinates add(msg); Hierarchy } Colors GLabel Methods Programs Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

  17. Creating Geometric Objects Mat 2170 Chapter Two: Constructors : Programming by Example Java new GRect(x, y, width, height) Programming Creates a rectangle whose upper left corner is at (x, y ) Week2 of the specified size. Graphics Messages Coordinates new GOval(x, y, width, height) Hierarchy Creates an oval that fits inside the rectangle with the Colors same dimensions. GLabel Methods new GLine(x0, y0, x1, y1) Programs Creates a line extending from (x0, y0) to (x1, y1). Patterns Classes Java Programming Mat 2170 Chapter Two: Programming by Example

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend