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

Similar documents
Chapter 3 - Introduction to Java 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

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

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Chapter 12 Advanced GUIs and Graphics

CS335 Graphics and Multimedia

CT 229 Arrays in Java

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

Java 1.8 Programming

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Introduction to Programming Using Java (98-388)

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

Chapter 4 Control Structures: Part 2

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

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

Sun ONE Integrated Development Environment

PROGRAMMING LANGUAGE 2

PROGRAMMING FUNDAMENTALS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Lecture Static Methods and Variables. Static Methods

Summary. 962 Chapter 23 Applets and Java Web Start

CS1004: Intro to CS in Java, Spring 2005

Java Applet & its life Cycle. By Iqtidar Ali

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Chapter 6 - Methods Prentice Hall. All rights reserved.

The Java language has a wide variety of modifiers, including the following:

Introduction to Computer Science I

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

Chapter 5 Control Structures: Part 2

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Datatypes, Variables, and Operations

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

G51PRG: Introduction to Programming Second semester Applets and graphics

Course Outline. Introduction to java

Chapter 3: Inheritance and Polymorphism

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

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

2. The object-oriented paradigm

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Advanced Internet Programming CSY3020

CompSci 125 Lecture 11

Introduction to Java

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

GUI, Events and Applets from Applications, Part III

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

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CSD Univ. of Crete Fall Java Applets

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

Java. Representing Data. Representing data. Primitive data types

2. The object-oriented paradigm!

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

ITI Introduction to Computing II

Loops. CSE 114, Computer Science 1 Stony Brook University

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Brief Summary of Java

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

Programming graphics

ITI Introduction to Computing II

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

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

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Programming. Syntax and Semantics

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

CS 180 Final Exam Review 12/(11, 12)/08

CS 335 Lecture 02 Java Programming

13 th Windsor Regional Secondary School Computer Programming Competition

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

index.pdf January 21,

Selected Java Topics

CS 11 java track: lecture 3

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

15CS45 : OBJECT ORIENTED CONCEPTS

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

CSC 1214: Object-Oriented Programming

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

CS313D: ADVANCED PROGRAMMING LANGUAGE

Defining Classes and Methods

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Control Structures in Java if-else and switch

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

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Java Applet Basics. Life cycle of an applet:

Java for Non Majors Spring 2018

9. APPLETS AND APPLICATIONS

Section 2.2 Your First Program in Java: Printing a Line of Text

APCS Semester #1 Final Exam Practice Problems

CS 1316 Exam 1 Summer 2009

Java Programming with Eclipse

Control Structures (Deitel chapter 4,5)

1. What is Jav a? simple

Lecture Static Methods and Variables. Static Methods

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

CSCI 355 LAB #2 Spring 2004

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Data Representation and Applets

Transcription:

Last Time v We created our first Java application v What are the components of a basic Java application? v What GUI component did we use in the examples? v How do we write to the standard output? v An applet is a small Java program that can be embedded into an HTML document Question: What does an html document look like? v The Java program is stored on the web server along with the html files, images, etc. v How is the Java program executed on the client s machine? v When the web browser loads the web page, it downloads the applet to the client machine and begins executing it v Web browsers are able to run Java applets either by Directly supporting Java (such as Netscape 7) Using the Java Plug-in (such as Microsoft Internet Explorer and Safari) v Another way to run Java applets is to use the appletviewer that is included with the Java SDK v The steps for creating and running applets are: Create the Java applet in the text editor and compile it using javac Create an html file that includes the Java applet Run the applet by using the appletviewer appletviewer myhtml.html First Java Applet v What are the useful things about Java applets? v Can you think of examples of situations where Java applets would be appropriate? import java.awt.graphics; // import class Graphics import javax.swing.japplet; // import class JApplet public class WelcomeApplet extends JApplet public void paint( Graphics g ) super.paint( g ); g.drawstring( "Welcome to Java Programming!",, ); // end method paint // end class WelcomeApplet

Executing the Applet HTML v To execute the applet, we must perform the following steps: Compile the applet in the same way that we did for Java applications How is that? Create and html document to load and run the applet Load the html file into a brower or using appletviewer v The html should contain the command to load the Java applet <applet code= WelcomeApplet.class width= 00 height= ></applet> 7 8 Properties of Applets How Applets Work v They do not need a main method. v They do however require a paint method v To simplify the process of writing Java applets, the applet inherits from the JApplet class. v This class contains all the methods needed to create basic applets (over 00 of them!) v When the browser or appletviewer begins executing the Java applet, three methods are called in this order: init start paint v These three methods are defined in the JApplet superclass and are inherited by the subclass v The inheritance is achieved by using the extends keyword 9 0 Method paint Method paint v In our example, the method paint overrides the method defined in the superclass v This new method is called instead of the original method v The next line in method paint draws a string to the screen g.drawstring( "Welcome to Java Programming!",, ); v Since we want the original paint method to be called we need to specify that in the body of our paint method super.paint(g);

Graphics Class Modifying the Applet v A package in Java is just a collection of classes v java.awt is an example of a package v It s classes include Dailog, Event, Graphics and Image v Let us practice using some of the other methods in the Graphics class v Which method would you like to use? v Let us try changing the colour of the text or shapes. How can we find out how to do this? Including Swing Components in Applets v What do I mean by Swing components? v The graphics class is not powerful enough to create meaningful Java applets v It is sometimes necessary to read in data from the users, display data on multiple lines and display several windows Swing and Applets v We can use the JOptionPane class in an applet in the same way that we used it in the application v Write a Java applet that will ask the user for two numbers and display the sum of these numbers v How can we combine Swing and applets? Basic Programming Concepts v Selection structures If statement. Same as in C++ if(x>0) System.out.println( X is positive ); else System.out.println( X is negative ); Conditional Operator (?:) v Java s only ternary operator What does ternary mean? v Shortcut of the if-else statement System.out.println( studentgrade >= 0? Passed : Failed ); 7 8

More Selection Structures Repetition Structures v Selection Structures: Switch statements. Same as C++ int choice = ; switch(choice) case : System.out.println( One ); case : System.out.println( Two ); case : System.out.println( Three ); default: System.out.println( None ); v Java supports three loop structures v While loop number = 0; while( number <= ) v For loop for( number=0; number<=; number++ ) 9 0 Repetition Structures Primitive Data Types v Do-while loop number = 0; do while( number <= ) v In other words, simple data types boolean true or false char bits chars byte 8 bits -8 to 7 short bits -,78 to,77 int bits -,7,8,8 to,7,8,7 long bits - to - float bits IEEE 7 floating point double bits IEEE 7 floating point Break Continue v The break statement can be used with while, dowhile, for and switch statements v break causes the immediate exit from any of the above statements for( int i=0; i<0; i++) if(i == ) number += i; v The continue statement is used with while, for or do-while loops v It causes the current iteration of the loop to stop and moves on to the next iteration for( int i=0; i<0; i++) if(i == ) continue; number += i;

Arrays Program v Arrays are declared by: int myarray[] = new int[0]; int myarray[]; myarray = new int[0]; v Write a program that creates an array of 0 elements. Assign values to the array (just hard code these in for now). Display a histogram of the contents of the array in the following format Element 0 Value 7 8 Histogram *** ***** ******* * *** ******** ****** GUI GUI v So far we have learnt how to output information using JOptionPane v A text area is better able to handle text formatting v As you notice, JOptionPane ignores \t 7 8 Assignment Summary v Knights Tour Assignment v Start Early v Due on Tuesday, February 7, 00 v We have covered: JTextArea Selection Structures Repetition Structures Arrays Batch Files 9 0