Chapter 1: Computer Systems Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. Focus of the Course Object-Oriented Software Development • problem solving • program design, implementation, and testing • object-oriented concepts classes objects interfaces inheritance polymorphism • graphics and Graphical User Interfaces • the Java programming language 2 Introduction to Graphics The last one or two sections of each chapter of the textbook focus on graphical issues Most computer programs have graphical components A picture or drawing must be digitized for storage on a computer A picture consists of pixels, and each pixel is stored separately 3 Representing Color A black and white picture can be stored using one bit per pixel (0 = white and 1 = black) A colored picture requires more information; there are several techniques for representing colors For example, every color can be represented as a mixture of the three additive primary colors Red, Green, and Blue In Java, each color is represented by three numbers between 0 and 255 that collectively are called an RGB value 4 Coordinate Systems Each pixel can be identified using a two-dimensional coordinate system When referring to a pixel in a Java program, we use a coordinate system with the origin in the top-left corner (0, 0) 112 X 40 (112, 40) Y 5 Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. Objects and Primitive Data Now we can explore some more fundamental programming concepts Chapter 2 focuses on: • • • • • • • • predefined objects primitive data the declaration and use of variables expressions and operator precedence creating and using objects class libraries Java applets drawing shapes 7 Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) A Java applet is a program that is intended to be transported over the Web and executed using a web browser An applet also can be executed using the appletviewer tool of the Java Software Development Kit An applet doesn't have a main method Instead, there are several special methods that serve specific purposes 8 Applets The paint method, for instance, is executed automatically and is used to draw the applet’s contents The paint method accepts a parameter that is an object of the Graphics class A Graphics object defines a graphics context on which we can draw shapes and text The Graphics class has several methods for drawing shapes 9 Applets The class that defines an applet extends the Applet class This makes use of inheritance, which is explored in more detail in Chapter 7 See Einstein.java (page 109) An applet is embedded into an HTML file using a tag that references the bytecode file of the applet class The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser 10 The HTML applet Tag <html> <head> <title>The Einstein Applet</title> </head> <body> <applet code="Einstein.class" width=350 height=175> </applet> </body> </html> 11 Drawing Shapes Let's explore some of the methods of the Graphics class that draw shapes in more detail A shape can be filled or unfilled, depending on which method is invoked The method parameters specify coordinates and sizes Recall from Chapter 1 that the Java coordinate system has the origin in the top left corner Shapes with curves, like an oval, are usually drawn by specifying the shape’s bounding rectangle An arc can be thought of as a section of an oval 12 Drawing a Line 10 150 X 20 45 Y page.drawLine (10, 20, 150, 45); or page.drawLine (150, 45, 10, 20); 13 Drawing a Rectangle 50 X 20 40 100 Y page.drawRect (50, 20, 100, 40); 14 Drawing an Oval 175 X 20 80 bounding rectangle Y 50 page.drawOval (175, 20, 50, 80); 15 The Color Class A color is defined in a Java program using an object created from the Color class The Color class also contains several static predefined colors, including: Object RGB Value Color.black Color.blue Color.cyan Color.orange Color.white Color.yellow 0, 0, 0 0, 0, 255 0, 255, 255 255, 200, 0 255, 255, 255 255, 255, 0 16 The Color Class Every drawing surface has a background color Every graphics context has a current foreground color Both can be set explicitly See Snowman.java (page115) 17 Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. Program Statements Now we will examine some other program statements Chapter 3 focuses on: • • • • • • program development stages the flow of control through a method decision-making statements expressions for making complex decisions repetition statements drawing with conditionals and loops 19 More Drawing Techniques Conditionals and loops can greatly enhance our ability to control graphics See Bullseye.java (page 189) See Boxes.java (page 191) See BarHeights.java (page 193) 20 Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: • • • • • • class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing method overloading method decomposition graphics-based objects 22 Applet Methods In previous examples we've used the paint method of the Applet class to draw on an applet The Applet class has several methods that are invoked automatically at certain points in an applet's life The init method, for instance, is executed only once when the applet is initially loaded The start and stop methods are called when the applet becomes active or inactive The Applet class also contains other methods that generally assist in applet processing 23 Graphical Objects Any object we define by writing a class can have graphical elements The object must simply obtain a graphics context (a Graphics object) in which to draw An applet can pass its graphics context to another object just as it can any other parameter See LineUp.java (page 257) See StickFigure.java (page 259) 24 Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. Enhancing Classes Now we can explore various aspects of classes and objects in more detail Chapter 5 focuses on: • • • • • • • • object references and aliases passing objects references as parameters the static modifier wrapper classes nested classes and inner classes interfaces dialog boxes GUI components, events, and listeners 26 Dialog Boxes A dialog box is a graphical window that pops up on top of any currently active window for the user The Swing API contains a class called JOptionPane that simplifies the creation and use of basic dialog boxes There are three categories of JOptionPane dialog boxes • A message dialog displays an output string • An input dialog presents a prompt and a single input text field • A confirm dialog presents the user with a simple yes-or-no question See EvenOdd.java (page 302) 27 The EvenOdd Program 28 Graphical User Interfaces A Graphical User Interface (GUI) is created with at least three kinds of objects • components • events • listeners A GUI component defines a screen element to display information or allow the user to interact with the program • buttons, text fields, labels, menus, etc. A container is a special component that holds and organizes other components • dialog boxes, applets, frames, panels, etc. 29 Events An event is an object that represents some activity to which we may want to respond For example, we may want our program to perform some action when the following occurs: • • • • • • the mouse is moved a mouse button is clicked the mouse is dragged a graphical button is clicked a keyboard key is pressed a timer expires Events often correspond to user actions, but not always 30 Events and Listeners The Java standard class library contains several classes that represent typical events Components, such as an applet or a graphical button, generate (fire) an event when it occurs Other objects, called listeners, wait for events to occur We can write listener objects to do whatever we want when an event occurs A listener object is often defined using an inner class 31 Events and Listeners Event Component Listener This object may generate an event This object waits for and responds to an event When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event 32 Listener Interfaces We can create a listener object by writing a class that implements a particular listener interface The Java standard class library contains several interfaces that correspond to particular event categories For example, the MouseListener interface contains methods that correspond to mouse events After creating the listener, we add the listener to the component that might generate the event to set up a formal relationship between the generator and listener 33 Creating GUIs To create a program with a GUI: • define and set up the components • create listener objects • set up the relationships between the listeners and the components which generate events of interest • define what happens in response to each event A push button is a component that allows the user to initiate an action with the press of the mouse button • defined by the JButton class • generates an action event A label is a component that displays a line of text (or an image, or both) • defined by the JLabel class 34 Creating GUIs The init method of an applet can be used to set up the GUI and add each component to the applet container The Swing version of the Applet class is called JApplet In a JApplet, components are added to the applet's content pane The content pane is retrieved using the getContentPane method A JButton generates an action event See PushCounter.java (page 305) 35 The PushCounter Program 36 Action Listeners The interface corresponding to an action event is called ActionListener, which defines only one method, called actionPerformed The ButtonListener inner class implements the ActionListener interface in the PushButton program When the button is pushed, the JButton object invokes the actionPerformed method, passing it an ActionEvent The listener method may or may not make use of the event object passed to it 37 GUI Applications A frame is a container component used for stand-alone GUI-based applications A panel is a container, but unlike a frame, it cannot be displayed on its own • it must be added to another container • it helps organize the components in a GUI See Fahrenheit.java (page 309) See FahrenheitGUI.java (page 310) 38 The Fahrenheit Program 39 Chapter 6: Arrays Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. Arrays Arrays are objects that help us organize large amounts of information Chapter 6 focuses on: • • • • • • • • array declaration and use passing arrays and array elements as parameters arrays of objects sorting elements in an array multidimensional arrays the ArrayList class polygons and polylines more button components 41 Polygons and Polylines Arrays often are helpful in graphics processing Polygons and polylines are shapes that can be defined by values stored in arrays A polyline is similar to a polygon except that its endpoints do not meet, and it cannot be filled See Rocket.java (page 360) 42 The Rocket Program 43 The Polygon Class The Polygon class, defined in the java.awt package can be used to define and draw a polygon Two versions of the overloaded drawPolygon and fillPolygon methods each take a single Polygon object as a parameter A Polygon object encapsulates the coordinates of the polygon 44 Check Boxes A check box is a button that can be toggled on or off A check box is represented by the JCheckBox class A change of state generates an item event The ItemListener interface corresponds to item events The itemStateChanged method of the listener responds when a check box changes state 45 The StyleOptions Program A frame is a container that can be used to create standalone GUI applications A frame is represented by the JFrame class A Font object represents by the font's: • family name (such as Times or Courier) • style (bold, italic, or both) • font size See StyleOptions.java (page 364) See StyleGUI.java (page 365) 46 The StyleOptions Program 47 Radio Buttons A set of radio buttons represents a set of mutually exclusive options When a radio button from a group is selected, the other button currently "on" in the group is toggled off A radio button generates an action event See QuoteOptions.java (page 368) See QuoteGUI.java (page 369) 48 The QuoteOptions Program 49 Inheritance and GUIs An applet is an excellent example of inheritance Recall that when we define an applet, we extend the Applet class or the JApplet class The Applet and JApplet classes already handle all the details about applet creation and execution, including: • interaction with a Web browser • accepting applet parameters through HTML • enforcing security restrictions 50 Inheritance and GUIs Our applet classes only have to deal with issues that specifically relate to what our particular applet will do When we define the paint method of an applet, for instance, we are actually overriding a method defined in the Component class, which is ultimately inherited into the Applet or JApplet class 51 The Component Class Hierarchy The Java classes that define GUI components are part of a class hierarchy Swing GUI components typically are derived from the JComponent class which is derived from the Container class which is derived from the Component class Many Swing components can serve as (limited) containers, because they are derived from the Container class 52 Mouse Events Events related to the mouse are separated into mouse events and mouse motion events Mouse Events: • mouse pressed – the mouse button is pressed down • mouse released – the mouse button is released • mouse clicked – the mouse button is pressed down and released without moving the mouse in between • mouse entered – the mouse pointer is moved onto (over) a component • mouse exited – the mouse pointer is moved off of a component 53 Mouse Events Mouse Motion Events: • mouse moved – the mouse is moved • mouse dragged – the mouse is dragged To satisfy the implementation of a listener interface, empty methods must be provided for unused events An ArrayList object is used to store objects so they can be redrawn as necessary See Dots.java (page 427) See DotsPanel.java (page 428) 54 The Dots Program 55 Mouse Events Each time the repaint method is called on an applet, the window is cleared prior to calling paint Rubberbanding is the visual effect caused by "stretching" a shape as it is drawn using the mouse See RubberLines.java (page 431) See RubberLinesPanel.java (page 432) 56 The RubberLines Program 57 Event Adapter Classes Listener classes can be created by implementing a particular interface (such as MouseListener interface) A listener also can be created by extending an event adapter class Each listener interface has a corresponding adapter class (such as the MouseAdapter class) Each adapter class implements the corresponding listener and provides empty method definitions 58 Event Adapter Classes When we derive a listener class from an adapter class, we override any event methods of interest (such as the mouseClicked method) Empty definitions for unused event methods need not be provided See OffCenter.java (page 435) See OffCenterPanel.java (page 437) 59 The OffCenter Program 60