Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Applets - Java programs are divided into two main categories, applets and applications. - An application is an ordinary Java program. - An applet is a kind of Java program that can be run across the Internet. Definition: Applets are small programs that are accessed on an internet server, transported over the internet, automatically installed and run as part of a web document. An applet is a small Java program that is embedded and ran in some other Java interpreter program such as - a Java technology-enabled browser Sun’s applet viewer program called appletviewer How applet works: The Applet class Hierarchy Class Hierarchy of awt: Applet and Frame are the two main windows supported by AWT to develop user interfaces These two are the containers, which contain AWT Components with which the user can interact gives input, generate events and gets output. Components and Containers A graphical user interface is built of graphical elements called components. Components allow the user to interact with the program and provide the user to enter the input, receive the visual feedback about the state of the program. In the AWT, all user interface components are instances of class Component or one of its subclasses. Components are placed within Containers. Containers contain and control the layout of components. Containers are themselves components, and can thus be placed inside other containers. In the AWT, all containers are instances of class Container or one of its subtypes. The Container class, like Component is an abstract class. It has two direct subclasses: Panel, which is only used for grouping components; Window, which is, as its name says, for creating and handling windows. The most important example of a Panel is an Applet, but you can define as many panels as you wish. Java distinguishes two kinds of windows: dialog windows (e.g. dialogs for opening and saving files), implemented by the class Dialog, "ordinary windows," implemented by the class Frame. Panel The Panel class is mainly used for holding components. The most important example of a Panel is an Applet. Components can be laid out within a container in two ways: via layout managers via absolute positioning The second way will not be treated because it is unsuitable for applets or for platform-independent programs. Two of the many problems with absolute positioning: the size of components can be different on different platforms and the size of the browser window containing an applet is user-defined and therefore may be not well-suited for the absolute sizes of components. All these problems with laying out components in a container are not present if you use a layout manager. Types of components The AWT provides ten basic non-container component classes from which a user interface may be constructed. These classes are Button – For creating buttons Canvas – For creating surface area to place other components Checkbox – For creating check boxes CheckboxGroup – For creating radio buttons Choice –For creating dropdown list Label – For creating any String List – To display List of Items Scrollbar – For creating Scroll bars of a window(if the data is more than a screen) TextArea,- To create a text Area, to enter multiple lines of data TextField. - To create a text box to enter single line text Component and Container Hierarchy Figure shows the inheritance relationship between the user interface component classes provided by the AWT. Class Component defines the interface to which all components must adhere. Types of containers The AWT provides four container classes. They are 1) Window class and its two subclasses - class Frame and class Dialog o Window class is a high level display area (window) which can not be embedded within another container and it does not contain border and title. o Frame is the sub class of Window with Border and Title and can contain a Menu Bar. o Dialog is the sub class of Window with Border and Title used for displaying Dialog Boxes on GUI (Graphical User Interface). An instance of Dialog class cannot be displayed without an associated instance of Frame class i.e. A Dialog Box is displayed on another Frame window 2) Panel class – Applet is the immediate subclass of a Panel which is also a container o It is a generic container class for holding components Every container is assigned with a Layout Manager A Layout Manager is a Manager which represents the physical arrangement of components on the containers. AWT supports 4 types of Layout Managers 1) Flow Layout (arranges components from Left to Right and from Top to Bottom) 2) Border Layout (arranges components across the borders i.e. NORTH, SOUTH, EAST, WEST & CENTER) 3) Grid Layout (arranges components in the form of Grids i.e. rows and columns) 4) Card Layout (arranges components in the form of stack) By default Frame is Border Layout and Panel is Flow Layout. An Example: import java.awt.*; import java.applet.*; public class EgApplet extends Applet { public void paint (Graphics gp) { gp.drawString(“A simple example”, 20, 20); } } 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 applet and Applet are different names. 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 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 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 paint method 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 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 object that 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 you have painted) Example Program import java.applet.Applet; import java.awt.*; public class WelcomeApplet extends Applet { public void paint(Graphics g) { …… } } 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. 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) Setting a color Pixels 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 until we tell it some different color A pixel is a picture (pix) element one pixel is one dot on your screen there are typically 72 to 90 pixels per inch java.awt measures everything in pixels 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 The Example: import java.applet.Applet; import java.awt.*; public class WelcomeApplet 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); } } 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 ); 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=" WelcomeApplet.class" width="250" height="200"> </applet> </body> </html> Applet Security Java applets execute within a Sandbox Applets cannot access the local file system Applets cannot connect to systems other than the server from which they were downloaded Applets cannot listen for inbound network connections Applets cannot spawn processes or load local jar files Applets cannot terminate the Java VM Applets cannot change the security policy In applets, four methods are included in every applet: public void init() public void start() public void stop() public void destroy() The Applet Life Cycle init ( ) - Called when applet is loaded onto user’s machine. Prep work or one-time-only work done at this time. start ( ) - Called when applet becomes visible (page called up). Called every time applet becomes visible. stop ( ) - Called when applet becomes hidden (page loses focus). destroy ( ) - Guaranteed to be called when browser shuts down. LifecycleApplet.java import java.applet.Applet; import java.awt.Graphics; public class LifecycleApplet extends Applet { public LifecycleApplet() { System.out.println("Constructor running..."); } public void init() { System.out.println("This is init."); } public void start() { System.out.println("Applet started."); } public void paint(Graphics theGraphics) { theGraphics.drawString("Hello, World!", 0, 50); System.out.println("Applet just painted."); } public void stop() { System.out.println("Applet stopped."); } public void destroy() { System.out.println("Applet destroyed."); } } The Applet Tag < APPLET CODE= applet class file WIDTH = pixels HEIGHT = pixels [ CODEBASE = directory to look for] [ALT= alternate text] [NAME = name of current applet instance] [ALIGN = applet alignment] [VSPACE = pixels] [HSPACE = pixels] Note: 1. CODEBASE: specifies the subdirectory where the applet is to be found 2. ALT: alternate text to be displayed if the browser understands the <APPLET> tag but cannot run the applet due to some reason. 3. WIDTH, HEIGHT: the size of the applet 4. ALIGN: alignment of the applet 5. VSPACE, HSPACE: specifies the margin to be left in the vertical and horizontal directions for the applet. AWT Programming Program to create a Login Form using the container Applet which contains two labels called username and password, OK button and two text fields. import java.awt.*; import java.applet.*; /*<applet code=Applet_Demo height=500 width=500> </applet>*/ public class Applet_Demo extends Applet { Label l1,l2; Button b1; TextField t1,t2; public void init() { setLayout(null); l1=new Label("User Name" ); l2=new Label("Pass Word"); b1=new Button("OK"); t1=new TextField(" "); t2=new TextField(" "); l1.setBounds(100,150,80,20); t1.setBounds(220,150,80,20); l2.setBounds(100,200,80,20); t2.setBounds(220,200,80,20); b1.setBounds(180,260,80,20); add(l1); add(l1); add(t1); add(l2); add(t2); add(b1); } public void paint(Graphics g) { g.drawString("Login Form" ,180,100); setBackground(Color.cyan); setForeground(Color.red); } } Passing Parameters to an Applet Pass parameters from a HTML page to an applet. This allows us to modify the action of an applet without actually having to re-code or re-compile the applet. For example earlier, we wrote an applet that displayed the string "Hello World!". We may wish to re-write that applet to display Hello ABC!, or whatever name we desire at a specific location that we can change within the HTML code. To do this we use the <param> markup tag within the <applet> markup tag of the HTML page. <title> Test Applet Page </title> <hr> <applet code=HelloWorld2.class width=200 height=100> <param name=displayName value="John Smith"> <param name=startx value="50" > <param name=starty value="20" > </applet> <hr> The HelloWorld2 Applet - Allows the modification of the parameters that are displayed in the applet. import java.awt.Graphics; import java.applet.Applet; public class HelloWorld2 extends Applet { // The class states - declared here so that the parameters can be passed // from the init() method to the paint() method. These states are given // default values of World to be displayed at (10,10) private String theDisplayString = "World"; private int theStartx = 10; private int theStarty = 10; public void init() { // try to read in the parameters from the HTML file String tempName = this.getParameter("displayName"); String tempXString = this.getParameter("startx"); String tempYString = this.getParameter("starty"); // check to see if any of the parameters are blank if ((tempName!=null)&&(tempXString!=null)&&(tempYString!=null)) { // OK - none of the parameters are blank // the HTML author did their job properly this.theDisplayString = tempName; // convert the (X,Y) strings received into integer values. this.theStartx = (new Integer(tempXString)).intValue(); this.theStarty = (new Integer(tempYString)).intValue(); } } public void paint(Graphics g) { g.drawString("Hello "+theDisplayString+"!",theStartx,theStarty); } }