News! Feedback after Lecture 4! About Java and OOP!

Size: px
Start display at page:

Download "News! Feedback after Lecture 4! About Java and OOP!"

Transcription

1 this Example: SignalGUI News Orphans Recursive References D0010E Object-Oriented Programming and Design Lecture 5 GUI Design Patterns Applets Model-View- Control Observer Hints for Lab 2 Use a Vector to store rooms that have been placed in Assignment 3. Assignment 4: Add code to the driver that creates a GUI that displays your level. Look-up the keyword this. Use it for the openings in rooms. Include dynamic variables in the room class that store the dimension and the color of the floor. Lecture 5 - Håkan Jonsson 1 Lecture 5 - Håkan Jonsson 2 Feedback after Lecture 4 Can a static method, like main, in one class be called from another class? Yes. Use ClassName.methodName( ) Is code declared public accessible to everyone, like over the Internet? No. Access is regulated with respect to code fragments [in the same project in Eclipse], not the programmers or the hardware. So, public means accessible from code everywhere in whatever package - in the same project. But in Internet-connected computers running Unix, each code fragment can be viewed as being stored at a unique location; URL + place in the file system. About Java and OOP All dynamic objects (like rooms in Lab 2) have state (data stored in dynamic variables) and behavior (effect caused by dynamic methods). Their behavior is triggered by someone calling their methods. The code in one method in an object (its behavior ) calls a method in another object (thereby causing it to behave). The result of a program is recorded in the states of all its objects. The final global result is a compilation of, and a computation using, data from local states. Usually just some subset of data is considered as the final result. Indeed, all data are the result of computations using data from states. A recursive explanation Lecture 5 - Håkan Jonsson 3 Lecture 5 - Håkan Jonsson 4

2 Keyword: this At run-time, the keyword this refers to the dynamic object that runs the code that contains the word this at the particular moment when this is encountered. NB Not the class. It is used like a reference variable but can not be assigned a new value. It makes it possible for dynamic objects to refer to themselves. It is useful to, for instance, send the dynamic object itself as an argument in a method call done in one of its methods, and to Compare with self in python differ between local and dynamic variables with the same name. public class Names { public Names(String first, String last) { this.first = first; this.last = last; } public String fullname() { return first + + last; } private String first, last; } (Parameters; local, automatic, variables) (Dynamic variables) Lecture 5 - Håkan Jonsson 5 Lecture 5 - Håkan Jonsson 6 Orphans An orphan is a dynamic object that no longer is possible to reach via a reference chain from main(). (In fact, not just main() but from any running thread.) Problem: Orphans still occupy memory space that can not be used for other purposes. Memory leakage. In some programming languages the memory occupied by orphans must be reclaimed by code in the program. All the responsibility on the programmer. Failure to reclaim leads to memory leakage and bugs that are hard (and time-consuming) to find. Out of memory. Orphans In Java, orphans are reclaimed automatically, when needed. Garbage collection. Advantage: Memory leakage can not occur However, memory is still finite so don t waste it Drawback: Garbage collection takes time and can kick in when not expected. It is possible to control the garbage collection process, but this is outside the scope of this course (there s no need). However, garbage collection works sufficiently good in applications that are not very time-critical. Lecture 5 - Håkan Jonsson 7 Lecture 5 - Håkan Jonsson 8

3 Recursive References public class Stuff { public Stuff morestuff; public int thing; public Stuff (int thing) { morestuff = null; this.thing = thing; } } (The following is executed in a static object with access to Stuff.) Stuff stuff = new Stuff(10); stuff.morestuff = new Stuff(2); stuff.morestuff.thing = 4; public class Stuff { public Stuff morestuff; public int thing; public Stuff (int thing) { morestuff = null; this.thing = thing; } } stuff.morestuff.morestuff = new Stuff(7); stuff.morestuff.morestuff = stuff; Lecture 5 - Håkan Jonsson 9 Lecture 5 - Håkan Jonsson 10 Stuff stuff = new Stuff(10); stuff.morestuff = new Stuff(2); morestuff == null thing == 10 morestuff thing == 10 A second object is created and referred to from the first object. stuff An object is created. Stuff stuff = new Stuff(10); stuff.morestuff = new Stuff(2); stuff.morestuff.thing = 4; stuff.morestuff.morestuff = new Stuff(7); stuff.morestuff.morestuff = stuff; Lecture 5 - Håkan Jonsson 11 stuff morestuff == null thing == 2 Stuff stuff = new Stuff(10); stuff.morestuff = new Stuff(2); stuff.morestuff.thing = 4; stuff.morestuff.morestuff = new Stuff(7); stuff.morestuff.morestuff = stuff; Lecture 5 - Håkan Jonsson 12

