Title: COM214J2: Introduction to Programming Applets
1COM214J2 Introduction to ProgrammingApplets
- Sarabjot Singh Anand
- 16E17
- ss.anand_at_ulster.ac.uk
- http//ijsr32.infj.ulst.ac.uk/e95346/COM214J2/ind
ex.html
2Applets Vs Applications
- Java applications are run by the Java Interpreter
that - loads the applications main class
- loads other classes as required
- Java applets runs within an Internet browser that
supports Java - it is a graphics window which can be controlled
by a Java program - makes Internet content interactive
- must be included in an HTML page using HTML tags
just like an image or text that needs to appear
in the web page - when the page is loaded, the applet is run by the
Java interpreter built into the browser
3(No Transcript)
4lthtmlgt ltheadgt lttitlegtWatch Appletlt/titlegt lt/headgt
ltbody bgcolor"996633"gt ltpgtThe current
timeltbrgt ltobject height"50" width350"
classid"clsid8AD9C840-044E-11D1-B3E9-00805F499D9
3" codebase"http//java.sun.com/products/plugin/a
utodl/jinstall-1_4_0-win.cab"gt ltparam name"Code"
valueNewWatch.classgt This program requires a
Java-enabled browser. lt/objectgt lt/bodygt lt/htmlgt
5The Applets Environment
- As applets run within a browser
- There is an existing window for the applet to run
in - A place to display graphics and receive
information - A predefined interface with the Browser
- Can react to events such as the page reloading
- An applet is a subclass of JApplet (javax.swing
package) or Applet (which is the superclass of
JApplet in the java.applet package) - public class myApplet extends javax.swing.JApplet
//java code
6Anatomy of an Applet
- Does not contain a main method
- The browser creates an instance of the applet
class and calls different methods whenever
specific events occur - Specific methods in an applet
- init()
- start()
- stop()
- destroy()
- paint()
- These methods are defined in the Applet class or
its superclasses but can be overridden in your
applet
7Method OverriddingHow does Java know which paint
method to use?
paint()
init() start() stop() destroy()
8Paint
- This is a key method that displays the applet on
the screen - Painting occurs whenever the browser window is
- initialised
- brought out from behind a page
- Resized
- ..
- Explicitly requested by the repaint() method call
- The method to override is defined as public void
paint(Graphics g)
9The paint method
- This method must be overridden as it defines what
must be shown in the java applet you are writing - Called by the browser whenever the applet is to
be redrawn - The parameter to the method is an instance of the
class Graphics - The object is created by the browser and passed
to the paint method - Hence the applet must import the
java.awt.Graphics class
10The Graphics object
- Represents the area being drawn to
- The Applet Window
- It is the equivalent of a blank canvas upon which
the paint method draws graphics like lines,
rectangle,. - Object used to draw text and other graphical
components in the applet window
11Co-ordinates on the Applet Window
(35,0)
(0,0)
(0,30)
(35,30)
12Methods provided by the Graphics Object
- drawString(String text, int xvalue, int yvalue)
- g.drawString(My Applet, 30,40 )
- drawLine(int xStart, int yStart,int xFinish, int
yFinish) - g.drawLine(30,40,60,40)
- drawRect(int xStart, int yStart, int width, int
height) - g.drawRect(25,35,40,10)
- setColor(Color myColor)
- g.setColor(Color.black)
- setFont(Font myFont)
- g.setFont(new Font(Arial, Font.BOLD,10))
Upper Left corner of String on Canvas
13import java.applet. import java.awt. public
class ColorandFontDemo extends Applet
public void paint(Graphics g) // paint
method Font f new Font3 // an
array to hold 3 fonts f0 new
Font("TimesRoman",Font.BOLD,12) f1
new Font("Courier", Font.ITALIC,16) f2
new Font("Arial",Font.PLAIN,20)
g.setColor(Color.green) // set the colour to
green g.setFont(f0) // choose the
first font g.drawString(First Green
Applet Message, 10 ,20) // display
message at (x10,y20)
g.setColor(Color.red) // change colour to
red g.drawLine(20,30, 200, 250) // draw
a straight red line from (20,30) to (200,250)
g.setColor(Color.yellow) // change colour
to yellow g.drawRect(30, 40, 200, 150)
// draw yellow rectangle 200 wide, 150 high with
top left corner at (30,40)
g.setColor(Color.blue) // change colour to
blue g.setFont(f1) // choose second
font g.drawString(Second Blue Message,
200 ,250) // display message starting at
(10,350)
14Viewing the Applet
ltHTMLgt ltAPPLET code"ColorandFontDemo.class"
width400 height300gt lt/APPLETgt lt/HTMLgt