Applets - lev' 2

860 views

Published on

A Detailed Discription of Applet Programing

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
860
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Applets - lev' 2

  1. 1. A Simple Applet
  2. 2. <ul><li>Rakesh T </li></ul><ul><li>S1 MCA </li></ul><ul><li>Vidya Academy Of </li></ul><ul><li>Science And Technology </li></ul>
  3. 3. Applets and applications <ul><li>An applet is a Java program that runs on a web page </li></ul><ul><ul><li>Applets can be run from: </li></ul></ul><ul><ul><ul><li>Internet Explorer </li></ul></ul></ul><ul><ul><ul><li>Netscape Navigator (sometimes) </li></ul></ul></ul><ul><ul><ul><li>appletviewer </li></ul></ul></ul><ul><li>An application is a Java program that runs all by itself </li></ul>
  4. 4. Packages and classes <ul><li>Java supplies a huge library of pre-written “code,” ready for you to use in your programs </li></ul><ul><li>Code is organized into classes </li></ul><ul><li>Classes are grouped into packages </li></ul><ul><li>One way to use this code is to import it </li></ul><ul><li>You can import a single class, or all the classes in a package </li></ul>
  5. 5. The Applet class <ul><li>To create an applet, you must import the Applet class </li></ul><ul><ul><li>This class is in the java.applet package </li></ul></ul><ul><li>The Applet class contains code that works with a browser to create a display window </li></ul><ul><li>Capitalization matters! </li></ul><ul><ul><li>applet and Applet are different names </li></ul></ul>
  6. 6. Importing the Applet class <ul><li>Here is the directive that you need: </li></ul><ul><ul><li>import java.applet.Applet; </li></ul></ul><ul><li>import is a keyword </li></ul><ul><li>java.applet is the name of the package </li></ul><ul><li>A dot ( . ) separates the package from the class </li></ul><ul><li>Applet is the name of the class </li></ul><ul><li>There is a semicolon ( ; ) at the end </li></ul>
  7. 7. The java.awt package <ul><li>“ awt” stands for “Abstract Window Toolkit” </li></ul><ul><li>The java.awt package includes classes for: </li></ul><ul><ul><li>Drawing lines and shapes </li></ul></ul><ul><ul><li>Drawing letters </li></ul></ul><ul><ul><li>Setting colors </li></ul></ul><ul><ul><li>Choosing fonts </li></ul></ul><ul><li>If it’s drawn on the screen, then java.awt is probably involved! </li></ul>
  8. 8. Importing the java.awt package <ul><li>Since you may want to use many classes from the java.awt package, simply import them all: </li></ul><ul><ul><li>import java.awt.*; </li></ul></ul><ul><li>The asterisk, or star ( * ), means “all classes” </li></ul><ul><li>The import directives can go in any order, but must be the first lines in your program </li></ul>
  9. 9. C and C++ programmers only <ul><li>C and C++ have an #include directive that copies a library function into your program </li></ul><ul><li>This makes your program bigger </li></ul><ul><li>Java’s import gives you access to the library </li></ul><ul><li>It does not make your program bigger </li></ul><ul><li>It’s OK to use lots of include directives! </li></ul>
  10. 10. The applet so far import java.applet.Applet; import java.awt.*;
  11. 11. Comments <ul><li>A comment adds information for the reader </li></ul><ul><li>Java ignores everything inside comments </li></ul><ul><li>There are three kinds of comments: </li></ul><ul><ul><li>// starts a comment that goes to the end of the line </li></ul></ul><ul><ul><li>/* starts a comment that can extend over many lines, and ends at */ </li></ul></ul><ul><ul><li>/** is a “javadoc” comment that can be extracted from your program and used in documentation */ </li></ul></ul>
  12. 12. Classes <ul><li>In Java, all code occurs in classes </li></ul><ul><ul><li>Except for the package and import directives </li></ul></ul><ul><ul><li>We will talk about package some day </li></ul></ul><ul><li>The code that you import is in classes </li></ul><ul><li>Your code will also be in classes </li></ul><ul><li>For now, a class is a bundle of code </li></ul><ul><ul><li>We will talk about what it really is very soon </li></ul></ul>
  13. 13. Your first class <ul><li>public class Drawing extends Applet { …the code for your class goes in here… } </li></ul><ul><li>public says your class is not hidden </li></ul><ul><ul><li>This makes your class visible to BlueJ </li></ul></ul><ul><ul><li>We will talk later about why we hide code </li></ul></ul><ul><li>class says we are making a class (Duh!) </li></ul>
  14. 14. Your first class, part 2 <ul><li>public class Drawing extends Applet { … } </li></ul><ul><li>Drawing is the name of your class </li></ul><ul><ul><li>Class names should always be capitalized </li></ul></ul><ul><li>extends Applet says that our Drawing is a kind of Applet , but with added capabilities </li></ul><ul><ul><li>Java’s Applet just makes an empty window </li></ul></ul><ul><ul><li>We are going to draw in that window </li></ul></ul>
  15. 15. Your first class, part 3 <ul><li>public class Drawing extends Applet { …the code for your class goes in here… } </li></ul><ul><li>The braces, { } , mark the beginning and ending of your code </li></ul>
  16. 16. The applet so far import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { … we still need to put some code in here... }
  17. 17. Methods <ul><li>A method is a group of commands that tell the computer to do something </li></ul><ul><ul><li>C programmers: methods are similar to functions </li></ul></ul><ul><li>A method takes information in, does something with it, and returns a result </li></ul><ul><ul><li>The input information is called the method’s parameters , or arguments </li></ul></ul><ul><ul><li>The result is just called a result </li></ul></ul>
  18. 18. The paint method <ul><li>Our applet is going to have a method to paint some colored rectangles on the screen </li></ul><ul><li>This method must be named paint </li></ul><ul><li>paint needs to be told where on the screen it can draw </li></ul><ul><ul><li>this will be the only parameter it needs </li></ul></ul><ul><li>paint doesn’t return any result </li></ul>
  19. 19. The paint method, part 2 <ul><li>public void paint(Graphics g) { … } </li></ul><ul><li>public says that anyone can use this method </li></ul><ul><li>void says that it does not return a result </li></ul><ul><li>paint is the name of the method </li></ul><ul><li>The argument (there’s only one) is inside parentheses </li></ul><ul><li>The method’s commands are inside braces </li></ul>
  20. 20. By the way…names <ul><li>( ) are parentheses </li></ul><ul><li>{ } are braces </li></ul><ul><li>[ ] are brackets </li></ul>
  21. 21. The paint method, part 3 <ul><li>public void paint(Graphics g) { … } </li></ul><ul><li>A Graphics is something that holds information about a painting </li></ul><ul><ul><li>It remembers what color you are using </li></ul></ul><ul><ul><li>It remembers what font you are using </li></ul></ul><ul><ul><li>You can “paint” on it, and it remembers what you have painted </li></ul></ul>
  22. 22. Classes and objects <ul><li>A class is a description of some objects </li></ul><ul><li>An object is a member of a class </li></ul><ul><ul><li>The type of an object is the class it belongs to </li></ul></ul><ul><ul><li>Classes are more abstract than objects </li></ul></ul><ul><li>If I have a dog named Fido, I can’t pet “dog,” but I can pet “Fido” </li></ul><ul><ul><li>Fido is an object of type Dog </li></ul></ul>
  23. 23. The paint method, part 4 <ul><li>public void paint(Graphics g) { … } </li></ul><ul><li>g is the parameter, or argument </li></ul><ul><ul><li>we could use any name we wanted for it </li></ul></ul><ul><ul><li>but it should not begin with a capital letter </li></ul></ul><ul><li>The type of a name tells what kind of thing the name refers to </li></ul><ul><li>Graphics g says g is an object of type Graphics </li></ul><ul><li>We can paint on g </li></ul>
  24. 24. The applet so far import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { public void paint(Graphics g) { …we still need to put some code in here… } }
  25. 25. Colors <ul><li>The java.awt package defines a class named Color </li></ul><ul><li>There are 13 predefined colors--here are their fully-qualified names: </li></ul><ul><li>For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black , Color.darkGray , etc. </li></ul>Color.BLACK Color.PINK Color.GREEN Color.DARK_GRAY Color.RED Color.CYAN Color.GRAY Color.ORANGE Color.BLUE Color.LIGHT_GRAY Color.YELLOW Color.WHITE Color.MAGENTA
  26. 26. New colors <ul><li>Every color is a mix of red, green, and blue </li></ul><ul><li>You can make your own colors: new Color( red , green , blue ) </li></ul><ul><li>Amounts range from 0 to 255 </li></ul><ul><li>Black is (0, 0, 0), white is (255, 255, 255) </li></ul><ul><li>We are mixing lights, not pigments </li></ul><ul><li>Yellow is red + green, or (255, 255, 0) </li></ul>
  27. 27. Setting a color <ul><li>To use a color, we tell our Graphics g what color we want: </li></ul><ul><ul><li>g.setColor(Color.RED); </li></ul></ul><ul><li>g will remember this color and use it for everything until we tell it some different color </li></ul>
  28. 28. The paint method so far public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle… g.setColor(Color.RED); …draw another rectangle… } }
  29. 29. Pixels <ul><li>A pixel is a picture ( pix ) el ement </li></ul><ul><ul><li>one pixel is one dot on your screen </li></ul></ul><ul><ul><li>there are typically 72 to 90 pixels per inch </li></ul></ul><ul><li>java.awt measures everything in pixels </li></ul>
  30. 30. Java’s coordinate system <ul><li>Java uses an (x, y) coordinate system </li></ul><ul><li>(0, 0) is the top left corner </li></ul><ul><li>(50, 0) is 50 pixels to the right of (0, 0) </li></ul><ul><li>(0, 20) is 20 pixels down from (0, 0) </li></ul><ul><li>(w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height </li></ul>(0, 0) (0, 20) (50, 0) (50, 20) (w-1, h-1)
  31. 31. Drawing rectangles <ul><li>There are two ways to draw rectangles: </li></ul><ul><li>g.drawRect( left , top , width , height ); </li></ul><ul><li>g.fillRect( left , top , width , height ); </li></ul>
  32. 32. The complete applet import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing 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); } }
  33. 33. Some more java.awt methods <ul><li>g.drawLine( x1 , y1 , x2 , y2 ); </li></ul><ul><li>g.drawOval( left , top , width , height ); </li></ul><ul><li>g.fillOval( left , top , width , height ); </li></ul><ul><li>g.drawRoundRect( left , top , width , height ); </li></ul><ul><li>g.fillRoundRect( left , top , width , height ); </li></ul><ul><li>g.drawArc( left , top , width , height , startAngle , arcAngle ); </li></ul>
  34. 34. Drawing Strings <ul><li>A String is a sequence of characters enclosed in double quote marks </li></ul><ul><ul><li>&quot;Hello, World!&quot; </li></ul></ul><ul><li>A double quote mark in a String must be preceded by a backslash ( ) </li></ul><ul><ul><li>&quot;He said, &quot;Please don't go.&quot;&quot; </li></ul></ul><ul><li>g.drawString( string , x , y ); </li></ul>
  35. 35. The End

×