4 stuff.morestuff.thing = 4; stuff.morestuff.morestuff = new Stuff(7); morestuff thing == 10 morestuff thing == 10 morestuff == null thing == 7 A third object is created and referred to from the second object. morestuff == null thing == 4 thing in the second object used to be 2 but is now assigned 4. morestuff thing == 4 stuff Stuff stuff = new Stuff(10); stuff.morestuff = new Stuff(2); stuff.morestuff.thing = 4; stuff.morestuff.morestuff = new Stuff(7); stuff.morestuff.morestuff = stuff; Lecture 5 - Håkan Jonsson 13 stuff Stuff stuff = new Stuff(10); stuff.morestuff = new Stuff(2); stuff.morestuff.thing = 4; stuff.morestuff.morestuff = new Stuff(7); stuff.morestuff.morestuff = stuff; Lecture 5 - Håkan Jonsson 14 stuff.morestuff.morestuff = stuff; morestuff thing == 10 stuff morestuff thing == 4 morestuff thing == 7 (An orphan) morestuff in the second object used to refer to the third object, but is now changed so it refers to the first object. This assignment orphans the third object. Stuff stuff = new Stuff(10); stuff.morestuff = new Stuff(2); stuff.morestuff.thing = 4; stuff.morestuff.morestuff = new Stuff(7); stuff.morestuff.morestuff = stuff; Lecture 5 - Håkan Jonsson 15 GUI:s in Java Everything on a computer screen has to be deliberately drawn. Text, geometric shapes, pictures, borders, scroll bars etc. Java provides means to make such drawings as Graphical User Interfaces (GUI:s) that consists of GUI components. With strange names like JPanel, JFrame and the like. Moreover, GUI:s in Java are event-driven. They react on events like mouse-clicks, keys pressed etc. When events occur, special methods are automatically called by the operating system. This might seem like magic but, no, that s not the case These methods are available for us to re-program, thereby making the program react in a suitable manner. We will soon look at two (2) examples to illustrate this. Lecture 5 - Håkan Jonsson 16

5 Again: Implementing" the Model-View-Control in Java 1) The model: Is observable. Automatically gets the methods setchange notifyobservers addobserver Must call " setchange notifyobservers in this order whenever it has changed. 2) The view: Is an observer. Must implement a method update The method update is called whenever the model calls setchange and notifyobservers. Should update the view. Must add itself as an observer of the model by calling" <model>.addobserver Again: Implementing" the Model-View in Java 3) The controller: Usually a listener for events. KeyListener, MouseListener, etc Automatically gets methods that are called when an event happens. A key is pressed, the mouse is moved, a mouse button is pressed etc. Different methods depending on the type of listener. Lecture 5 - Håkan Jonsson 17 Lecture 5 - Håkan Jonsson 18 Example: Counter This example shows a simple counter that can be changed by typing on the keyboard. The design is done using the design pattern Model-View-Control. The code is therefore split into three separate classes depending on function: Model View Control (In fact, two variants of controllers.) 5. I vyns update-metod ändrar vyn på sig för att överensstämma med det nya modelltillståndet. "Model" CounterView CounterModel "Control" CounterController "View" 4. Anropen i incdata resulterar i att update-metoden i (alla) observerare anropas. 3. incdata ökar räknaren (modellen ändrar sitt tillstånd) och anropar setchange samt notifyobservers. 2. I keypressed anropas incdata i modellen. (6. Baserat på den nya vyn kanske användaren trycker på ännu en knapp, och loopen sluts.) 1. På tangentbordet trycks "+" ned, vilket ger ett anrop till keypressed. Lecture 5 - Håkan Jonsson 19 Lecture 5 - Håkan Jonsson 20

