Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.

Similar presentations


Presentation on theme: "David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API."— Presentation transcript:

1 David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API COMP 112 2014T1 2

2 COMP 112 2: 2 © D Streader Programming for User Interaction Programming requires you to understand both the building blocks your language gives you and what you are trying to achieve. We are not going to consider what makes a well designed User Interaction. We are going to talk about the Building blocks of UI programming. 1.Text bases UI 2.Graphical UI

3 COMP 112 2: 3 © D Streader Text base UI import ecs100.UI; Java text base UI 1.inputs from System.in.read(). – the keybord and 2.outputs to System.out.println().– the screen Output to the screen (standard out) Print to screen - UI.print (“Hellow”); Print to screen and new line - UI.println (“Hellow”); UI.print (“Hellow\n”); == UI.println (“Hellow”); Integer,int,boolean,…. Are valid println parameters.

4 COMP 112 2: 4 © D Streader Typed input from the keyboard UI.askInt( question ); UI.askString( question ); UI.askBoolean( question ); … These methods have built in error checking unlike the java primitives. Time line for execution: 1.The question is displayed on the screen 2.The program waits till Rtn ( Enter ) is keyed 3.Then the command returns the input text and ends Text base UI Program System Call Back 3 1 2

5 COMP 112 2: 5 © D Streader Graphical UI import ecs100.*; Graphical objects can be displayed Input from the mouse can be processed Displaying images from a file (in the directory where the code is held) at top left location x=10,y=100. UI.drawImage ("main.jpg", 10, 100); You can draw lines, arcs, rectangles, strings, ovals and polygons in a similar way. in BlueJ type UI.draw and Ctrl space for details.

6 COMP 112 2: 6 © D Streader While loops in Java Spot the mistake in the code below How would you break out of the loop to correct it?

7 COMP 112 2: 7 © D Streader While and return Need the return statement be at the method’s end? For some people this is bad style For others it is good style

8 COMP 112 2: 8 © D Streader While Loop control flow : 1.falling through – when guard is false 2.using break to jump out of the loop 3.using continue to jump to the next iteration. 4.using return to exit the method

9 COMP 112 2: 9 © D Streader Graphical UI import ecs100.*; Graphical objects can be displayed Input from the mouse can be processed Displaying images from a file (in the directory where the code is held) at top left location x=10,y=100. UI.drawImage ("main.jpg", 10, 100); You can draw lines, arcs, rectangles, strings, ovals and polygons in a similar way. in BlueJ type UI.draw and Ctrl space for details.

10 COMP 112 2: 10 © D Streader Buttons Give a Button a text to display and a method to call UI.addButton(“Drop Me”, this::doDrop); Display a Button with the text “Drop Me” and when the button is called the method doDrop on this object will be executed

11 COMP 112 2: 11 © D Streader Listen for mouse events Treat Mouse events just like Button events: 1.Tell the system the call back method 2.Implement the method that the system will call back 2. Implement the method 1. the call back method


Download ppt "David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API."

Similar presentations


Ads by Google