Presentation is loading. Please wait.

Presentation is loading. Please wait.

© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.

Similar presentations


Presentation on theme: "© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs."— Presentation transcript:

1 © The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs

2 © The McGraw-Hill Companies, 2006 The Swing package in the earlier versions of Java, graphical programming was achieved exclusively by making use of a package known as the Abstract Window Toolkit (AWT).; nowadays, graphics in Java is achieved via the Swing package; the Swing classes build on the AWT classes to provide enhanced functionality and appearance;

3 © The McGraw-Hill Companies, 2006 The SmileyFace class

4 © The McGraw-Hill Companies, 2006 Analysis of the SmileyFace class The import clauses the first of these imports the standard Java Abstract Window Toolkit; although we are going to be using Swing, we still need many of the AWT classes for drawing and painting; the next clause imports the Swing package - the Swing classes come in a library called Javax (Java eXtension).

5 © The McGraw-Hill Companies, 2006 The class header: a JFrame is a Swing component that forms the visible window in which the graphic is displayed; by extending the JFrame class, we are making our SmileyFace class a kind of JFrame. The constructor

6 © The McGraw-Hill Companies, 2006 The paint method this special method is a method of a basic graphics class called Component, of which JFrame is an extension; we don't need to call this method anywhere because when the component becomes visible, the paint method is automatically called; when this happens, an object of a core Java class called Graphics (which comes with the AWT package) is automatically sent in to this method.

7 © The McGraw-Hill Companies, 2006 a Graphics object many useful methods. the drawOval method takes four integer parameters; referring to these as x, y, l, h, the oval that gets drawn fits into an imaginary rectangle that starts at position (x,y), and is l pixels long and h pixels high: because we want a circle, we have made the values of l and h equal.

8 © The McGraw-Hill Companies, 2006 the next line draws the mouth: this method requires six parameters, all integers - we shall call them x, y, l, h, , . the first four define an imaginary rectangle as above; the arc is drawn so that its centre is the centre of this rectangle, as shown below; the next two parameters,  and , represent angles; the first, , is the start angle – measured from an imaginary horizontal line pointing to the "quarter-past-three" position (representing zero degrees); the next, , is the finish angle; if  is positive then the arc is drawn by rotating from the start position in an anti-clockwise direction; if it is negative we rotate in a clockwise direction.

9 © The McGraw-Hill Companies, 2006

10 Event-handling in Java : The ChangingFace class we are going to change our SmileyFace class into a ChangingFace class that can smile or frown on the click of a button:

11 © The McGraw-Hill Companies, 2006 Analysis of the ChangingFace class the buttons are objects of the Swing component JButton; they are declared as attributes of the class, and initialized at the same time: as can be seen the JButton class has a constructor that allows us to create the buttons with the required caption by sending in this caption as a parameter.

12 © The McGraw-Hill Companies, 2006 The constructor of the ChangingFace class after setting the title, the buttons are added to the frame; a container, like the ChangingFace (which extends JFrame), always has a layout policy attached to it; this policy determines the way in which components are added to it; the default policy for a JFrame is called BorderLayout, which will be explained later; what we want to use a policy called FlowLayout, which means: –the components are just placed in the order in which they were added; –when one row fills up then the next row starts to be filled. we have changed the layout policy with this instruction:

13 © The McGraw-Hill Companies, 2006 Event-handling note the additional import statement: the event package contains all the classes (such as ActionListener below) that we need for handling events; The class header the ActionListener class is a special class called an interface; these classes contain only abstract methods which; ActionListener insists that we code a method called actionPerformed to handle our events; implementing an interface is very like extending a class; any class that implements ActionListioner must have an actionPerformed method.

14 © The McGraw-Hill Companies, 2006 note the new attribute: this will be set to true when the Smile button is pressed and false when the Frown button is pressed; the face will then be repainted with the appropriate expression; we have initialized it to true, so that the face will start off happy.

15 © The McGraw-Hill Companies, 2006 consider these two lines of the constructor when we use the keyword implements with an interface class like ActionListener we achieve an effect very similar to that achieved by inheritance; the JButton class has a method called addActionListener that receives an object of the ActionListener class as a parameter; since our class is now a kind of ActionListener we can send it to the addActionListener method of a JButton; the ActionListener that we send in is the class where the code for handling the event is to be found; by adding an ActionListener to an object such as a button, we are making that object "listen" for a mouse-click; the program will then respond to this event - the mouse-click - by taking some action.

