CSC System Development with Java Introduction to Java Applets Budditha Hettige

Similar documents
Chapter 3 - Introduction to Java Applets

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Java Applet Basics. Life cycle of an applet:

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Java TM Applets. Rex Jaeschke

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

Graphics Applets. By Mr. Dave Clausen

Advanced Internet Programming CSY3020

G51PRG: Introduction to Programming Second semester Applets and graphics

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

UNIT -1 JAVA APPLETS

9. APPLETS AND APPLICATIONS

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Introduction to Computer Science I

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

Graphics Applets. By Mr. Dave Clausen

Java Applet & its life Cycle. By Iqtidar Ali

CSD Univ. of Crete Fall Java Applets

Unit 1- Java Applets. Applet Programming. Local Applet and Remote Applet ** Applet and Application

TWO-DIMENSIONAL FIGURES

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Appendix F: Java Graphics

(0,0) (600, 400) CS109. PictureBox and Timer Controls

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Appendix F: Java Graphics

China Jiliang University Java. Programming in Java. Java Applets. Java Web Applications, Helmut Dispert

CSIS 10A Assignment 14 SOLUTIONS

Chapter 2 Using Objects

8/23/2011. Chapter Goals

2/9/2012. Chapter Two: Using Objects. Chapter Goals

Applets and the Graphics class

Topic 8 Graphics. Margaret Hamilton

Building Java Programs

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

Java - Applets. public class Buttons extends Applet implements ActionListener

Data Representation and Applets

Lecture Static Methods and Variables. Static Methods

SIMPLE APPLET PROGRAM

Chapter 12 Advanced GUIs and Graphics

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Here is a list of a few of the components located in the AWT and Swing packages:

Using Graphics. Building Java Programs Supplement 3G

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

Object Oriented Programming Concepts-15CS45

Contents 8-1. Copyright (c) N. Afshartous

GUI, Events and Applets from Applications, Part III

Variables, Types, and Expressions

Data Representation and Applets

Data Representation and Applets

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

TOPIC 5 INTRODUCTION TO PICTURES

Topic 8 graphics. -mgrimes, Graphics problem report 134

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Dr. Hikmat A. M. AbdelJaber

CT 229 Arrays in Java

TOPIC 5 INTRODUCTION TO PICTURES


AP CS Unit 12: Drawing and Mouse Events

CIS 162 Project 1 Business Card Section 04 (Kurmas)

Lecture Static Methods and Variables. Static Methods

IT101. Graphical User Interface

Building Java Programs

Garfield AP CS. Graphics

Object-Oriented Programming

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Expressions, Statements, and Control Structures

Programming Lecture 2. Programming by example (Chapter 2) Hello world Patterns Classes, objects Graphical programming

Graphics and Java2D. Objectives

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Building Java Programs

Building Java Programs

cs Java: lecture #5

Using NetBeans to document code. The NetBeans IDE can be used to help generate Javadoc documentation and to check that the documentation is complete.

GETTING STARTED. The longest journey begins with a single step. In this chapter, you will learn about: Compiling and Running a Java Program Page 2

Repetition. We might start out with the following program that draws only the left most object.

Sun ONE Integrated Development Environment

Programming graphics

Assignment 2. Application Development

Some classes and interfaces used in this chapter from Java s original graphics capabilities and from the Java2D API.

Introduction to Java Applets 12

PROGRAMMING LANGUAGE 2

MODULE 8p - Applets - Trials B

WebVisit User course

CS335 Graphics and Multimedia

User interfaces and Swing

Inheritance 2. Inheritance 2. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Lab 3: Work with data (IV)

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

ming 3 Resize the Size to [700, 500]. Note that the background containers in the program code: reference the then under () { 153, 255, 0 ) );

Chapter 3: Inheritance and Polymorphism

Part 1: Introduction. Course Contents. Goals. Books. Difference between conventional and objectoriented

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Programming: You will have 6 files all need to be located in the dir. named PA4:

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Transcription:

CSC 308 2.0 System Development with Java Introduction to Java Applets Budditha Hettige Department of Statistics and Computer Science

What is an applet? applet: a Java program that can be inserted into a web page and run by loading that page in a browser Brings web pages to life with interactive content, multimedia, games, and more The feature of Java that is primarily responsible for its initial popularity Users can run applets simply by visiting a web page that contains an applet program (if they have the Java runtime environment installed on their computer) Budditha Hettige 2