6 2. Signals from the last lecture Our GUI (the view) is separate from the signal (the model). The model is represented by the class Signal. Holds the current state (on or off). The view is represented by the class SignalGUI. Keeps a drawing of the model up-to-date and reacts to user input (events). The controller has been included into the view. The model and the view/controller are both members of the package signal. This close relationship is beneficial since the view needs to be able to change the model (via methods the model provides) whenever the user requests this and access to the state of the model to be able draw a picture of the model after it has been change. SignalGUI Example of a GUI programmed in Java. 4 classes: Signal SignalGUI Driver A main program Separates the model from the view. The model is a signal. The view is a 2D picture with a colored rectangle in a pop-up window. Apart from model and view there is a third part called control; here it is bundled with the view. Lecture 5 - Håkan Jonsson 21 Lecture 5 - Håkan Jonsson 22 A GUI for Signals Lecture 5 - Håkan Jonsson 23 Lecture 5 - Håkan Jonsson 24

7 Again: GUITest package f5.ex1; public class GUITest { public static void main(string[] args) { Driver driver = new Driver(); driver.run(); } } Lecture 5 - Håkan Jonsson 25 Driver - NEW package f5.ex2; import f5.ex2.signal.signal; import f5.ex2.signal.signalgui; import f5.ex2.signal.signaltui; public class Driver { public void run() { // a signal s1 Signal s1 = new Signal(); // 1a. a graphical GUI for s1 SignalGUI sgs1a = new SignalGUI(s1, 100, 100); // 1b. a second graphical GUI for s1 // SignalGUI sgs1b = new SignalGUI(s1, 100, 100); // 2. a textual UI for s1 // SignalTUI stui = new SignalTUI(s1, 400, 100); // 3. another signal s2 // Signal s2 = new Signal(); // 4. a graphical GUI for s2 // SignalGUI sgs2 = new SignalGUI(s2, 100, 100) } } Lecture 5 - Håkan Jonsson 26 Again: Signal package f5.ex1.signal; import java.util.observable; // public class Signal extends Observable { public Signal() {} boolean on = false; boolean ison() { return on; } void seton() { on = true; setchanged(); // notifyobservers(); // } void setoff() { on = false; setchanged(); // notifyobservers(); // } package f5.ex1.signal; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.util.observable; import java.util.observer; import javax.swing.jframe; import javax.swing.jpanel; public class SignalGUI implements Observer { private Signal s; private Display d; public SignalGUI(Signal s, int x, int y) { this.s = s; JFrame frame = new JFrame("A Signal"); frame.setdefaultcloseoperation(jframe.exit_on_close); d = new Display(); frame.getcontentpane().add(d); frame.pack(); frame.setlocation(x, y); frame.setvisible(true); s.addobserver(this); Again: SignalGUI:1 } } Lecture 5 - Håkan Jonsson 27 Lecture 5 - Håkan Jonsson 28

8 public void update(observable obs, Object arg1) { d.repaint(); } private class Display extends JPanel { public Display() { addkeylistener(new Listener()); setbackground(color.white); setpreferredsize(new Dimension(200, 200)); setfocusable(true); } public void paintcomponent(graphics g) { super.paintcomponent(g); if (s.ison()) { g.setcolor(color.green); } else { g.setcolor(color.red); } g.fillrect(50, 50, 100, 100); g.setcolor(color.black); g.drawrect(50, 50, 100, 100); } Again: SignalGUI:2 } } private class Listener implements KeyListener { public void keypressed(keyevent arg0) {} } Again: SignalGUI:3 public void keyreleased(keyevent arg0) {} public void keytyped(keyevent event) { switch (event.getkeychar()) { case '1': s.seton(); break; case '2': s.setoff(); break; default: break; } } Lecture 5 - Håkan Jonsson 29 Lecture 5 - Håkan Jonsson 30 package f5.ex2.signal; import java.awt.textarea; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.util.observable; import java.util.observer; import javax.swing.jframe; import javax.swing.jscrollpane; public class SignalTUI implements Observer { public SignalTUI(Signal s, int x, int y) { this.s = s; s.addobserver(this); JFrame frame = new JFrame("Text UI for a Signal"); text = new TextArea(20,40); text.seteditable(false); text.addkeylistener(new Listener()); JScrollPane scrollp = new JScrollPane(text); frame.getcontentpane().add(scrollp); printstatus(); frame.pack(); frame.setlocation(x,y); frame.setvisible(true); } SignalTUI:1- NEW Lecture 5 - Håkan Jonsson 31 } public void update(observable obs, Object arg1) { printstatus(); } void printstatus() { if (s.ison()) { text.append("on\n"); } else { text.append("off\n"); } } private Signal s; private TextArea text; /* instead of a Display */ private class Listener implements KeyListener { public void keypressed(keyevent arg0) {} public void keyreleased(keyevent arg0) {} } public void keytyped(keyevent event) { switch (event.getkeychar()) { case '1' : s.seton(); break; case '2' : s.setoff(); break; default : break; } } SignalTUI:2 Lecture 5 - Håkan Jonsson 32

9 Applets Coding an Applet An applet is a kind of Java program that can be included on a web page. The code of the applet is fetched from the web server by the browser along with HTML-code that describes the web page. The applet is then started in the browser and its graphical output is shown in a dedicated rectangular region on the page. It is possible to program applets to interact with users but applets have very restricted access to the local host for security reasons. An applet is essentially a computer program that you download and run without inspecting the code You program an applet pretty much as a regular program with a GUI. However, since an applet runs as part of another program (the browser) no main-method is needed. Let s take a look at an example: AppletExample.java Here is also an example of a mouse listener. Lecture 5 - Håkan Jonsson 33 Lecture 5 - Håkan Jonsson 34 package f5.ex1; import java.awt.*; import java.awt.event.*; AppletExample:1 import javax.swing.*; public class AppletExample extends JApplet { public void init() { MyDisplay d = new MyDisplay(); d.addmouselistener(new MyListener(d)); getcontentpane().add(d); } class MyDisplay extends JPanel { public MyDisplay() { setbackground(color.black); setforeground(color.yellow); setfont(new Font("SansSerif", Font.BOLD, 14)); } } public void paintcomponent(graphics g) { Color c; super.paintcomponent(g); g.setcolor(color.yellow); if (pressed) { g.drawstring(" The mouse button IS being pressed", 10, 20); g.setcolor(color.green); g.fillrect(150,50,20,20); } else { g.drawstring("the mouse button is NOT being pressed", 10, 20); g.setcolor(color.red); g.filloval(150,50,20,20); } } void userpressed() { pressed = true; } void userreleased() { pressed = false; } private boolean pressed = false; AppletExample:2 Lecture 5 - Håkan Jonsson 35 Lecture 5 - Håkan Jonsson 36

10 } class MyListener implements MouseListener { private MyDisplay mydisplay; } public MyListener(MyDisplay md) { mydisplay = md; } public void mousepressed(mouseevent arg0) { mydisplay.userpressed(); mydisplay.repaint(); } public void mousereleased(mouseevent arg0) { mydisplay.userreleased(); mydisplay.repaint(); } AppletExample:3 public void mouseentered(mouseevent arg0) { /* not in use */ } public void mouseexited(mouseevent arg0) { /* not in use */ } public void mouseclicked(mouseevent arg0) { /* not in use */ } Lecture 5 - Håkan Jonsson 37

Lecture 5. Lecture

Lecture 5. Lecture this GUI Example: SignalGUI Orphans D0010E Object- Oriented Programming and Design Model- View- Control Example: Counter Observer Recursive References Design PaOerns - Håkan Jonsson 1 1 About dynamic objects

More information

G51PRG: Introduction to Programming Second semester Applets and graphics

G51PRG: Introduction to Programming Second semester Applets and graphics G51PRG: Introduction to Programming Second semester Applets and graphics Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous two lectures AWT and Swing Creating components and putting

More information

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in Lab 4 D0010E Object-Oriented Programming and Design Lecture 9 Lab 4: You will implement a game that can be played over the Internet. The networking part has already been written. Among other things, the

More information

Lecture 9. Lecture

Lecture 9. Lecture Layout Components MVC Design PaCern GUI Programming Observer Design PaCern D0010E Lecture 8 - Håkan Jonsson 1 Lecture 8 - Håkan Jonsson 2 Lecture 8 - Håkan Jonsson 3 1 1. GUI programming In the beginning,

More information

User interfaces and Swing

User interfaces and Swing User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends

More information

Lecture 4. Lecture

Lecture 4. Lecture Interfaces Classes Building blocks Methods Arrays Example: BitArray Packages D0010E Object- Oriented Programming and Design InformaGon hiding Graphics and interacgon Example: A blinking signal Access Modifiers

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Overview

More information

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is INDEX Event Handling. Key Event. Methods Of Key Event. Example Of Key Event. Mouse Event. Method Of Mouse Event. Mouse Motion Listener. Example of Mouse Event. Event Handling One of the key concept in

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014 2011 Extending JFrame Dialog boxes Overview Ge

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Ge?ng user input Overview Displaying message or error Listening for

More information

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.! True object-oriented programming: Dynamic Objects Reference Variables D0010E Object-Oriented Programming and Design Lecture 3 Static Object-Oriented Programming UML" knows-about Eckel: 30-31, 41-46, 107-111,

More information

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

Exam: Applet, Graphics, Events: Mouse, Key, and Focus Exam: Applet, Graphics, Events: Mouse, Key, and Focus Name Period A. Vocabulary: Complete the Answer(s) Column. Avoid ambiguous terms such as class, object, component, and container. Term(s) Question(s)

More information

Object-oriented programming in Java (2)

Object-oriented programming in Java (2) Programming Languages Week 13 Object-oriented programming in Java (2) College of Information Science and Engineering Ritsumeikan University plan last week intro to Java advantages and disadvantages language

More information

PESIT Bangalore South Campus

PESIT Bangalore South Campus INTERNAL ASSESSMENT TEST II Date : 20-09-2016 Max Marks: 50 Subject & Code: JAVA & J2EE (10IS752) Section : A & B Name of faculty: Sreenath M V Time : 8.30-10.00 AM Note: Answer all five questions 1) a)

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14 Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9,

