View
218
Download
0
Tags:
Embed Size (px)
Citation preview
OOP&M - theory lectures 1
OOP&M – the third day
“…I cannot help with this terrible headache …”- Bjorn the morning after -
OOP&M - theory lectures 2
OOP&M – input / output to programs
Javaprogram
keyboard
monitor
files
network
InputStream
System.out
FileOutputStream
Javaprogram
??
FileInputStream
System.in
OOP&M - theory lectures 3
OOP&M – input … an overview
Javaprogram
InputStrea
m
bytes of data
ewsc24rfds53Hej20Hur20m76ar20du20?rsf
Input source
We must build a bridge between the Java Program and the input source!!
OOP&M - theory lectures 4
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:
String s;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()
OOP&M - theory lectures 5
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 – internetEXERCISE C.1 – Just reading a file
OOP&M - theory lectures 6
•Features:•Interpret HTML TAGs
•underline a text if it is a link (<A HREF..>)•jump line with <P> <BR>•show the name of the images (<IMG SRC...>)•try to show tables (<TABLE><TR><TD>...)•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
OOP&M - theory lectures 7
OOP&M – the third day
“…and the little computer knew then that computers would always grow wiser and more powerful …”
- Isaac Asimov -
OOP&M - theory lectures 8
OOP&M – the e-volution
1001100010110011100110
01001111100
load reg;nop;add x;jmp $009;
time
OOP&M - theory lectures 9
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
OOP&M - theory lectures 10
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
OOP&M - theory lectures 11
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
OOP&M - theory lectures 12
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.
OOP&M - theory lectures 13
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!!
OOP&M - theory lectures 14
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
OOP&M - theory lectures 15
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);
OOP&M - theory lectures 16
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); }}
OOP&M - theory lectures 17
OOP&M – the GUI – the problem appears•Imagine a button:
PRESS MEPRESS ME
•Can you decompose it into different graphical components?
•DO YOU THINK YOU CAN?
•ARE YOU SURE? you have 5 minutes
OOP&M - theory lectures 18
OOP&M – the GUI – the problem appears•Decomposition of the button:
PRESS MEPRESS ME
PRESS MEPRESS ME
OOP&M - theory lectures 19
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]
OOP&M - theory lectures 20
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 initial setup for the applet
OOP&M - theory lectures 21
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:
Button b;b = new Button(“touch Bjorn”);
OOP&M - theory lectures 22
OOP&M – the GUI – a place to pressNow 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:
Button b;b = new Button(“touch Bjorn”);add(b);