Unit Vii Applets & Swings

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

UNIT VII APPLETS AND SWINGS

UNIT VII APPLETS AND SWINGS


An applet is a window based program. The applet class provides the necessary support for applet.All applets are subclasses of Applet class. The applet class is defined in the java.applet package. Therefore all applets must import java.applet package ,Applets must also import java.awt package(abstract window toolkit) since all applets run in a window , it is necessary to include support for that window. Applets are not executed by the console based java runtime interpreter rather they are executed by either an applet viewer or a web browser t is the small aplication. !enerally applets are used for internet application. t can be downloaded in internet by java compatiable web browser just like an vidio , audio files. t is more secure,you can download it without any fear of virus attacks, because java is more secure. Applet class hierarchy

Two Types of Applets The first are those based directly on the Applet class. These applets are use the abstract window toolkit(A"T) to provide the graphic user interface. This style of applet has been available since java was first created The second type of applets are based on the swing class #Applet. $wing applets use the swing classes to provide the !% . $wing offers a richer and often easier&to&use user interface that does the awt. Thus, swing&based applets are now the most popular

7.1

Prepared by PHANI KISHORE ROMPICHRLA

UNIT VII APPLETS AND SWINGS

Local Applets A local applet is one that is stored on your own computer system . "hen your "eb page must find a local applet, it doesn't need to retrieve information from the nternet&in fact, your browser doesn't even need to be connected to the nternet at that time. As you can see infigure, a local applet is specified by a path name and a file name.

Remote Applets A remote applet is one that is located on another computer system ((igure ).*). This computer system may be located in the building next door or it may be on the other side of the world&it makes no difference to your #ava&compatible browser. +o matter where the remote applet is located, it's downloaded onto your computer via the nternet.

Differences between applet an application ,. Applets can be embedded in -T./ pages and downloaded over the nternet whereas Applications have no special support in -T./ for embedding or downloading. *. Applets can only be executed inside a java compatible container, such as a browser or appletviewer whereas Applications are executed at command line by java.exe or jview.exe. ). Applets execute under strict security limitations that disallow certain operations (sandbox model security) whereas Applications have no inherent security restrictions. 0. Applets don1t have the main() method as in applications. nstead they operate on an entirely different mechanism where they are initiali2ed by init(),started by start(),stopped by stop() or destroyed by destroy(). 3. Applets are designed just for handling the client site problems. while the java applications are designed to work with the client as well as server 4. Applets are not capable of reading and writing the user's file system. This means that the
7.2 Prepared by PHANI KISHORE ROMPICHRLA

UNIT VII APPLETS AND SWINGS

applet neither can access nor place anything locally. 5. Applets are not capable of communicating the server than one from which they are originating.

Lifecycle of

Applet

"hen an applet begins, the following methods are called, init() start() paint() "hen an applet is terminated, the following se6uence of method calls take place stop() destroy() init! " The init! " method is the first method to be called. This is where you should initiali2e variables. This method is called only once during the run time of your applet. start! " The start! " method is called after init! ". t is also called to restart an applet after it has been stopped. "hereas init! " is called once7the first time an applet is loaded7 start! " is called each time an applet1s -T./ document is displayed onscreen. $o, if a user leaves a web page and comes back, the applet resumes execution at start! ". paint! "
7.3 Prepared by PHANI KISHORE ROMPICHRLA

UNIT VII APPLETS AND SWINGS

The paint! " method is called each time your applet1s output must be redrawn. This situation can occur for several reasons. (or example, the window in which the applet is running may be overwritten by another window and then uncovered. 8r the applet window may be minimi2ed and then restored. paint! " is also called when the applet begins execution. "hatever the cause, whenever the applet must redraw its output, paint! " is called. The paint! " method has one parameter of type Grap#ics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is re6uired stop! " The stop! " method is called when a web browser leaves the -T./ document containing the applet7when it goes to another page, for example. "hen stop! " is called, the applet is probably running. 9ou should use stop! " to suspend threads that don1t need to run when the applet is not visible. 9ou can restart them when start! " is called if the user returns to the page . estroy! " The estroy! " method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop! " method is always called before estroy! ". E$ample Applet Pro%ram& import java.awt.:; import java.applet.:; public class Ap extends Applet < public void paint(!raphics g) < (ont f=new (ont(>arial>,(ont.?8/@,*A); g.set(ont(f); set?ackground(Bolor.!CDD+); g.draw$tring(> m your friend>,,A,,A); E E F:Gapplet code=Ap width=,AA height=,AAH GFappletH :F Passin% Parameters to Applets The AII/DT tag in -T./ allows you to pass parameters to your applet. To retrieve a parameter, use the %etParameter! " method. t returns the value of the specified parameter in the form of a Strin% object. Thus, for numeric and 'oolean values, you will need to convert their string representations into their internal formats. -ere is an example that demonstrates passing parametersJ
7.4 Prepared by PHANI KISHORE ROMPICHRLA

UNIT VII APPLETS AND SWINGS