Applet classes in Java implementation a top-level container, like a JFrame behaves more like a JPanel class javax.swing.japplet java.lang.object java.awt.component java.awt.container java.awt.panel java.applet.applet» javax.swing.japplet Budditha Hettige 3

Applets contd. Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing applet is opened and downloaded Applications run in command windows Budditha Hettige 4

Sample Program import java.awt.graphics; // import class Graphics import javax.swing.japplet; // import class JApplet public class WelcomeApplet extends JApplet { // draw text on applet s background public void paint( Graphics g ) { // call superclass version of method paint super.paint( g ); // draw a String at x-coordinate 25 and y-coordinate g.drawstring( "Welcome to Java Programming!", 25, 25 ); } // end method paint } // end class WelcomeApplet Budditha Hettige 5

Running the applet Compile javac WelcomeApplet.java If no errors, bytecodes stored in WelcomeApplet.class Create an HTML file Loads the applet into appletviewer or a browser Ends in.htm or.html To execute an applet Create an HTML file indicating which applet the browser (or appletviewer) should load and execute Budditha Hettige 6

Create a HTML file 1 <html> 2 <applet code = "WelcomeApplet.class" width = "300" height = "45"> 3 </applet> 4 </html> Simple HTML file (WelcomeApplet.html) Usually in same directory as.class file Remember,.class file created after compilation HTML codes (tags) Usually come in pairs Begin with < and end with > Lines 1 and 4 - begin and end the HTML tags Line 2 - begins <applet> tag Specifies code to use for applet Specifies width and height of display area in pixels Line 3 - ends <applet> tag Budditha Hettige 7

Execute an applet appletviewer only understands <applet> tags Ignores everything else Minimal browser Executing the applet appletviewer WelcomeApplet.html Perform in directory containing.class file Budditha Hettige 8

The paint method 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 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) Budditha Hettige 9

Colors The java.awt package defines a class named Color There are 13 predefined colors here are their fully-qualified names: 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 Budditha Hettige 10

Colors 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 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 Budditha Hettige 11

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 Budditha Hettige 12

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 Budditha Hettige 13

Drawing rectangles There are two ways to draw rectangles: g.drawrect( left, top, width, height ); g.fillrect(left, top, width, height ); Budditha Hettige 14

Sample Applet import java.applet.applet; import java.awt.*; 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); Budditha Hettige 15

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 ); Budditha Hettige 16

Applet Methods public void init () public void start () public void stop () public void destroy () public void paint (Graphics) public void repaint() public void update (Graphics) public void showstatus(string) public String getparameter(string) java.awt.panel java.a pplet.apple t destroy() init() start() stop() Budditha Hettige 17

Applet Method contd. import java.applet.*; import java.awt.*; public class BasicApplet extends Applet { public void init() { // Called once by the browser when // it starts the applet. } public void start() { // Called whenever the page containing // this applet is made visible. } public void stop() { // Called whenever the page containing this // applet is not visible. } public void destroyed() { // Called once when the browser destroys // this applet. } public void paint(graphics g) { // Called whenever this applet needs to // repaint itself. } } Budditha Hettige 18

The Life-Cycle of Applet init() Called exactly once in an applet s life. Called when applet is first loaded, which is after object creation, e.g., when the browser visits the web page for the first time. Used to read applet parameters, start downloading any other images or media files, etc. start() Called at least once. Called when an applet is started or restarted, i.e., whenever the browser visits the web page. stop() Called at least once. Called when the browser leaves the web page. Budditha Hettige 19

Applet Life-Cycle (Cont.) destroy() Called exactly once. Called when the browser unloads the applet. Used to perform any final clean-up. init start stop destroy start Budditha Hettige 20

Passing parameter to applet Parameters can be placed in the HTML code of your web page, which your applet can read: <APPLET code="mypackage/myapplet.class" width=400 height=300> <PARAM name="password" value="tacoma"> </PARAM> </APPLET> Methods in the JApplet class: public String getparameter(string name) Returns the value of the parameter with the given name. String password = this.getparameter("password"); public String[][] getparameterinfo() Returns an array of all parameter names, descriptions, and values. Budditha Hettige 21

Applet on NetBeans Budditha Hettige 22

Sample Programs C:/Program Files/Java/jdk1.6.0_23/demo/applets/ Budditha Hettige 23