Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP&M - theory lectures1 OOP&M – the third day “… I cannot help with this terrible headache …” - Bjorn the morning after -

Similar presentations


Presentation on theme: "OOP&M - theory lectures1 OOP&M – the third day “… I cannot help with this terrible headache …” - Bjorn the morning after -"— Presentation transcript:

1 OOP&M - theory lectures1 OOP&M – the third day “… I cannot help with this terrible headache …” - Bjorn the morning after -

2 OOP&M - theory lectures2 OOP&M – input / output to programs Java program keyboard monitor files network InputStream System.out FileOutputStream Java program ? ? FileInputStream System.in

3 OOP&M - theory lectures3 OOP&M – input … an overview Java program InputStream bytes of data ewsc24rfds53Hej20Hur20m76ar20du20?rsf Input source We must build a bridge between the Java Program and the input source!!

4 OOP&M - theory lectures4 OOP&M – from LAB2 – internet When we are try to read information from the Web The Net as other communication media for our programs can be read using Streams There is a difference between what we saw until now and what will happen with the Net: files are in HTML code The constructors are: Strings; URL u; InputStream ins; InputStreamReader isr; BufferedReader link; u = new URL("http://www.domain.ext/"); ins = u.openStream(); isr = new InputStreamReader(ins); link = new BufferedReader(isr); s = link.readLine(); As you see here, the structure is similar to the one that we saw for the files. The difference is at the constructor for the InputStream object, which is special!! But the method for reading the information is the same: *.readline()