More information

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline Sequential vs. Event-driven Programming Reacting to the user GUI Program Organization Let s digress briefly to examine the organization of our GUI programs We ll do this in stages, by examining three example

More information

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread G51PGP Programming Paradigms Lecture 008 Inner classes, anonymous classes, Swing worker thread 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals

More information

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each CS 120 Fall 2008 Practice Final Exam v1.0m Name: Model Solution True/False Section, 20 points: 10 true/false, 2 points each Multiple Choice Section, 32 points: 8 multiple choice, 4 points each Code Tracing

More information

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009 SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009 NAME (Printed) NAME (Signed) E-mail Exam Rules Show all your work. Your grade

More information

Lecture 3. Lecture

Lecture 3. Lecture True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design

More information

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing. COSC User Interfaces Module 3 Sequential vs. Event-driven Programming Example Programs DemoLargestConsole.java DemoLargestGUI.java Demo programs will be available on the course web page. GUI Program Organization

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners GUI (Graphic User Interface) Programming Part 2 (Chapter 8) Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons, text fields, and the

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 32 April 9, 2018 Swing I: Drawing and Event Handling Chapter 29 HW8: Spellchecker Available on the web site Due: Tuesday! Announcements Parsing, working

More information

GUI in Java TalentHome Solutions

