8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 1/55
1
A Simple Applet
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 2/55
2
Applets and applications
An applet is a Java program that runs on a web page Applets can be run within any modern browser
To run modern Java applets, old browsers need an up-to-date
Java plugin appletviewer is a program that can run
An application is a Java program that runs all by itself
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 3/55
3
Packages and classes
Java supplies a huge library of pre-written “code,”
ready for you to use in your programs
Code is organized into classes
Classes are grouped into packages
One way to use this code is to import it
You can import a single class, or all the classes in a
package
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 4/55
4
The Applet class
To create an applet, you must import the Applet class This class is in the java.applet package
The Applet class contains code that works with a
browser to create a display window Capitalization matters!
applet and Applet are different names
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 5/55
5
Importing the Applet class
Here is the directive that you need:
import java.applet.Applet;
import is a keyword
java.applet is the name of the package
A dot ( . ) separates the package from the class
Applet is the name of the class
There is a semicolon ( ; ) at the end
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 6/55
6
The java.awt package
“awt” stands for “Abstract Window Toolkit”
The java.awt package includes classes for: Drawing lines and shapes
Drawing letters
Setting colors
Choosing fonts
If it’s drawn on the screen, then java.awt is probably involved!
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 7/557
Importing the java.awt package
Since you may want to use many classes from the
java.awt package, simply import them all:
import java.awt.*;
The asterisk, or star (*), means “all classes”
The import directives can go in any order, but must be
the first lines in your program
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 8/558
The applet so far
import java.applet.Applet;
import java.awt.*;
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 9/559
Your applet class
public class Drawing extends Applet {… }
Drawing is the name of your class
Class names should always be capitalized extends Applet says that our Drawing is a kind of Applet, but with added capabilities Java’s Applet just makes an empty window
We are going to draw in that window The only way to make an applet is to extend Applet
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 10/5510
The applet so far
import java.applet.Applet;
import java.awt.*;
public class Drawing extends Applet {
…we still need to put some code in here...
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 11/5511
The paint method
Our applet is going to have a method to paint some
colored rectangles on the screen
This method must be named paint
paint needs to be told where on the screen it can draw This will be the only parameter it needs
paint doesn’t return any result
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 12/5512
The paint method, part 2
public void paint(Graphics g) { … } public says that anyone can use this method
void says that it does not return a result
A Graphics (short for “Graphics context”) is an objectthat holds information about a painting It remembers what color you are using
It remembers what font you are using
You can “paint” on it (but it doesn’t remember what youhave painted)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 13/5513
The applet so far
import java.applet.Applet;
import java.awt.*;
public class Drawing extends Applet {
public void paint(Graphics g) {
…we still need to put some code in here… }
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 14/5514
Colors
The java.awt package defines a class named Color There are 13 predefined colors—here are their fully-qualified
names:
For compatibility with older programs (before the naming
conventions were established), Java also allows color names
in lowercase: Color.black, Color.darkGray, etc.
Color.BLACK Color.PINK Color.GREENColor.DARK_GRAY Color.RED Color.CYAN
Color.GRAY Color.ORANGE Color.BLUE
Color.LIGHT_GRAY Color.YELLOW
Color.WHITE Color.MAGENTA
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 15/5515
New colors
Every color is a mix of red, green, and blue
You can make your own colors:
new Color( red , green , blue )
Amounts range from 0 to 255 Black is (0, 0, 0), white is (255, 255, 255)
We are mixing lights, not pigments
Yellow is red + green, or (255, 255, 0)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 16/5516
Setting a color
To use a color, we tell our Graphics g what color we
want:
g.setColor(Color.RED);
g will remember this color and use it for everything untilwe tell it some different color
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 17/5517
The paint method so far
public void paint(Graphics g) {
g.setColor(Color.BLUE);
…draw a rectangle…
g.setColor(Color.RED); …draw another rectangle…
}
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 18/5518
Java’s coordinate system
Java uses an (x, y) coordinate system
(0, 0) is the top left corner
(50, 0) is 50 pixels to the right of (0, 0) (0, 20) is 20 pixels down from (0, 0)
(w - 1, h - 1) is just inside the bottom right corner, where w
is the width of the window and h is its height
(0, 0)
(0, 20)
(50, 0)
(50, 20)
(w-1, h-1)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 19/5519
Drawing rectangles
There are two ways to draw rectangles: g.drawRect( left , top , width , height );
g.fillRect(left , top , width , height );
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 20/5520
The complete applet
import java.applet.Applet;
import java.awt.*;
public class Drawing extends Applet {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(20, 20, 50, 30);g.setColor(Color.RED);
g.fillRect(50, 30, 50, 30);
}
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 21/5521
Some more java.awt methods
g.drawLine( x1 , y1 , x2 , y2 );
g.drawOval( left , top , width , height );
g.fillOval( left , top , width , height );
g.drawRoundRect( left , top , width , height );
g.fillRoundRect( left , top , width , height ); g.drawArc( left , top , width , height ,
startAngle , arcAngle ); g.drawString( string , x , y );
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 22/55
22
The HTML page
You can only run an applet in an HTML page The HTML looks something like this:
<html>
<body>
<h1>DrawingApplet Applet</h1><applet code="DrawingApplet.class"
width="250" height="200">
</applet>
</body></html>
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 23/55
Java
GUI building with Swing
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 24/55
24
AWT (Abstract Window Toolkit)
Present in all Java implementations Described in (almost) every Java textbook
Adequate for many applications
Uses the controls defined by your OS (whatever it is) therefore it's “least common denominator”
Difficult to build an attractive GUI
import java.awt.*;import java.awt.event.*;
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 25/55
25
Swing
Present in all modern Java implementations (since 1.2) More controls, and they are more flexible Gives a choice of “look and feel” packages Much easier to build an attractive GUI import javax.swing.*;
import javax.swing.event.*;
and
import java.awt.*;import java.awt.event.*; You may not need all of these packages
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 26/55
26
Swing vs. AWT
Swing is built “on top of” AWT, so you need to import AWT
and use a few things from it
Swing is bigger and slower
Swing is more flexible and better looking
Swing and AWT are incompatible--you can use either, but you
can’t mix them Actually, you can, but it’s tricky and not worth doing
Basic controls are practically the same in both AWT: Button b = new Button ("OK");
Swing: JButton b = new JButton("OK");
Swing gives far more options for everything (buttons with
pictures on them, etc.)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 27/55
AWT Class Hierarchy
27
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 28/55
28
To build a GUI...
1. Make somewhere to display things (a Container) Usually you would use a JFrame or a JApplet
2. Create some Components (buttons, text areas, panels, etc.) It’s usually best to declare Components as instance variables, and
Define them in your applet’s init() method or in some applicationmethod
3. Add your Components to your display area Choose a layout manager Add your Components to your JFrame or JApplet according to the
rules for your layout manager
4. Attach Listeners to your Components Interacting with a Component causes an Event to occur A Listener gets a message when an interesting event occurs, and
executes some code to deal with it
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 29/55
29
Containers and Components
A GUI is built by putting components into containers
The job of a Container is to hold and display Components
Some frequently used types (subclasses) of Component are
JButton, JCheckbox, JLabel, JTextField, and JTextArea
A Container is also a Component This allows Containers to be nested
Important Container classes are JFrame, JApplet, and JPanel JFrame and JApplet both contain other containers; use
getContentPane() to get to the container you want
You typically create and use JPanels directly
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 30/55
30
Starting with a Container
First, import some packages: import javax.swing.*;
import javax.swing.event.*;import java.awt.*;import java.awt.event.*;
Second, extend a Container type: For an application, extend JFrame
public class MyClass extends JFrame { ... }
For an applet, extend JApplet public class MyApplet extends JApplet { ... }
Neither of these returns a Container that you use directly; instead, Both JFrame and JApplet have a getContentPane() method
that returns a Container you can use: getContentPane( ).doSomething( );
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 31/55
31
A JApplet is a Panel in a Container
java.lang.Object|
java.awt.Component
|
java.awt.Container
|
java.awt.Panel
|
java.applet.Applet
|javax.swing.JApplet
…so you can display things in an Applet
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 32/55
32
Example: A "Life" applet
Container (Applet)
Containers (Panels)
Component (Canvas)
Components (Buttons)
Components (Labels)
Components (TextFields)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 33/55
33
Applets
An application has a public static void main(String args[ ]) method, but
an applet usually does not
An applet's main method is in the Browser
To write an applet, you extend JApplet and override
some of its methods
The most important methods are init( ), start( ), and
paint(Graphics g)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 34/55
34
To create an applet
public class MyApplet extends JApplet { … } The only way to make an applet is to extend Applet or
JApplet
You can add components to the applet
The best place to add components is in init( )
You can paint directly on the applet, but…
…it’s better to paint on a contained component
Do all painting from paint(Graphics g)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 35/55
35
Some types of components
Label Button
Button
Checkbox
Choice
List
Scrollbar
TextField TextArea
CheckboxGroupCheckbox
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 36/55
36
Creating components
JLabel lab = new JLabel ("Hi, Dave!");
JButton but = new JButton ("Click me!");
JCheckBox toggle = new JCheckBox ("toggle");
JTextField txt =new JextField ("Initial text.", 20);
JScrollbar scrolly = new JScrollbar
(JScrollbar.HORIZONTAL, initialValue,
bubbleSize, minValue, maxValue);
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 37/55
37
Adding components to the Applet
class MyApplet extends JApplet { public void init () {
getContentPane().add(lab); // or this.getContentPane().add(lab);
getContentPane().add(but); getContentPane().add(toggle); getContentPane().add(txt);
getContentPane().add(scrolly); ...
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 38/55
38
Creating a JFrame
When you create an JApplet, The size of the applet is determined by the HTML page The browser makes the applet visible
When you write a GUI for an application, you need to do
these things yourself public class MyApplication extends JFrame {
void createMyGUI() { ... add components ... pack(); // compute the size and lay it out setVisible(true); // make the JFrame visible
}}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 39/55
39
Arranging components
Every Container has a layout manager You do not directly control where components are placed; the
layout manager does this for you
The default layout for a Panel or JPanel is FlowLayout A JApplet is a Panel; therefore, the default layout for a
JApplet is FlowLayout
The default layout for a JFrame is BorderLayout
You can set the layout manager explicitly; for example, setLayout(new FlowLayout());
setLayout(new BorderLayout());
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 40/55
40
FlowLayout
Use add(component ); to add to a component when using aFlowLayout Components are added left-to-right If no room, a new row is started
Exact layout depends on size of Applet Components are made as small as possible FlowLayout is convenient but often ugly
Note: The following code and screenshots use AWTComponents rather than Swing Components I haven’t had a chance to make new screenshots The layout managers are identical, not just similar There is no real difference with Swing instead of AWT
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 41/55
41
Complete example: FlowLayout
import java.awt.*;import java.applet.*;
public class FlowLayoutExample extends Applet {
public void init () {
setLayout (new FlowLayout ()); // default
add (new Button ("One"));
add (new Button ("Two"));
add (new Button ("Three"));
add (new Button ("Four"));
add (new Button ("Five"));
add (new Button ("Six"));}
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 42/55
42
BorderLayout
At most five components can beadded
If you want more components, add a
Panel, then add components to it.
setLayout (new BorderLayout());
add (new Button("NORTH"), BorderLayout.NORTH);
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 43/55
43
BorderLayout with five Buttons
public void init() {
setLayout (new BorderLayout ());
add (new Button ("NORTH"), BorderLayout.NORTH);
add (new Button ("SOUTH"), BorderLayout.SOUTH);add (new Button ("EAST"), BorderLayout.EAST);
add (new Button ("WEST"), BorderLayout.WEST);
add (new Button ("CENTER"), BorderLayout.CENTER);
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 44/55
44
Complete example: BorderLayout
import java.awt.*;
import java.applet.*;
public class BorderLayoutExample extends Applet {
public void init () {setLayout(new BorderLayout());
add(new Button("One"), BorderLayout.NORTH);
add(new Button("Two"), BorderLayout.WEST);
add(new Button("Three"), BorderLayout.CENTER);
add(new Button("Four"), BorderLayout.EAST);
add(new Button("Five"), BorderLayout.SOUTH);
add(new Button("Six"), BorderLayout.SOUTH);
}
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 45/55
45
Using a Panel
Panel p = new Panel();
add(p, BorderLayout.SOUTH);
p.add(new Button ("Button 1"));
p.add(new Button ("Button 2"));
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 46/55
46
GridLayout
The GridLayout manager
divides the container up into
a given number of rows and
columns:
new GridLayout(rows , columns)
All sections of the grid are equally sized and as large as
possible
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 47/55
47
Complete example: GridLayout
import java.awt.*;
import java.applet.*;
public class GridLayoutExample extends Applet {
public void init () {
setLayout(new GridLayout(2, 3));add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
add(new Button("Five"));}
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 48/55
48
Making components active
Most components already appear to do something--
buttons click, text appears
To associate an action with a component, attach a
listener to it Components send events, listeners listen for events
Different components may send different events, and
require different listeners
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 49/55
49
Listeners
Listeners are interfaces, not classes class MyButtonListener implements
ActionListener {
An interface is a group of methods that must be supplied When you say implements, you are promising to supply
those methods
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 50/55
50
Writing a Listener
For a JButton, you need an ActionListener
b1.addActionListener(new MyButtonListener ( ));
An ActionListener must have anactionPerformed(ActionEvent) method
public void actionPerformed(ActionEvent e) {…
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 51/55
51
MyButtonListener
public void init () {
...b1.addActionListener (new MyButtonListener ());
}
class MyButtonListener implements ActionListener {public void actionPerformed (ActionEvent e) {
showStatus ("Ouch!");
}
}
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 52/55
52
Listeners for JTextFields
An ActionListener listens for someone hitting the Enter key
An ActionListener requires this method: public void actionPerformed (ActionEvent e)
You can use getText( ) to get the text
A TextListener listens for any and all keys
A TextListener requires this method: public void textValueChanged(TextEvent e)
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 53/55
53
Summary I: Building a GUI
Create a container, such as JFrame or JApplet
Choose a layout manager
Create more complex layouts by adding JPanels; each
JPanel can have its own layout manager Create other components and add them to whichever
JPanels you like
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 54/55
54
Summary II: Building a GUI
For each active component, look up what kind of Listeners it can have
Create (implement) the Listeners often there is one Listener for each active component
Active components can share the same Listener
For each Listener you implement, supply the methods
that it requires
For applets, write the necessary HTML
8/2/2019 Applets n Swings-surabhi Prakash
http://slidepdf.com/reader/full/applets-n-swings-surabhi-prakash 55/55
The End