Applet Example 1 Applets and applications An applet
- Slides: 35
Applet Example 1
Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run modern Java applets, old browsers need an up-to-date Java plugin appletviewer is a program that can run An application is a Java program that runs all by itself 2
Applet An applet is a program that comes from server into a client and gets executed at client side and displays the result. An applet represents byte code embedded in a html page. (applet = bytecode + html) and run with the help of Java enabled browsers such as Internet Explorer. An applet is a Java program that runs in a browser. Unlike Java applications applets do not have a main () method. To create applet we can use java. applet. Applet. 3
Applet 4
Packages and classes Java supplies a huge library of pre-written “code, ” ready for you to use in your programs Code is organized into classes Classes are grouped into packages One way to use this code is to import it You can import a single class, or all the classes in a package 5
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 Capitalization matters! applet and Applet are different names 6
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 There is a semicolon ( ; ) at the end 7
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 If it’s drawn on the screen, then java. awt is probably involved! 8
Importing the java. awt package 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 import directives can go in any order, but must be the first lines in your program 9
The applet so far import java. applet. Applet; import java. awt. *; 10
Your applet class public class Drawing extends Applet { … } Drawing is the name of your class Class names should always be capitalized extends Applet says that our Drawing is a kind of Applet, but with added capabilities Java’s Applet just makes an empty window We are going to draw in that window The only way to make an applet is to extend Applet 11
The applet so far import java. applet. Applet; import java. awt. *; public class Drawing extends Applet { …we still need to put some code in here. . . } 12
The paint method Our applet is going to have a method to paint some colored rectangles on the screen This method must be named 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 13
The paint method, part 2 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) 14
The applet so far import java. applet. Applet; import java. awt. *; public class Drawing extends Applet { public void paint(Graphics g) { …we still need to put some code in here… } } 15
Colors The java. awt package defines a class named Color There are 13 predefined colors—here are their fully-qualified names: Color. BLACK Color. DARK_GRAY Color. LIGHT_GRAY Color. WHITE Color. PINK Color. RED Color. ORANGE Color. YELLOW Color. MAGENTA Color. GREEN Color. CYAN Color. BLUE For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color. black, Color. dark. Gray, etc. 16
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) We are mixing lights, not pigments Yellow is red + green, or (255, 0) 17
Setting a color To use a color, we tell our Graphics g what color we want: g. set. Color(Color. RED); g will remember this color and use it for everything until we tell it some different color 18
The paint method so far public void paint(Graphics g) { g. set. Color(Color. BLUE); …draw a rectangle, write any string… g. set. Color(Color. RED); …draw another rectangle write any string… } } 19
Pixels 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 20
Java’s coordinate system (0, 0) (50, 0) (0, 20) (50, 20) (w-1, h-1) 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 21
Drawing rectangles There are two ways to draw rectangles: g. draw. Rect( left , top , width , height ); g. fill. Rect(left , top , width , height ); 22
The complete applet import java. applet. Applet; import java. awt. *; public class Drawing extends Applet { public void paint(Graphics g) { g. set. Color(Color. BLUE); g. fill. Rect(20, 50, 30); g. set. Color(Color. RED); g. fill. Rect(50, 30, 50, 30); } } 23
Some more java. awt methods g. draw. Line( x 1 , y 1 , x 2 , y 2 ); g. draw. Oval( left , top , width , height ); g. fill. Oval( left , top , width , height ); g. draw. Round. Rect( left , top , width , height ); g. fill. Round. Rect( left , top , width , height ); g. draw. Arc( left , top , width , height , start. Angle , arc. Angle ); g. draw. String( string , x , y ); 24
The HTML page You can only run an applet in an HTML page The HTML looks something like this: <html> <body> <h 1>Drawing. Applet</h 1> <applet code="Drawing. Applet. class" width="250" height="200"> </applet> </body> </html> Blue. J will create this HTML for you 25
26
Programme-2 import java. applet. *; import java. awt. *; public class Hello. World extends Applet { Compiling and Executing Program: public void init() { javac Hello. World. java resize(150, 25); appletviewer test. Hello. World. html }//init public void paint(Graphics g) { g. set. Font(new Font("Helvetica", Font. PLAIN, 8)); g. draw. String("Hello world!", 50, 25); }//paint }//Hello. World 27
Programme-1 28
Programme-1 import java. applet. *; import java. awt. *; public class Drawing. Lines extends Applet { int width, height; public void init() { width = get. Size(). width; height = get. Size(). height; set. Background( Color. black ); } public void paint( Graphics g ) { g. set. Color( Color. green ); / 10, 0 ); } }} for ( int i = 0; i < 10; ++i ) { g. draw. Line( width, height, i * width 29
Basic Methods(1/2) Methods for Milestones init() – initialize the applet start() – start the applet’s execution stop() – stop the applet’s execution destroy() – perform a final cleanup 30
Basic Methods(2/2) Typical Structure import java. applet. Applet; import java. awt. Graphics; public class Simple extends Applet {. . . public void init() {. . . } public void start() {. . . } public void stop() {. . . } public void destroy() {. . . } public void paint(Graphics g) {. . . } } 31 <Applet Code> <APPLET CODE=. . . CODEBASE=. . . WIDTH=. . . HEIGHT=. . . >. . . </APPLET> <HTML Code>
Example Source Code import java. applet. Applet; import java. awt. Graphics; public class Hello. World extends Applet { public void paint(Graphics g) { g. draw. String("Hello", 50, 20); } } Hello. World. java <APPLET CODE="Hello. World. class“ WIDTH=200 HEIGHT=140> </APPLET> Hello. World. htm 32 Result
Programme public class Set. Graphics. Color. Example extends Applet{ public void paint(Graphics g) { /* * Graphic objects like lines and rectangles uses current * foreground color. * To change the current graphic color use * void set. Color(Color c) method of Graphics Class. */ //this will create light blue color Color custom. Color = new Color(10, 255); g. set. Color(custom. Color); g. draw. Line(10, 30, 30); g. set. Color(Color. red); g. fill. Rect(40, 40, 40); g. set. Color(Color. green); g. fill. Rect(80, 40, 40); g. draw 3 DRect(81, 40, true); } }
Program 1: Write an applet program with a message and display the message in paint () method. /* <applet code="My. Applet. class" width = 600 height= 450> </applet> */
Program
- Difference between local applet and remote applet
- Import java.applet.applet
- Import java.applet.applet
- Tanslate
- G.draw
- Import java.applet.applet
- Bagaimana cara menjalankan applet melalui applet viewer?
- 자바 애플릿 실행
- Wisweb applets
- Wis web
- Reese's pieces simulation
- Polling applets
- Applet java ejemplo
- Applet example
- Difference between applet and awt
- Steps involved in developing and running a local applet
- Difference between swing and applet
- Life cycle of applet
- Onlinestatbook sampling distributions applet
- Syntax of applet tag in java
- A term that applies to a program or applet
- Test java applet
- Box plot applet
- Java applet program draw cartoon
- Statistical investigation process
- Aj applet
- Theory based inference applet
- Theory-based inference applet
- Standing wave applet
- Reeses pieces applet
- Gbn applet
- Avl tree applet
- Applet wiskunde
- Simple random sample applet
- Fft applet
- Hierarchy of applet