GUI in Java TalentHome Solutions GUI in Java TalentHome Solutions AWT Stands for Abstract Window Toolkit API to develop GUI in java Has some predefined components Platform Dependent Heavy weight To use AWT, import java.awt.* Calculator

More information

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a CBOP3203 An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 36 April 18, 2012 Swing IV: Mouse and Keyboard Input Announcements Lab this week is review (BRING QUESTIONS) Game Project is out, due Tuesday April

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #15: October 21, 2015 1/34 Generic classes and types Generic programming is the creation of programming constructs that can be usedwith many different types.

More information

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the

More information

Introduction to Computer Science I

Introduction to Computer Science I Introduction to Computer Science I Graphics Janyl Jumadinova 7 February, 2018 Graphics Graphics can be simple or complex, but they are just data like a text document or sound. Java is pretty good at graphics,

More information

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

APPENDIX. public void cekroot() { System.out.println(nilai root : +root.data); } APPENDIX CLASS NODE AS TREE OBJECT public class Node public int data; public Node left; public Node right; public Node parent; public Node(int i) data=i; PROCEDURE BUILDING TREE public class Tree public

More information

Course overview: Introduction to programming concepts

Course overview: Introduction to programming concepts Course overview: Introduction to programming concepts What is a program? The Java programming language First steps in programming Program statements and data Designing programs. This part will give an

More information

CIS 162 Project 1 Business Card Section 04 (Kurmas)

CIS 162 Project 1 Business Card Section 04 (Kurmas) CIS 162 Project 1 Business Card Section 04 (Kurmas) Due Date at the start of lab on Monday, 17 September (be prepared to demo in lab) Before Starting the Project Read zybook chapter 1 and 3 Know how to

More information

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI Statics and GUI Dynamics 2 Statics: what s drawn on the screen Components buttons, labels, lists, sliders, menus,... Containers: components that contain

