Chapter 3 DATA: TYPES, CLASSES, OBJECTS and IO - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Chapter 3 DATA: TYPES, CLASSES, OBJECTS and IO

Description:

An application must have a main() method. An applet has no main() method. ... applets do not have a main(), instead init() is called automatically ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 26
Provided by: spotCo
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3 DATA: TYPES, CLASSES, OBJECTS and IO


1
Chapter 3DATA TYPES, CLASSES, OBJECTS and I/O
Chapter 13 Applets and Graphics
2
Applications vs. Applets
  • The fundamental differences between Java
    applications and applets are
  • An application is a stand-alone program. An
    applet is embedded in an HTML file which is
    loaded into a browser in order for the applet to
    run.
  • An applet extends the Applet class.
  • An application must have a main() method. An
    applet has no main() method.
  • Frames are not needed for GUI applets.

3
Applets
The term applet denotes a little application.
Applets are created to run on the Internet from a
Java-enabled Web browser. Such a Web browser
contains the the Java Virtual Machine (JVM) that
includes an interpreter to translate Java byte
code into executable code for the resident
platform.
4
Creating Applets
  • The process to create a Java applet includes
  • Create a file with the applets source code and
    compile.
  • Create a HTML file with the ltAPPLETgt tag to embed
    the applet in the HTML file.
  • Load the HTML file into an applet viewer or
    browser to execute the applet.

5
Creating Applets
The init() method is called automatically when an
applet is referenced within a HTML page, or
executed. It is used to initialize the
applet. Unlike applications, applets do not have
a main(), instead init() is called automatically
when the applet is executed. Applets never use
main() and never have objects created for them.
6
Creating Applets
The java.swing package includes a class called
JApplet which we extend to create our own applet.
The JApplet superclass takes care of creating a
window with a title bar, borders, status bar, and
all the simple behaviors you would expect of a
window.
7
Standard Applet Methods
public void init() Called when the applet is
first loaded for execution and also when the
applet is reloaded. Used to initialize variables
and GUI components.
public void paint(Graphics g) Called
automatically after init() to paint graphical
components. The paint() method receives an
object from the standard Graphics class to create
applet graphics. Also called by repaint() when
the applet must be repainted.
8
Standard Applet Methods (continued)
public void start() Called after init()
and when the applet is reactivated, or when the
user moves back to the HTML page containing the
applet. Used to start any tasks, such as
animation, that must be started each time the
HTML page containing the applet is revisited.
public void stop() Called when the user
leaves the HTML page containing the applet. Used
to stop any actions, such as animation, being
performed by the applet.
9
Standard Applet Methods (continued)
public void destroy() Called after stop() only
when an applet is being removed from memory such
as when the use of the browser is terminated.
Used to release any system resources that were
allocated by the applet.
10
A Simple Applet Class
Import swing and awt packages
import javax.swing. //FOR JApplet CLASS import
java.awt. //FOR Graphics CLASS  public
class AppletWithStatus extends Japplet
public void init() //END init()
public void paint(Graphics g)
Font f new Font("Courier",Font.ITALIC
Font.BOLD,20)
g.setFont(f)  g.setColor(Color.blue) 
g.drawString("This is an applet
window",0,150)  showStatus("This is the
status bar") //END paint() //END APPLET
init() used like a constructor
Inherit JApplet
Graphics object
paint() used to produce graphics
11
An Applet with Graphic and Status
(0,0) for string graphics
(0,0) for all other graphics
String graphic
Status bar
12
Applet vs. Application
An applets paint() method is called
automatically when it is executed. In an
application, the paint() method must be called
indirectly by calling the repaint() method. This
is normally done within the application
constructor. An application frame coordinate
system places (0,0) at the upper left-hand corner
of the frame, or window, while the applet
coordinate system places (0,0) just below the
title bar of the applet.
13
Applets and HTML
The ltAPPLETgt tag is used to invoke the browsers
built-in Java interpreter to translate and
execute the Java .class file. Example ltAPPLET
codebase.. CodeChapter13Code/SimpleApplet.class
width350 height200gt lt/APPLETgt code is
followed by the applet class path within quotes.
The width and height provide the area in which
the applet is to be displayed.
Must specify the .class file and NOT the .java
file
14
GUI Applets
  • You can create GUI components and add them to an
    applet using the init() method.
  • To convert a GUI program to an applet you need to
    do the following
  • Get rid of main()
  • Have the class extend Applet instead of Frame
  • Move statements from the constructor into the
    init() method
  • Get rid of the event listener for the window

15
Graphics Class Components
How many shapes do you see?
Ovals 7 Rectangles 2 Arcs 2 String
1 Lines 1
16
Set the Graphics Object Color
//RGB COLOR, 0 NONE, 255 MAX private int red
100 private int green 150 private int blue
250 g.setColor(new Color(red,green,blue))
Using RGB values
or
Using one of the 13 Color class colors
//COLOR CLASS COLOR g.setColor(Color.cyan)
17
3-D Rectangle
Raised 3-D effect
g.setColor(new Color(100,150,250))
g.fill3DRect(50,75,100,150,true)
horizontal and vertical (x,y) coordinate of upper
left-hand corner
Width, height in pixels
18
Rounded Rectangle
Horiz.and vert.diameters of four corners
g.setColor(Color.black) g.fillRoundRect(50,250,5
0,150,30,30)
(x,y) coordinate of upper left-hand corner
Width, height in pixels
19
Filled Oval
g.setColor(Color.white) g.fillOval(75,125,50,50)

(x,y) coordinate of upper left-hand corner of
bounding rectangle
Width, height of bounding rectangle
20
Non-Filled Oval
g.setColor(Color.blue) g.drawOval(200,150,50,100
)
(x,y) coordinate of upper left-hand corner of
bounding rectangle
Width, height of bounding rectangle
21
Filled Arc
Start angle, arc angle
g.setColor(Color.black) g.fillArc(400,325,75,75,
0,135)
(x,y) coordinate of upper left-hand corner of
bounding rectangle
Width, height of bounding rectangle
22
Smiling Face Ovals Arc
g.setColor(Color.lightGray) g.fillOval(275,10,20
0,150)
g.setColor(Color.black) g.fillOval(400,60,20,10)

g.fillOval(370,90,10,10)
g.drawArc(325,90,100,50,0,-180)
g.fillOval(335,60,20,10)
23
Line
g.setColor(Color.black) g.drawLine(200,400,300,3
50)
(x,y) coordinate of start of line
(x,y) coordinate of end of line
24
String
g.setColor(Color.blue) g.setFont(new
Font("Helvetica",Font.ITALIC Font.BOLD,30)) g.d
rawString("These are color graphics in
JAVA",50,300)
(x,y) coordinate of string baseline (lower-left
corner)
25
Filled Polygon
(x,y) coordinate arrays
int x 200,300,400,400,300,200
//X-COORDINATES int y 100,200,100,300,200,300
//y-COORDINATES  g.setColor(Color.cyan)  g.fi
llPolygon(x,y,6)
Number of coordinate pairs to use from arrays
Arrays storing (x,y) coordinates
Write a Comment
User Comments (0)
About PowerShow.com