Introduction to Java

Download Report

Transcript Introduction to Java

Java Applets

A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia

Java Applets

 Introduction to Applets  Applet Development  Graphics

Java Applications and Applets

 In Java, you can develop applications and applets  Applications can run by themselves  Applets are executed in a controlled environment, usually within web browsers  Let us see some examples of applets embedded in a web page

Some Applets

 Hilo Game:  http://mainline.brynmawr.edu/Courses/cs11 0/fall2003/Applets/HiLo/Hi.html

 Hangman Game:  http://www.bodo.com/Applets/Hangman/ind ex.html

Applet Mechanism

 Java applets are programs that are stored with a web page  When a user requests that web page, the applet embedded with it is sent from the server to the user’s computer  Then the applet is executed in a “sandbox” preventing it from corrupting the user’s computer

Applet Development

 There are two distinct phases in applet development  The first phase is concerned with the development of the actual Java applet  The second phase is about embedding the applet in a web page  Let us try to change the programs developed earlier to applets

Application Source Code

    import javax.swing.JOptionPane; public class myfirst { public static void main(String[] args) { String input = JOptionPane.showInputDialog("What is your name?");  JOptionPane.showMessageDialog(null,"Good Morning "+input,"Greeting Window",JOptionPane.ERROR_MESSAGE);   System.exit(0); } }

Converting to Applet

 Add the following line at the top:  import javax.swing.JApplet;  Add “extends JApplet” after the class name  Rename the main( ) to myfirst( ). There is no main( ) method in an applet. Use public myfirst( ) instead of the long list of prequalifiers in the method title

Constructors

 This new method “public myfirst( )” bears the name of the class to which it belongs  Such methods are known as constructors. They are very useful for initializing a program  Constructors come in handy when there is no main( ) method in a class

Embedding an applet inside a web page

 Web pages are written in a language called HTML (HyperText Markup Language)  HTML provides tags and their attributes for formatting the text  Following table shows the common tags used in web pages

Table 9.1 Common tags Beginning Tag

--------------- <H1 or H2…> <B> <I> <U> <SUB> <SUP> <CENTER> <BR> <OL> <UL> <LI> <IMG> <A></b></p> <p><b><i>Ending Tag</i></b></p> <p><b>--------------- </HTML> </HEAD> </BODY>

Meaning

--------------------------- document document head document body document title different header levels boldface Italic underlined subscript superscript centered line break ordered list unordered list an item in the list an image an address (hyperlink)

Program 9.4

HTML Program

Sample Document This is the photo of a race:

Tags and Attributes

    In the HTML file (or the web page) shown on the previous slide, a tag named img is used The attributes of img are “src” and “align” The “src” attribute points to the source of the image The “align” attribute lets us select left, center or middle position for the image  Try to save the HTML file and load it in the web browser. The image file is provided through a separate URL on the course homepage

A Web Page that embeds our applet

 The description seen by inept browsers

Explanation

  In this web page, we use a tag named “applet” The attributes of this tag include the width and height of the applet window and code file name  Remember that the code file name used here is “myfirst.class” instead of “myfirst.java”  This is because we need a compiled and ready to run program as an applet   Please save this web page in a .html file Compile the applet and then load the .html file to see it run  Demo required

Graphics

 We can draw various shapes inside an applet window  A specific method named paint( ) is added to the applet in order to draw shapes  Let us try to draw a rectangle  Java defines a class Rectangle that can be used directly

Using Rectangle Class

 Add the following import line at the top  import java.awt.Rectangle;  Define a rectangle in the program as follows:   Rectangle book1 = new Rectangle(20,30,200,120) The parameters are left top ‘x’ and ‘y’ followed by ‘width’ and ‘height’  In a Java graphics window, x increases left to right and y increases top to bottom

Keeping it Simple

 In order to keep it simple and manageable, we replace the constructor method by paint method  Replace the title line – public myfirst( )  By this line – public void paint(Graphics g)

Inside the Paint method

 Inside the paint() method, simply add the following line – Graphics2D g2 = (Graphics2D)g;  The above line is required in order to use advanced 2-D graphics features   Next insert the rectangle line as on slide 17 Call the draw method to draw the rectangle – g2.draw(book1);  You may use fill method to fill the box with the current color  Demo required for fill method

Changing Colors

 The default fill color is black but we can change the colors !!!

 Add the following import line at the top – Import java.awt.Color;  Define a new color with the following line inside the program just before the fill call – Color Fillcolor = new Color(1.0f, 0.2f, 0.3f);  Here the three floating point numbers represent the strengths of red, green and blue in fillcolor  Now set the current color with the following line – g2.setColor(fillcolor);  Demo: run the program, make at least two new colors and run again

Drawing Lines

 Add the following line at the top of the program: – import java.awt.geom.Line2D;   Draw the line with the following statement – g2.draw(bar);  Note how carefully we have placed the line below the shape drawn earlier  Define a new line – Line2D.Double bar = new Line2D.Double(20,200,220,200); Programming Exercise: Draw the front of a colonial home with vaulted ceiling, front entrance door and one window. Fill the door and window with colors as desired