More information

Computer Science 210: Data Structures. Intro to Java Graphics

Computer Science 210: Data Structures. Intro to Java Graphics Computer Science 210: Data Structures Intro to Java Graphics Summary Today GUIs in Java using Swing in-class: a Scribbler program READING: browse Java online Docs, Swing tutorials GUIs in Java Java comes

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 34 April 13, 2017 Model / View / Controller Chapter 31 How is the Game Project going so far? 1. not started 2. got an idea 3. submitted design proposal

More information

Handling Mouse and Keyboard Events

Handling Mouse and Keyboard Events Handling Mouse and Keyboard Events 605.481 1 Java Event Delegation Model EventListener handleevent(eventobject handleevent(eventobject e); e); EventListenerObject source.addlistener(this);

More information

CS2110. GUIS: Listening to Events

CS2110. GUIS: Listening to Events CS2110. GUIS: Listening to Events Also anonymous classes Download the demo zip file from course website and look at the demos of GUI things: sliders, scroll bars, combobox listener, etc 1 mainbox boardbox

More information

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

SINGLE EVENT HANDLING

SINGLE EVENT HANDLING SINGLE EVENT HANDLING Event handling is the process of responding to asynchronous events as they occur during the program run. An event is an action that occurs externally to your program and to which

More information

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Graphics and Events Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Draw shapes, text in various fonts, and colors. 2) Build

More information

THE UNIVERSITY OF AUCKLAND

THE UNIVERSITY OF AUCKLAND CompSci 101 with answers THE UNIVERSITY OF AUCKLAND FIRST SEMESTER, 2012 Campus: City COMPUTER SCIENCE Principles of Programming (Time Allowed: TWO hours) NOTE: You must answer all questions in this test.

More information

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I Electrical and Computer Engineering Object-Oriented Topic : Graphics GUI Part I Maj Joel Young Joel.Young@afit.edu 15-Sep-03 Maj Joel Young A Brief History Lesson AWT Abstract Window Toolkit Implemented

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 36 November 30, 2018 Mushroom of Doom Model / View / Controller Chapter 31 Announcements Game Project Complete Code Due: Monday, December 10th NO LATE

More information

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games ECE 462 Object-Oriented Programming g using C++ and Java Key Inputs in Java Games Yung-Hsiang Lu yunglu@purdue.edu d YHL Java Key Input 1 Handle Key Events have the focus of the keyboard inputs by calling

More information

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } } 9.12) public interface Filter boolean accept(object x); Describes any class whose objects can measure other objects. public interface Measurer double measure(object anobject); This program tests the use

More information

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction to the JAVA UI classes Advanced HCI IAT351 Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber Portion of the Java class hierarchy that include basic graphics classes and Java 2D API classes and interfaces. java.lang.object Java.awt.Color Java.awt.Component Java.awt.Container

More information

Lecture 3: Java Graphics & Events

