block1-unit 5 swing - awt - applet budd 13,14,21

43
1 Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

Upload: estefani-perez

Post on 01-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21. Section-1 Swing and AWT packages. AWT and Swing Packages. The Abstract Windowing Toolkit (AWT) Provides basic facilities for drawing graphics (GUI) Drawing the lines and shapes AWT is core part of Java since the beginning - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

1

Block1-Unit 5Swing - AWT - Applet

Budd 13,14,21

Page 2: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

2

Section-1

Swing and AWT packages

Page 3: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

3

AWT and Swing Packages

• The Abstract Windowing Toolkit (AWT)– Provides basic facilities for drawing graphics

(GUI)• Drawing the lines and shapes

– AWT is core part of Java since the beginning

• Swing package like AWT– Jframe in swing , frame in AWT– but Swing was only introduced with Java 2.

Page 4: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

4

Differences between AWT and Swing Packages

• AWT components vary according to the underlying operating system, while Swing components do not. – if you create an AWT button it will look like a Windows button

on Windows based PC and a Macintosh button on a Macintosh.

– A Swing component will look the same on any platform.

• Swing is also larger and more comprehensive than AWT

• Swing components are much more computationally intensive

Page 5: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

5

java.lang.Object

Component MenuComponentCheckboxGroup

Button CheckboxCanvas Choice Container Label List Scrollbar TextComponent

JComponent Window

Frame

JFrame

Dialog

JDialog

PanelScrollpane

Applet

JApplet

java.awt.* javax.swing.*

JLabel JListAbstractButton

JButton

JPanel JScrollpane

Page 6: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

6

A Component class

• A Component something can be displayed on a two-dimensional screen and the user can interact with it.– buttons, checkboxes, scroll bars are declared as types of

components.

• Attributes of a component include a size, a location, foreground and background colors, visibility , and a set of listeners for events.

• Methods component class:– setLocation(int,int), getLocation()– setSize(int,int), getSize()– repaint(Graphics)– paint(Graphics)– addMouseListener(MouseListener)

Page 7: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

7

Container

• A Container is a type of component that can nest other components within it.

• Complex graphical interfaces are constructed using a container

• A container maintains – a list of the components it manipulates, – a layout manager to determine how the components

should be displayed.

• Methods defined in container class :– setLayout(LayoutManager) Set layout manager for

display– Add(Component),– remove(Component)

Page 8: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

8

Window

• A Window is a type of Container. • Methods defined in windows class :

– show() Make the window visible– toFront() Move window to front– toBack() Move window to back

• A Frame is a type of window • Methods defined in Frame class

– setTitle(String), getTitle() Set or get title– setCursor(int) Set cursor– setResizable() Make the window resizable– setMenuBar(MenuBar) Set menu bar for window

Page 9: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

9

JFrame

• The Swing class JFrame is a subclass of AWT class Frame.

• A JFrame will maintain a Container, called the contentPane,

• contentPane will hold all the graphical elements of the window.

Page 10: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

10

layout manager

• The layout manager places the components held in a container in their positions.

Page 11: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

11

layout manager Types

• Five standard types of layout managers:1. BorderLayout

2. GridLayout

3. FlowLayout

4. CardLayout

5. GridBagLayout

Page 12: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

12

BorderLayout

• The BorderLayout can manage no more than five different components.

• The default layout manager for applications constructed by subclassing from JFrame.

• The five locations in this layout manager are: North, West, East, South and Center.

• Not all five locations need to be filled. • If a location is not used, the space is

allocated to the remaining components.

Page 13: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

13

BorderLayout Example

Example://Add a button to the top of the display.

// In JFrame, we must first use the method

// getContentPane in order to

// access the container holding the window content

getContentPane().add("North", new JButton("quit"));

.

Page 14: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

14

GridLayout

• The GridLayout creates a rectangular array of components

• Each occupying the same size portion of the screen.

• Using arguments with the constructor, the programmer specifies the number of rows and columns in the grid.

• Two additional integer arguments can be used to specify a horizontal and vertical space between the components.

Page 15: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

15

GridLayout Example

//Make a 4 by 4 grid with 3 pixels between //each element.

JPanel p = new JPanel();

p.setLayout(new GridLayout(4,4,3,3));

p.add(new ColorButton(Color.black, "black"));

Page 16: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

16

FlowLayout

• The FlowLayout places components in rows left to right, top to bottom.

• Unlike the GridLayout manager– The components need not all have the

