• Tidak ada hasil yang ditemukan

Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "Programming in Java"

Copied!
10
0
0

Teks penuh

(1)

Programming in Java

CBCS BHCS03

(2)

Chapter 13

I/O, Applets and other Topics

(3)

Applet Fundamentals

•  Applets are small applicaCons that are

accessed on an Internet server, transported over the Internet, automaCcally installed, and run as part of a web document

•  It has limited access to resources of its client

so that it does not run the risk of viruses or

breaching data integrity.

(4)

A Simple Applet

import java.awt.*;

import java.applet.*;

public class SimpleApplet extends Applet {

public void paint(Graphics g) {

g.drawString("A Simple Applet", 20, 20);

}

}

(5)

Applet Fundamentals

•  This applet begins with two import statements

•  The first imports the Abstract Window Toolkit (AWT) classes

•  Applets interact with the user (either directly or indirectly) through the AWT

•  AWT contains support for a window-based, graphical user interface

•  Applets can also use Swing to provide the graphical user interface

•  The second import statement imports the applet package, which contains the class Applet

•  Every applet that you create must be a subclass of Applet.

•  The next line in the program declares the class SimpleApplet. This class must be declared as public, because it will be accessed by code that is outside the program.

(6)

Applet Fundamentals

•  Inside SimpleApplet, paint( ) a method is defined by the AWT and must be overridden by the applet

•  paint( ) is called each Cme that the applet must redisplay its output. This situaCon can occur for several reasons such as:

–  the window in which the applet is running can be overwriRen by another window and then uncovered

–  the applet window can be minimized and then restored –  paint( ) is also called when the applet begins execuCon

•  Whatever the cause, whenever the applet must redraw its output, paint( ) is called

•  The paint( ) method has one parameter of type Graphics,

which contains the graphics context or , the environment in

which the applet is running. This context is used whenever

output to the applet is required

(7)

Applet Fundamentals

•   Inside paint( ) is a call to drawString( ), which is a

member of the Graphics class. This method outputs a string beginning at the specified X,Y locaCon

•  It has the following general form:

void drawString(String message, int x, int y)

•   Here, message is the string to be output beginning at x,y.

•   In a Java window, the upper-leW corner is locaCon 0,0.

•  The call to drawString( ) in the applet causes the

message “A Simple Applet” to be displayed beginning at locaCon 20,20.

•  An applet begins execuCon when the name of its class

is passed to an applet viewer or to a network browser

(8)

Applet Fundamentals

•  AWer you enter the source code for SimpleApplet, compile in the same way that you have been compiling programs

•  There are two ways in which you can run an applet:

–  ExecuCng the applet within a Java-compaCble web browser.

–  Using an applet viewer, such as the standard tool, appletviewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet.

•  To execute an applet in a web browser, you need to write a short HTML text file that contains a tag that loads the applet.

•  Here is the HTML file that executes SimpleApplet:

<applet code="SimpleApplet" width=200 height=60> </applet>

•  The width and height statements specify the dimensions of the display area used by the applet.

•  AWer you create this file, you can execute your browser and then load this file, which causes SimpleApplet to be executed.

•  To execute SimpleApplet with an applet viewer, you may also execute the HTML file shown earlier. For example, if the preceding HTML file is called RunApp.html, then the following command line will run SimpleApplet:

C:\>appletviewer RunApp.html

(9)

Applet Fundamentals

•  A more convenient method is to simply include a comment at the head of your Java source code file that contains the APPLET tag.

•  By doing so, your code is documented with a prototype of the

necessary HTML statements, and you can test your compiled applet merely by starCng the applet viewer with your Java source code

file.

•  If you use this method, the SimpleApplet source file looks like this:

/* <applet code="SimpleApplet" width=200 height=60> </

applet> */

public class SimpleApplet extends Applet { …}

•  With this approach, you can quickly iterate through applet development by using these three steps:

–  Edit a Java source file –  Compile your program

–  Execute the applet viewer, specifying the name of your applet’s source file.

(10)

Applet Fundamentals

•  The applet viewer will encounter the APPLET tag within the comment and execute your applet.

•  Here are the key points that you should remember now:

–  Applets do not need a main( ) method.

–  Applets must be run under an applet viewer or a Java- compaCble browser.

–  User I/O is not accomplished with Java’s stream I/O

classes. Instead, applets use the interface provided by

the AWT or Swing.

Referensi

Dokumen terkait

public class ProgramPertama extends MIDlet implements CommandListener { Display display; Form frmTampil; Command cmdKeluar; public ProgramPertama() { display

After reading and working through the code in this book, you'll have a deep understanding of Java 9 modularity, its features, the impact on the platform, and how you can use this

If you want to create easily understood, professional, and powerful reports from disordered, scattered data using a free, open source Java class library, this book on JasperReports

JLabel and ImageIcon • NoFce that icons are specified by objects of type Icon, which is an interface defined by Swing • The easiest way to obtain an icon is to use the ImageIcon class

The type of the event is specified by type, and the component that has been added to or removed from the container is comp • You can obtain a reference to the container that generated

AdjustmentEvent Class • There is an integer constant, ADJUSTMENT_VALUE_CHANGED, that indicates that a change has occurred • Here is one AdjustmentEvent constructor:

Color Methods • You can obtain the red, green, and blue components of a color independently using getRed , getGreen , and getBlue , shown here: int getRed int getGreen int

• “Object” means a particular item that belongs to a class – Also called an “instance” • Example String s1 = "Hello"; – Here, String is the class, and the variable s1 and the value