Lecture 3: Java Graphics & Events Lecture 3: Java Graphics & Events CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki Text Input Scanner class Constructor: myscanner = new Scanner(System.in); can use file instead of System.in new Scanner(new

More information

Chapter 14: Applets and More

Chapter 14: Applets and More Chapter 14: Applets and More Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 14 discusses the following main topics: Introduction to

More information

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More Chapter 14: Applets and More Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 14 discusses the following main topics: Introduction to

More information

1 Definitions & Short Answer (5 Points Each)

1 Definitions & Short Answer (5 Points Each) Fall 2013 Final Exam COSC 117 Name: Write all of your responses on these exam pages. If you need more space please use the backs. Make sure that you show all of your work, answers without supporting work

More information

Chapter 14: Applets and More

Chapter 14: Applets and More Chapter 14: Applets and More Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All rights reserved.

More information

FirstSwingFrame.java Page 1 of 1

FirstSwingFrame.java Page 1 of 1 FirstSwingFrame.java Page 1 of 1 2: * A first example of using Swing. A JFrame is created with 3: * a label and buttons (which don t yet respond to events). 4: * 5: * @author Andrew Vardy 6: */ 7: import

More information

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application Last Time v We created our first Java application v What are the components of a basic Java application? v What GUI component did we use in the examples? v How do we write to the standard output? v An

More information

Final Examination Semester 2 / Year 2010

Final Examination Semester 2 / Year 2010 Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2010 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student

More information

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009 ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009 1. True or False: (a) T In Alice, there is an event that is processed as long

More information

Applet which displays a simulated trackball in the upper half of its window.

Applet which displays a simulated trackball in the upper half of its window. Example: Applet which displays a simulated trackball in the upper half of its window. By dragging the trackball using the mouse, you change its state, given by its x-y position relative to the window boundaries,

More information

The AWT Event Model 9

The AWT Event Model 9 The AWT Event Model 9 Course Map This module covers the event-based GUI user input mechanism. Getting Started The Java Programming Language Basics Identifiers, Keywords, and Types Expressions and Flow

More information

Question 1. Show the steps that are involved in sorting the string SORTME using the quicksort algorithm given below.

Question 1. Show the steps that are involved in sorting the string SORTME using the quicksort algorithm given below. Name: 1.124 Quiz 2 Thursday November 9, 2000 Time: 1 hour 20 minutes Answer all questions. All questions carry equal marks. Question 1. Show the steps that are involved in sorting the string SORTME using

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 35 April 15, 2013 Swing III: OO Design, Mouse InteracGon Announcements HW10: Game Project is out, due Tuesday, April 23 rd at midnight If you want

More information

Java Swing Introduction

Java Swing Introduction Course Name: Advanced Java Lecture 18 Topics to be covered Java Swing Introduction What is Java Swing? Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java

More information

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

More information

CSC System Development with Java Introduction to Java Applets Budditha Hettige

CSC System Development with Java Introduction to Java Applets Budditha Hettige CSC 308 2.0 System Development with Java Introduction to Java Applets Budditha Hettige Department of Statistics and Computer Science What is an applet? applet: a Java program that can be inserted into

More information

CT 229 Arrays in Java

CT 229 Arrays in Java CT 229 Arrays in Java 27/10/2006 CT229 Next Weeks Lecture Cancelled Lectures on Friday 3 rd of Nov Cancelled Lab and Tutorials go ahead as normal Lectures will resume on Friday the 10 th of Nov 27/10/2006

More information

Programming graphics

Programming graphics Programming graphics Need a window javax.swing.jframe Several essential steps to use (necessary plumbing ): Set the size width and height in pixels Set a title (optional), and a close operation Make it

More information

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Events and Listeners CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides

More information

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN CSSE 220 Event Based Programming Check out EventBasedProgramming from SVN Interfaces are contracts Interfaces - Review Any class that implements an interface MUST provide an implementation for all methods

More information

PSIptt - Push-To-Talk Client API Documentation

PSIptt - Push-To-Talk Client API Documentation PSIptt - Push-To-Talk Client API Documentation Version 1.4.5 Stand 22.06.2009 PSI Transcom GmbH 2009 Content 1 INTRODUCTION...3 1.1 REQUIREMENTS...3 2 CONCEPT...4 2.1 ONLINE RESOURCES:...4 3 EXAMPLES...5

More information

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

Layouts and Components Exam

Layouts and Components Exam Layouts and Components Exam Name Period A. Vocabulary: Answer each question specifically, based on what was taught in class. Term Question Answer JScrollBar(a, b, c, d, e) Describe c. ChangeEvent What

More information

Control Statements: Part Pearson Education, Inc. All rights reserved.

Control Statements: Part Pearson Education, Inc. All rights reserved. 1 5 Control Statements: Part 2 5.2 Essentials of Counter-Controlled Repetition 2 Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable Increment/decrement

More information

Chapter 3 - Introduction to Java Applets

Chapter 3 - Introduction to Java Applets 1 Chapter 3 - Introduction to Java Applets 2 Introduction Applet Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language)

More information

Abstract Classes and Interfaces

Abstract Classes and Interfaces Abstract methods Abstract Classes and Interfaces You can declare an object without defining it: Person p; Similarly, you can declare a method without defining it: public abstract void draw(int size); Notice

More information

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing CSEN401 Computer Programming Lab Topics: Graphical User Interface Window Interfaces using Swing Prof. Dr. Slim Abdennadher 22.3.2015 c S. Abdennadher 1 Swing c S. Abdennadher 2 AWT versus Swing Two basic

More information

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox CS2110. GUIS: Listening to Events Lunch with instructors: Visit pinned Piazza post. A4 due tonight. Consider taking course S/U (if allowed) to relieve stress. Need a letter grade of C- or better to get

More information

Global Gomoku Lab 4 in D0010E