DxampleJ import java.awt.:; import java.applet.:; public class Iaram@emo extends Applet < public void paint(!raphics g) FF @isplay parameters. < int x = nteger.parse nt(getIarameter(KaL)); int y = nteger.parse nt(getIarameter(KbL)); g.draw$tring(>sum is >M(xMy), A, ,A); E E F:Gapplet code=>Iaram@emo> width=)AA height=NAH Gparam name=a value=,AH Gparam name=b value=,0H GFappletH :F Swin%s $wing is not an acronym. The name represents the collaborative choice of its designers when the project was kicked off in late ,OO4. $wing is actually part of a larger family of #ava products known as the #ava (oundation Blasses ( #(B), which incorporate many of the features of +etscape's nternet (oundation Blasses ( (B) as well as design aspects from ?.'s Taligent division and /ighthouse @esign. $wing has been in active development since the beta period of the #ava @evelopment Pit ( #@P) ,.,, circa spring of ,OO5. $wing contains many more graphical components than its immediate predecessor, A"T. .any are components that were scribbled on programmer wishlists since #ava first debuted including tables, trees, internal frames, and a plethora of advanced text components. n addition, $wing contains many design advances over A"T. (or example, $wing introduced an Action class that makes it easier to coordinate !% components with their functionality. 9ou'll also find that a much cleaner design prevails throughout $wing; this cuts down on the number of unexpected surprises that you're likely to face while coding Swin% (eat)res $wing provides many features for writing large&scale applications in #ava. -ere is an overview of some of the more popular features. Pluggable Look-and-Feels 8ne of the most exciting aspects of the $wing classes is the ability to dictate the /Q( of each of the components, even resetting the /Q( at runtime. /Q(s have become an important issue in !% development over the past ,A years. .any users are familiar with the .otif style of user
7.5 Prepared by PHANI KISHORE ROMPICHRLA

UNIT VII APPLETS AND SWINGS

interface, which was common in "indows )., and is still in wide use on %nix platforms.
.icrosoft created a more optimi2ed /Q( in their "indows O3FONF+TF*AAA operating systems. n

addition, the .acintosh computer system has its own carefully designed /Q(, which most Apple users feel comfortable with. Lightweight Components .ost $wing components are lightweight. n the purest sense, this means that components are not dependent on native peers to render themselves. nstead, they use simplified graphics primitives to paint themselves on the screen and can even allow portions to be transparent. Additional Features $everal other features distinguish $wing from the older A"T componentsJ $wing has wide variety of new components, such as tables, trees, sliders, spinners, progress bars, internal frames, and text components. $wing components support the replacement of their insets with an arbitrary number of nested borders. $wing components can have tooltips placed over them. A tooltip is a textual pop up that momentarily appears when the mouse cursor rests inside the component's painting region. Tooltips can be used to give more information about the component in 6uestion. 9ou can arbitrarily bind keyboard events to components, defining how they react tovarious keystrokes under given conditions. There is additional debugging support for rendering your own lightweight $wing components T#e *o el+View+,ontroller Arc#itect)re $wing uses the model-view-controller architecture (.RB) as the fundamental design behind each of its components. Dssentially, .RB breaks !% components into three elements. Dach of these elements plays a crucial role in how the component behaves. Model The model encompasses the state data for each component. There are different models for different types of components. (or example, the model of a scrollbar component might contain information about the current position of its adjustable >thumb,> its minimum and maximum values, and the thumb's width (relative to the range of values). A menu, on the other hand, may simply contain a list of the menu items the user can select from. This information remains the same no matter how the component is painted on the screen; model data is always independent of the component's visual representation. View The view refers to how you see the component on the screen. (or a good example of how views can differ, look at an application window on two different !% platforms. Almost all window
7.6 Prepared by PHANI KISHORE ROMPICHRLA

UNIT VII APPLETS AND SWINGS

frames have a title bar spanning the top of the window. -owever, the title bar may have a close box on the left side (like the .ac 8$ platform), or it may have the close box on the right side (as in the "indows platform). These are examples of different types of views for the same window object. Controller The controller is the portion of the user interface that dictates how the component interacts with events. Dvents come in many forms 7 e.g., a mouse click, gaining or losing focus, a keyboard event that triggers a specific menu command, or even a directive to repaint part of the screen. The controller decides how each component reacts to the event7if it reacts at all.
E$ample of Swin% pro%ram& import java.awt.:; import javax.swing.:; import java.awt.event.:; public class #?@emo extends #Applet implements Action/istener < #?utton b,,b*; public void init() < Bontainer c=getBontentIane(); c.set/ayout(new (low/ayout((low/ayout.BD+TDC)); b,=new #?utton(KyesL); b*=new #?utton(KnoL); c.add(b,); c.add(b*); b,.addAction/istener(this); b*.addAction/istener(this); E public void actionIerformed(ActionDvent e) < show$tatus(e.getActionBommand()); E E F:Gapplet code=L#?@emo.classL width=)AA height=*3AH:F outputJ

7.7

Prepared by PHANI KISHORE ROMPICHRLA

You might also like