same size.– When a component cannot be completely

placed on a row without truncation, a new row is created.

Page 17: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

17

CardLayout– The CardLayout stacks components vertically. Only one

component is visible at a time.– The components managed by a card layout manger

which named (using the addmethod)– Subsequently, a named component can be made the

visible component.CardLayout lm = new CardLayout();JPanel p = new Jpanel(lm);p.add("One", new JLabel("Number One"));p.add("Two", new JLabel("Number Two"));p.add("Three", new JLabel("Number Three"));.. lm.show(p, "Two"); //show component "Two"

Page 18: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

18

GridBagLayout

• the most general type of layout managers.

• It allows the programmer to create non-uniform grid of squares and place components in various positions within each square.

Page 19: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

19

User Interface Components• In Swing, all user interface components are

subclass of the parent class JComponent which is a subclass of the Container class

Page 20: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

20

JPanel

• A JPanel is both a Container and a Component.

• it represents a rectangular region of the display.

• It can hold its own layout manager for inserting components into it.

• it must in turn be inserted into the application display.

Page 21: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

21

JPanel Example

Page 22: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

22

JPanel Exampleprivate JPanel makeScrollBars() {

JPanel p = new JPanel();p.setLayout(new BorderLayout());p.add("West", redBar);p.add("Center", greenBar);p.add("East", blueBar);return p;

}• The panel return from last method is placed on left side

of the application window.Container p = getContentPane();…p.add("West", makeScrollBars());…

Page 23: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

23

Dialogs• A JDialog is a window displayed for a short

period of time during execution, then it disappears.– notify the user of certain events,– to ask simple questions.

• Two types– A modal dialog demands a response from the user,

and it prevents the user from performing any further action until the dialog is dismissed.

– A nonmodal dialog (or modeless dialog), can be ignored by the user.

Page 24: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

24

The Menu Bar

• A Swing bar is a graphical component declared as a sub-class of JComponent,

• both menu bars and menus act like containers.• A menu bar contains a series of menus, and

each menu contains a series of menu items.• An instance of JMenuBar can be attached to a

Frame using the method setJMenuBar:

JMenuBar bar = new JMenuBar();

setJMenuBar(bar);

Page 25: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

25

Section-2

Applets

Page 26: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

26

Applets

• Applets allow web browsers to execute applications downloaded from other computers.

• Applets :Applications written for the WWW.• Applets are attached to documents distributed

over the WWW. • These documents are written using the

HyperText Markup Language (HTML) protocol.• A Web browser that includes a Java processor

will then automatically retrieve and execute the Java program.

Page 27: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

27

Applets in HTML file• In HTML file

<appletcodebase = "http://www.sun.com"code = "MyApplet.class"width = "400"height = "300"><param name=name1 value="value1">You do not have a Java enabled browser</applet>

• The <applet> tag indicates the start of applet.• The codebase parameter gives the URL Web address

where the Java program• The code parameter provides the name of the class.• The width and height attributes provide the space

allocated to applet in the browser

Page 28: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

28

Applets in HTML file

• <param> tags provides data to applet

• values associated with parameters can be accessed

• using the method getParameter(). Example: String s = getParameter(name1); // s

will contain "value1"

• If an applet cannot be loaded– text between <applet> and </applet> which is

not part of a <param> clause, will be displayed by the browser.

Page 29: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

29

Security Issues

• Applets are not permitted to run local executable program.

• Applets cannot read or write to the local computer's file system.

• Applets can only communicate with the server from which they originate.

• Applets can learn only a very restricted set of facts about the local computer.

Page 30: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

30

Applets and Applications• A program that is intended to run on the Web is

subclasses from class Applet (instead of JFrame).• class Applet provides the necessary structure and

resources needed to run a program on the Web.• The Swing class JApplet is a subclass of the AWT

Applet class which is a subclass of Panel.– JApplet inherits the applet functionality of Applet and

the graphical component attributes of Panel.• applets start execution at a method named init,

which is defined in class Applet but can be overridden by users.– As you know applications start running from main

method

Page 31: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

31

Methods in applet class

• init() – Invoked when an applet is first loaded.– Can be used for one-time initialization

• start() – Called to begin execution of the applets.

• stop() – Called when a Web page containing an applet is

hidden.

• destroy() – Called when the applet is about to be terminated.– Should halt the application and free any used

resources.

Page 32: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

32

Sequence of methods execution of applets

• Suppose a Web page containing an applet and several links