Global Gomoku Lab 4 in D0010E Luleå University of Technology February 20, 2012 Computer Science Håkan Jonsson Global Gomoku Lab 4 in D0010E 1 Introduction Modern forms of communication are more and more carried out over the Internet,

More information

CS2110. GUIS: Listening to Events

CS2110. GUIS: Listening to Events CS2110. GUIS: Listening to Events Lunch with instructors: Visit pinned Piazza post. A4 due tonight. Consider taking course S/U (if allowed) to relieve stress. Need a letter grade of C- or better to get

More information

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets Advanced Swing Outline More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets Using menus Frame menus add a menu bar to the frame (JMenuBar) add menus to

More information

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphics. Lecture 18 COP 3252 Summer June 6, 2017 Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple

More information

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using Java Provided Classes In this lesson we'll focus on using the Graphics class and its capabilities. This will serve two purposes: first

More information

Windows and Events. created originally by Brian Bailey

Windows and Events. created originally by Brian Bailey Windows and Events created originally by Brian Bailey Announcements Review next time Midterm next Friday UI Architecture Applications UI Builders and Runtimes Frameworks Toolkits Windowing System Operating

More information

Assignment 2. Application Development

Assignment 2. Application Development Application Development Assignment 2 Content Application Development Day 2 Lecture The lecture covers the key language elements of the Java programming language. You are introduced to numerical data and

More information

Topic 8 Graphics. Margaret Hamilton

Topic 8 Graphics. Margaret Hamilton Topic 8 Graphics When the computer crashed during the execution of your program, there was no hiding. Lights would be flashing, bells would be ringing and everyone would come running to find out whose

More information

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms. Java GUI Windows Events Drawing 1 Java GUI Toolkits Toolkit AWT Description Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

More information

: 15. (x, y) (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1)

: 15. (x, y) (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1) 73 12 12.1 15 15 ( 12.1.1) 16 15 15 12.1.1: 15 15 (x, y) x y 0 3 0 0 (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1) 74 12 Program 12.1.1 Piace.java import java.awt.*; public class Piece { private

More information

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

More information

UCLA PIC 20A Java Programming

UCLA PIC 20A Java Programming UCLA PIC 20A Java Programming Instructor: Ivo Dinov, Asst. Prof. In Statistics, Neurology and Program in Computing Teaching Assistant: Yon Seo Kim, PIC University of California, Los Angeles, Summer 2002

More information

CSIS 10A Practice Final Exam Solutions

CSIS 10A Practice Final Exam Solutions CSIS 10A Practice Final Exam Solutions 1) (5 points) What would be the output when the following code block executes? int a=3, b=8, c=2; if (a < b && b < c) b = b + 2; if ( b > 5 a < 3) a = a 1; if ( c!=

More information

Unit 7: Event driven programming

Unit 7: Event driven programming Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Unit 7: Event driven programming 1 1. Introduction 2.

More information

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal Review Session for EXCEPTIONS & GUI -Ankur Agarwal An Exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Errors are signals

More information

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2, Java Mouse Actions C&G criteria: 5.2.1, 5.4.1, 5.4.2, 5.6.2. The events so far have depended on creating Objects and detecting when they receive the event. The position of the mouse on the screen can also

More information

Packages: Putting Classes Together

Packages: Putting Classes Together Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic

More information

Part 3: Graphical User Interface (GUI) & Java Applets

Part 3: Graphical User Interface (GUI) & Java Applets 1,QWURGXFWLRQWR-DYD3URJUDPPLQJ (( Part 3: Graphical User Interface (GUI) & Java Applets EE905-GUI 7RSLFV Creating a Window Panels Event Handling Swing GUI Components ƒ Layout Management ƒ Text Field ƒ

More information

READ AND OBSERVE THE FOLLOWING RULES:

READ AND OBSERVE THE FOLLOWING RULES: This examination has 11 pages: check that you have a complete paper. Check that you have a complete paper. Each candidate should be prepared to produce, upon request, his or her SUNY/UB card. This is a

More information

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions Lunch with instructors: Visit Piazza pinned post to reserve a place Download demo zip file from course website, look at

More information

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011 CIS 120 Programming Languages and Techniques Final Exam, May 3, 2011 Name: Pennkey: My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in

More information

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components CS112-Section2 Hakan Guldas Burcin Ozcan Meltem Kaya Muge Celiktas Notes of 6-8 May Graphics Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how

More information

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information