16 © The McGraw-Hill Companies, 2006 The paint method we begin by calling the paint method of the superclass, JFrame; after drawing the face and the eyes as before we have two possibilities for the mouth: –if isHappy is true, a smiling mouth is drawn; –if not a frowning mouth is drawn. by changing the very last parameter of the drawArc method from a negative value to a positive value the arc will be drawn anti-clockwise instead of clockwise; this will make the mouth frown instead of smile; therefore if the isHappy attribute is set to true the mouth will smile – if not it will frown! note that we have moved everything down in order to accommodate the buttons.

17 © The McGraw-Hill Companies, 2006 The event-handler

18 © The McGraw-Hill Companies, 2006 we determine what happens when the mouse-button is clicked by coding the actionPerformed method that is required by the ActionListener interface; when the mouse is clicked, this method is automatically sent an object of the class ActionEvent; this class has a method called getSource that returns the name of the object that was clicked on; we use this method in the condition of the if statement to find out which button was clicked; if it was the happyButton that was pressed, isHappy is set to true and a special method – repaint – is called; this causes the paint method to be called again so that the screen is repainted; the sadButton works in the same way, but sets isHappy to false.

19 © The McGraw-Hill Companies, 2006 An interactive graphics class the class below (PushMe) allows the user to input information via a graphics screen; it allows the user to enter some text and then, by clicking on a button, to see the text that was entered displayed in the graphics window:

20 © The McGraw-Hill Companies, 2006 Analysis of the PushMe class there are three components involved here; we have declared them all as attributes of the class and initialized them at the same time: the first of the above three components is a JTextField; we have used the fact that it has a constructor that accepts an integer value (in this case 15) that allows you to specify the length of the text field to be displayed (in terms of columns). next we declare the JButton, which, as before, we have initialized with the required caption ("please push me"); finally we have declared and instantiated a JLabel.

21 © The McGraw-Hill Companies, 2006 The constructor

22 © The McGraw-Hill Companies, 2006 The event-handling routine we use the getText method of the JTextField class to read the current "value" of the text in myTextField; we then use the setText method of the JLabel class to "transfer" it to myLabel; notice that since there is only one component that is able to listen, it is not necessary to determine the source of the mouse-click.

23 © The McGraw-Hill Companies, 2006 A graphical user interface (GUI) for the Oblong class we are now in a position to write graphical user interfaces for our classes; the example below does this for the Oblong class.

24 © The McGraw-Hill Companies, 2006 Analysis of the OblongGui class the first attribute is an Oblong object, myOblong; this is initialized as a new Oblong with a length and height of zero (since the user hasn't entered anything yet): after this we declare the graphics components; the only new one the JTextArea, which is the large text area where the area and perimeter of the oblong are displayed it has a constructor that allows you to fix the size by entering values for the number of rows and columns.

25 © The McGraw-Hill Companies, 2006 The event-handling routine for the calcButton the two local variables, lengthEntered and heightEntered, will hold the values entered by the user; these values are read (as Strings) using the getText method of TextField; we check that these are not of length zero (that is, that something has been entered) ; if one of the fields is empty we display an error message; otherwise, we use the setLength and setHeight methods of Oblong to set the length and the height of myOblong to the values entered. these methods expect to receive doubles so we must convert from Strings; to do this we use the parseDouble method of the Double class:

26 © The McGraw-Hill Companies, 2006 had we wanted to convert them to ints, we would have used the parseInt method of the Integer class. if you want to do this the other way round and convert a double or an int to a String you can do so simply by concatenating it onto an empty String. For example or: or even:

27 © The McGraw-Hill Companies, 2006 A metric converter

28 © The McGraw-Hill Companies, 2006 Layout policies The FlowLayout manager

29 © The McGraw-Hill Companies, 2006 The BorderLayout manager This is the default policy for JFrame;

30 © The McGraw-Hill Companies, 2006 the MetricConverter class uses a combination of BorderLayout and FlowLayout; some components (for example JPanel) have FlowLayout as their default layout policy and some (for example JFrame) have BorderLayout; if you want to change the policy of such a component - say myPanel - to BorderLayout you would do so as follows: the MetricConverter class uses a combination of BorderLayout and FlowLayout; some components (for example JPanel) have FlowLayout as their default layout policy and some (for example JFrame) have BorderLayout; if you want to change the policy of such a component - say myPanel - to BorderLayout you would do so as follows: the following line would then place a component called myButton in the North region: there are a number of other layout managers that can be used such as GridLayout and CardLayout; these will be introduced in the second semester.

31 © The McGraw-Hill Companies, 2006


Download ppt "© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs."

Similar presentations


Ads by Google