Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.

Slides:



Advertisements
Similar presentations
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
Chapter 2 - Introduction to Java Applications
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
CMT Programming Software Applications
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
1 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
 2002 Prentice Hall. All rights reserved. 1 Week 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Chapter 2 - Introduction to Java Applications
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 24 - Introduction to Java Applications and Applets Outline 24.1Introduction 24.2Basics of a Typical.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 24 - Introduction to Java Applications and Applets.
1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software Development Kit The TicTacToe Applet.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
JAPPLET.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
 Pearson Education, Inc. All rights reserved Introduction to Java Applets.
Chapter 3 Introduction to Java Applets 1 3 There is no main method in a Java Applet. A Java Applet can only run in a browser. An Applet is run.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
Java I--Copyright © Tom Hunter. Chapter 3 Introduction to Java Applets.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Review of CIS 120 Concepts: What you said you want….
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.3Sample.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Creating a Java Application and Applet
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CS202 Java Object Oriented Programming GUI Programming – Applets Chengyu Sun California State University, Los Angeles.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Chapter No. : 1 Introduction to Java.
2.5 Another Java Application: Adding Integers
Introduction to Java Applets
Chapter 3 - Introduction to Java Applets
Java Applets.
Java I.
Chapter 2 - Introduction to Java Applications
Chapter 24 - Introduction to Java Applications and Applets
Java Applets.
Chapter 3 - Introduction to Java Applets
Applet 2019/4/23.
Presentation transcript:

Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing a String & Lines Faculty of Engineering & IT Software Engineering Department Eng.Omar Al-Nahal Eng.Omar Al-Nahal

Introduction to Java Applets Applets, Web page, Client, Server

Introduction to Java Applets b Applet Program that runs inProgram that runs in –appletviewer (test utility for applets) –Web browser (IE, Communicator) Executes when HTML document containing applet is openedExecutes when HTML document containing applet is opened b Sample Applets Provided in Java 2 Software Development Kit (J2SDK)Provided in Java 2 Software Development Kit (J2SDK) Source code included (.java files)Source code included (.java files)  Running applets In command prompt, change to subdirectory of applet cd directoryName In command prompt, change to subdirectory of applet cd directoryName There will be an HTML file used to execute applet There will be an HTML file used to execute applet type appletviewer example1.html type appletviewer example1.html Applet will run, Reload and Quit commands under Applet menu Applet will run, Reload and Quit commands under Applet menu

