Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

2 Agenda Announcements – Cell phones / laptops off & away / Name signs out Last time –null –this (revisited) Today –interfaces and realization –type hierarchy –introduction to graphics and event handling

3 Announcements Exam 2 –covers material from exam 1 up to & including 10/19 –review on Monday 10/22 –exam on Wednesday 10/24 No recitations in exam week: 10/22 – 10/26

4 Interfaces and Realization the function of an interface the form of an interface definition the realization (“implements”) relationship

5 Realization Realization is a relationship between a class and an interface. An interface contains method specifications, rather than full method definitions.

6 Implementation as contract A class which implements an interface is obligated to provide full definitions of all the methods specified in the interface.

7 Types When you define a class, you are defining a type. When you define an interface, you are also defining a type. A class which implements an interface is a SUBTYPE of the interface type.

8 Assignment If a variable is declared to be of an interface type, it can be assigned an instance of a subtype class.

9 Form of an interface header + body –header access control modifier keyword ‘interface’ name (generally an adjective, following class name conventions, but prefixed with an upper- case ‘ I ’) –body method specifications (method headers followed by ‘;’, also called method declarations, as opposed to method definition)

10 Interfaces – no instantiation While classes can be instantiated, interfaces cannot be instantiated.

11 Examples 1)Example from Java’s libraries (one detail omitted) public interface ActionListener { public void actionPerformed(ActionEvent e); } 2) Example from Java’s libraries (one detail omitted) public interface MenuKeyListener { void menuKeyTyped(MenuKeyEvent e); void menuKeyPressed(MenuKeyEvent e); void menuKeyReleased(MenuKeyEvent e); }

12 Implementation A class can implement an interface: public class EventHandler implements ActionListener {... }

13 public class EventHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(“Button clicked”); } Examples

14 ActionListener x; x = new ActionListener(); NO! Interfaces cannot be instantiated. x = new EventHandler(); OK, because EventHandler implements ActionListener. x = new FooBar(); OK, if FooBar implements ActionListener.

15 The point? Subtypes of a common supertype can be treated uniformly when treated as members of the supertype: see next slide.

16 public class EventHandler implements ActionListener { private String _name; public EventHandler(String s) { _name = s; } public void actionPerformed(ActionEvent e) { System.out.println(“Button clicked”); } public String getName() { return _name; } Examples

17 Calling methods ActionListener x; x = new EventHandler(“Fred”); x.actionPerformed(...); x.getName(); NO! Not all ActionListener objects define this method!

18 Miscellaneous things public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions pane) imports –Allow us to use, with unqualified names, things defined in other packages comments – ignored by the compiler –block comments /* … */ –single-line comments // –javadoc comments and tags (?) /** … */ String class and String literals (e.g. “Fred”) true as a boolean literal (for setVisible method call)

19 Using the Java graphics classes In this slide set we will explain the basics of how to create graphical programs. Some advanced issues will be glossed over (e.g. thread safety, graphics library design). In CSE116 we will revisit these and other more advanced topics.

20 Graphical elements There are two basic types of graphical elements: –Containers able to hold graphical objects, such as containers and components –Components must be put into containers able to generate events when manipulated

21 Containers Top-level containers –some containers are called “top-level” because they do not need to be place inside any other containers –javax.swing.JFrame is an example (JDialog and JApplet are others) Other containers (not top-level) –most containers must be placed inside some other container –javax.swing.JPanel is an example

22 Adding elements to a JFrame Top-level containers have multiple panes Content pane is the one which holds components With javax.swing.JFrame, two ways: –call getContentPane() on frame to get frame’s content pane, then call add(…) on content pane to add a component –call add(…) directly on the JFrame object Second approach is just a convenience method, does the same thing the first approach

23 Example Creating just a frame –new javax.swing.JFrame() Creating a frame with a title –new javax.swing.JFrame(“My title”) Making the frame visible –call setVisible(true) on the frame Making application close when window is closed: –call setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) on the frame See code from lecture (in SwingExamples project)

24 Another component: a JButton A JButton is a component which is typically set up to react to clicks.

25 Events Clicks on buttons, mouse movements, etc. are all considered events. A program can react to events by setting up event handlers. An event handler defines what should happen when a particular event occurs.

26 Event handling – 1 The component which gives rise to an event is decoupled from the part of the code that handles the event. This is called the observer pattern. General form: –www.research.ibm.com/designpatterns/example.htmwww.research.ibm.com/designpatterns/example.htm

27 Event handling – 2 Observer pattern in Java –An observer is called a listener in Java –Button clicks are “ActionEvents”. –Handlers for ActionEvents are ActionListeners. –An event-generator can have many listeners –Use “addActionListener” method to register a listener with a component


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 1."

Similar presentations


Ads by Google