• The applet will first call init(), then start().• If the user clicks on one of the links,

– the Web page holding the applet is overwritten,– the method stop() will be invoked to temporarily halt

the applet.

• When the user returns to the Web page– the method start(), but not init(), will be called again

• When the user finally exits the page – the method destroy() will be called.

Page 33: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

33

Differences between an application and an applet

• Applets are created by subclassing from the class JApplet

• Applets begin execution with the method init, rather than main.

• Applets can be halted and restarted as the Web browser moves to a new page and returns.

Page 34: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

34

Section-2Input and Output Streams

Page 35: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

35

Streams versus Readers and Writers

• stream is a programming abstraction and can be thought of as a communication channel between the program and a source (an input stream) or sink (an output stream).

• This channel is represented as an object in a Java program, and has associated with it various methods for sending, receiving and manipulating data.

• At lowest level – a stream is a device for transmitting or receiving 8-bit values.

• Example– A file is a collection bytes stored on an external device – A Java object (e.g. instance of FileStream class) provides the

means to access the data values in the file – but it does not actually hold the file contents.

Page 36: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

36

Input/output

• Input and output can be divided – stream abstractions

• InputStream and OutputStream• read and write 8-bit values,

– Reader/Writer classes,• manipulates 16-bit Unicode character values.

Page 37: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

37

Input streams• The class InputStream is an abstract class• parent to 10 subclasses in the Java library.• Has the following common methods• read() :reads a single byte from the input and returns it

as a positive int (or –1– It will wait until data is , the program will be blocked

• read(byte[] buffer) :reads a collection of values from the input, placing them into buffer– It returns a positive int containing the number of bytes read or –1 if the

end of the stream was reached.• skip(long n): skips bytes from the input and returns a long value

containing a count of the number of bytes that were actually skipped.

• available(): determines the number of bytes readable without blocking and returns it as an int.

• close closes the input stream.• All methods throws an exception, IOException, if error occurs

Page 38: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

38

Output streams• Methods of outputStreem

– write() : writes a single byte to the output.– write (byte [] buffer): writes an array of byte values.– flush(): flush all output from buffers.– Close: closes the output stream.

• Sending one character to output device is inefficient– Many subclasses of OutputStream will collect data in

an internal buffer and a set of bytes sent– sometimes the programmer may need to sends bytes

currently in buffer to output • method flush() is used to do this

– Closing a stream will automatically flush all pending operations.

Page 39: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

39

Output streams• classes characterize the physical location of

the output. – ByteArrayOutputStream: writes values into an in-

memory byte array– FileOutputStream: writes values to an external

file– PipedOutputStream: writes values to a pipe.

• classes adds more behavior to an output stream.– ObjectOutputStream – FilterOutputStream :performs some tasks before sending

• BufferedOutputStream, DataOutputStream, PrintStream.

Page 40: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

40

Readers and Writers• manipulate 16-bit Unicode character values• Divided into two :

– Manipulate physical locations• CharArrayReader, StringReader, FileReader

– Add functionality to data being generated to another reader

• BufferedReader, LineNumberReader, FilterReader

• Readers and writers are useful whenever the input or output values are purely textual, as opposed to binary data such as colors or images.

Page 41: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

41

Readers writers example

• The class BufferedReader is a filter, which must be built on top of another reader.

• To use it to read text from a file,– create a FileReader, and – then use the file reader to construct the

BufferedReader:FileReader f = new FileReader("filename");BufferedReader input = new BufferedReader(f);…// read line of text from a fileString text = input.readLine();

Page 42: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

42

Readers writers

• Readers and writers are linked to streams (i.e. InputStream and OutputStream) via the wrapper classes InputStreamReader and OutputStreamReader respectively.

• the constructor for InputStreamReader takes an InputStream object as an argument and returns an object that responds to Reader methods.

• the constructor for OutputStreamReader takes an OutputStream object as an argument and returns an object that responds to Writer methods.

Page 43: Block1-Unit 5 Swing - AWT - Applet Budd 13,14,21

43

Example

//The following could be used to read lines from a file that //contained Cyrillic characters:

// first get access to the fileFileInputStream f = new FileInputStream("filename");// then convert bytes to charactersInputStreamReader r = new InputStreamReader(f, "MacCyrillic");// then buffer the inputBufferedReader input = new BufferedReader(r);// now read text line by lineString text = input.readLine();while (text != null) {..text = input.readLine();}