5 OOP&M - theory lectures5 import java.net.*; import java.io.*; class MyFirstBrowser { public static void main(String[] arg) throws Exception { URL uNet; InputStream insNet; InputStreamReader isrNet; BufferedReader linkNet; String s; uNet = new URL("http://webzone.k3.mah.se/k3dacu/oopm/week5/index1.html"); insNet = uNet.openStream(); isrNet = new InputStreamReader(insNet); linkNet = new BufferedReader(isrNet); s = linkNet.readLine(); System.out.println(s); s = linkNet.readLine(); System.out.println(s); System.out.println("connection closed"); } OOP&M – from LAB2 – internet EXERCISE C.1 – Just reading a file

6 OOP&M - theory lectures6 Features: Interpret HTML TAGs underline a text if it is a link ( ) jump line with show the name of the images ( ) try to show tables (...) think how to ask to the user for the next jump to do... Interpret scripting languages JavaScript Visual Basic Script Perl Calls external applications Office Java Virtual Machine OOP&M – from LAB2 – internet The Browser

7 OOP&M - theory lectures7 OOP&M – the third day “… and the little computer knew then that computers would always grow wiser and more powerful …” - Isaac Asimov -

8 OOP&M - theory lectures8 OOP&M – the e-volution 100110 0010110011100110 010011 11100 load reg; nop; add x; jmp $009; time

9 OOP&M - theory lectures9 OOP&M – the e-volution – a definition The evolution of the electronics has leaded us to a moment in which the interaction between computers and humans is made by the use of a 2 dimensional interface Because the control of the electronic equipment is quite complex, engineers have developed programs that use a reduced set of interaction possibilities in order to allow creators an easy way of work The GRAPHICAL USER INTERFACE (GUI from now) defines the use of interfaces which include some of that interaction methods Buttons, menus, scrollable windows and graphical drawings are used to communicate with the users

10 OOP&M - theory lectures10 OOP&M – the GUI – an approach The are different ways of attacking the problem of designing a user interface One is directly related to the Internet As you know, HTML has got only some very basic features for the communication to users … it is one-directional: it goes from the server to the screen of the user, but, by itself, cannot capture any user actions As we have seen in the description of a browser, HTML interpreters can call to other programs that execute external code, as JAVA, improving the communication An approach to the GUI design is to program applets

11 OOP&M - theory lectures11 OOP&M – the GUI – an approach with applets An applet is a Java program embedded in a web page. It is not only a way of providing a way of getting information form the user, because it is capable of compute something as well Applets use the GUI facilities that are provided with Java. These are NOT a part of the language proper; they belong to a package of predefined classes known as the abstract window toolkit (AWT) Applets are defined in another package, called the applet package and, for that reason they don’t have a main() declaration, it is a part of the class Applets can have a declaration called init() instead, which will contain special variable declarations and other “stuff” that should declared only once

12 OOP&M - theory lectures12 OOP&M – the GUI – some methods from AWT We already know one method contained into the AWT package In LABone we were experimenting with drawString in order to show some text on the screen In the AWT there are methods that can be used to define Graphics objects On of those objects models the drawing behavior of a portion of a computer screen; that is, it will respond to messages requesting that rectangles be drawn, background colors be set, current font information be returned, etc.

13 OOP&M - theory lectures13 OOP&M – the GUI – some methods from AWT import java.applet.*; import java.awt.*; public class textapplet extends Applet { public void paint(Graphics g) { g.drawString("Time |To Do |Needings", 30, 30); g.drawString("--------------------------------------------- ", 30, 45); g.drawString("10:00 |wake up |faith ", 30, 60); g.drawString("10:05 |shower |soap ", 30, 75); g.drawString("10:25 |breakfast |milk, juice, eggs, jam... ", 30, 90); g.drawString("11:00 |university |pants, T-shirt, books? ", 30, 105); } Do you remember our first applet? We had the problem of the alignment of the last column of the table AWT provides us some tools for solving this!!

14 OOP&M - theory lectures14 OOP&M – the GUI – some methods from AWT AWT provides a Color and a Font class that enable the programmer to define and use new colors and fonts Colors are described using three integers that specify the amount of RGB in a scale of 0..255 each Fonts are specified by naming a font family (expressed as a String : “TimesRoman” or “Helvetica”, e.g.), a font style ( Font.PLAIN, Font.BOLD or Font.ITALIC ) and a number specifying point size (12 is typical) To change one of this features we need to first create an object and send a message to the Graphics object before drawing the text

15 OOP&M - theory lectures15 OOP&M – the GUI – some methods from AWT Change the Color Color c; c = new Color(180,10,120); g.setColor(c); g.drawString("Time …", 30, 30); Change the Font Font f; f = new Font(“Helvetica”, Font.BOLD, 14); g.setFont(f); g.drawString("Time …", 30, 30);

16 OOP&M - theory lectures16 OOP&M – the GUI – some methods from AWT Besides drawing strings and setting font and color, Graphics provides the following simple geometric object drawing methods: fillOval : draws an oval on the screen filled up with color fillRect : draws a rectangle filled up with color drawOval : draws an oval drawRect : draws a rectangle drawLine : draws a line An example would be: import java.awt.*; import java.applet.*; public class DrawOval extends Applet { public void paint(Graphics g) { Color c = new Color(20,120,160); g.setColor(c); g.fillOval(20,20,60,30); }

17 OOP&M - theory lectures17 OOP&M – the GUI – the problem appears Imagine a button: PRESS ME Can you decompose it into different graphical components? DO YOU THINK YOU CAN? ARE YOU SURE?  you have 5 minutes

18 OOP&M - theory lectures18 OOP&M – the GUI – the problem appears Decomposition of the button: PRESS ME

19 OOP&M - theory lectures19 OOP&M – the GUI – the problem appears The common Interaction tools that we use for the interaction with computers are VERY COMPLICATED to be implemented each time that we need them Buttons, menu-bars, scroll controls, text boxes, radio buttons … are predefined and ready to use in the AWT package so that we do not need to create them ourselves [Later we will see other possibilities, like the Swing package that offers many other ways of handling with this object]

20 OOP&M - theory lectures20 OOP&M – the GUI – Applets are alive Before we start to draw buttons on the screen, we need to learn a little bit more about the life cycle of applets When an applet is first loaded into the browser, the browser invokes its init() method Each time that we make a change on the screen or that we move the window … the Applet class invokes again all the methods contained into the applet But the init() method is called only once. Its intended role is to do the init ial setup for the applet

21 OOP&M - theory lectures21 OOP&M – the GUI – a place to press Java AWT provides a predefined class for modeling buttons, called Button the constructor for Button accepts the text in it (a String reference) as its argument new Button ( string ) An example of this would be: Buttonb; b = new Button(“touch Bjorn”);

22 OOP&M - theory lectures22 OOP&M – the GUI – a place to press Now we have created the object button, but we must also accomplish the following: Buttons should appear in the applet The applet should be able to respond to clicks in the buttons Java AWT provides a predefined method that handles both problems: add() The complete code would be: Buttonb; b = new Button(“touch Bjorn”); add(b);


Download ppt "OOP&M - theory lectures1 OOP&M – the third day “… I cannot help with this terrible headache …” - Bjorn the morning after -"

Similar presentations


Ads by Google