A Simple Java Applet: Drawing a String b Create our own applet Print "Welcome to Java Programming!"Print "Welcome to Java Programming!" import javax.swing.JAppletimport javax.swing.JApplet –Needed for all applets import java.awt.Graphicsimport java.awt.Graphics –Allows program to draw graphics (lines,text) on an applet Like applications, applets have at least one class definitionLike applications, applets have at least one class definition b Rarely create applets from scratch Use of class existing definitionsUse of class existing definitions public class WelcomeApplet extends JApplet { extends ClassName - class to inherit fromextends ClassName - class to inherit from –In this case, inherit from class JApplet

A Simple Java Applet: Drawing a String b Classes Templates/blueprints create or instantiate objectsTemplates/blueprints create or instantiate objects –Objects - locations in memory to store data –Implies that data and methods associated with object b Methods paint, init, and start called automatically for all appletspaint, init, and start called automatically for all applets –Get "free" version when you inherit from JApplet –By default, have empty bodies

A Simple Java Applet: Drawing a String  Method paint Used to draw graphics, define:Used to draw graphics, define: public void paint( Graphics g ) –Takes a Graphics object g as a parameter –For now, all method definitions begin with public Call methods of object g to draw on appletCall methods of object g to draw on applet drawString("String to draw", x, y); –Draws "String to draw" at location ( x, y ) –Coordinates specify bottom left corner of string –( 0, 0 ) is upper left corner of screen –Measured in pixels (picture elements)

A Simple Java Applet: Drawing a String  Create the HTML file (.html or.htm) Many HTML codes (tags) come in pairsMany HTML codes (tags) come in pairs Create tags with tags insideCreate tags with tags inside appletviewer only understands tagsappletviewer only understands tags –Minimal browser –Specify complied.class file, width, and height of applet (in pixels) –Close tag with –Close tag with b Running the applet appletviewer WelcomeApplet.html

1// Fig : WelcomeApplet.java 2// A first applet in Java 3import javax.swing.JApplet; // import class JApplet 4import java.awt.Graphics; // import class Graphics 5 6public class WelcomeApplet extends JApplet { 7 public void paint( Graphics g ) 8 { 9 g.drawString( "Welcome to Java Programming!", 25, 25 ); 10 } 11} Example – Welcome Applet super.paint( g (

Two More Simple Applets: Drawing Strings and Lines  Other methods of class Graphics No concept of lines of text, as in System.out.println when drawing graphicsNo concept of lines of text, as in System.out.println when drawing graphics To print multiple lines, use multiple drawString callsTo print multiple lines, use multiple drawString calls drawLine( x1, y1, x2, y2 ) ;drawLine( x1, y1, x2, y2 ) ; –Draws a line from ( x1, y1 ) to ( x2, y2 )

1// Fig : WelcomeLines.java 2// Displaying text and lines 3import javax.swing.JApplet; // import class JApplet 4import java.awt.Graphics; // import class Graphics 5 6public class WelcomeLines extends JApplet { 7 public void paint( Graphics g ) 8 { 9 g.drawLine( 15, 10, 210, 10 ); 10 g.drawLine( 15, 30, 210, 30 ); 11 g.drawString( "Welcome to Java Programming!", 25, 25 ); 12 } 13} Simple Applets: Drawing Strings and Lines super.paint( g (

Another Java Applet: Adding Integers b Next applet mimics program to add two integers This time, use floating point numbersThis time, use floating point numbers –Can have decimal point, –float - single precision floating point number. –double - approximately double precision floating point number. –Uses more memory Use showInputDialog to get input, as beforeUse showInputDialog to get input, as before Use Double.parseDouble( String)Use Double.parseDouble( String) –Converts a String to a double

Another Java Applet: Adding Integers  import statements Not necessary if specify full class name every time neededNot necessary if specify full class name every time needed public void paint( java.awt.Graphics g) * - indicates all classes in package should be available* - indicates all classes in package should be available –import java.swing.*; –Recall that this contains JApplet and JOptionPane –Does not import subdirectories b Instance variables Variables declared in body of a class (not in a method)Variables declared in body of a class (not in a method) –Each object of class gets its own copy –Can be used inside any method of the class Before, variables declared in mainBefore, variables declared in main –Local variables, known only in body of method defined

Another Java Applet: Adding Integers b Instance variables Have default valuesHave default values –Local variables do not, and require initialization before use –Good practice to initialize instance variables anyway  Method init Called automatically in all appletsCalled automatically in all applets Commonly used to initialize variablesCommonly used to initialize variables public void init() b References Identifiers (such as myString ) refer to objectsIdentifiers (such as myString ) refer to objects –Contain locations in memory References used to call methods, i.e. g.drawStringReferences used to call methods, i.e. g.drawString

Another Java Applet: Adding Integers b Variables vs. Objects VariablesVariables –Defined by a primitive data type –char, byte, short, int, long, float, double, boolean –Store one value at a time –Variable myInt Objects defined in classesObjects defined in classes –Can contain primitive (built-in) data types –Can contain methods –Graphics object g If data type a class name, then identifier is a referenceIf data type a class name, then identifier is a reference –Otherwise, identifier is a variable

Another Java Applet: Adding Integers  Other methods of class Graphics drawRect( x1, y1, x2, y2 );drawRect( x1, y1, x2, y2 ); Draws a rectangle with upper-left corner ( x1, y1 ), and lower right corner ( x2, y2 )Draws a rectangle with upper-left corner ( x1, y1 ), and lower right corner ( x2, y2 )

1// Fig : AdditionApplet.java 2// Adding two floating-point numbers 3import java.awt.Graphics; // import class Graphics 4import javax.swing.*; // import package javax.swing 5 6public class AdditionApplet extends JApplet { 7 double sum; // sum of the values entered by the user 8 9 public void init() 10 { 11 String firstNumber, // first string entered by user 12 secondNumber; // second string entered by user 13 double number1, // first number to add 14 number2; // second number to add // read in first number from user 17 firstNumber = 18 JOptionPane.showInputDialog( 19 "Enter first floating-point value" ); // read in second number from user 22 secondNumber = 23 JOptionPane.showInputDialog( 24 "Enter second floating-point value" ); // convert numbers from type String to type double 27 number1 = Double.parseDouble( firstNumber ); 28 number2 = Double.parseDouble( secondNumber ); // add the numbers Example : Addition Applet

31 sum = number1 + number2; 32 } public void paint( Graphics g ) 35 { 36 // draw the results with g.drawString 37 g.drawRect( 15, 10, 270, 20 ); 38 g.drawString( "The sum is " + sum, 25, 25 ); 39 } 40} Example : Addition Applet super.paint( g );

Program Output