method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:



Similar documents
Interactive Programs and Graphics in Java

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Introduction to Java Applets (Deitel chapter 3)

How To Write A Program For The Web In Java (Java)

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

The Basic Java Applet and JApplet

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Chapter 1 Java Program Design and Development

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

How to Convert an Application into an Applet.

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Essentials of the Java(TM) Programming Language, Part 1

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Essentials of the Java Programming Language

Fundamentals of Java Programming


11. Applets, normal window applications, packaging and sharing your work

An Overview of Java. overview-1

Homework/Program #5 Solutions

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

CS 335 Lecture 06 Java Programming GUI and Swing

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Programming with Java GUI components

Event processing in Java: what happens when you click?

For Introduction to Java Programming, 5E By Y. Daniel Liang

WEEK 2 DAY 14. Writing Java Applets and Java Web Start Applications

Java applets. SwIG Jing He

Fireworks 3 Animation and Rollovers

Syllabus for CS 134 Java Programming

Packaging and Deploying Java Projects in Forte

Computing Concepts with Java Essentials

There are some important differences between an applet and a standalone Java application, including the following:

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Web Development and Core Java Lab Manual V th Semester

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Swing. A Quick Tutorial on Programming Swing Applications

Using Impatica for Power Point

Java is commonly used for deploying applications across a network. Compiled Java code

CSC 551: Web Programming. Spring 2004

BCA 421- Java. Tilak Maharashtra University. Bachelor of Computer Applications (BCA) 1. The Genesis of Java

SE 450 Object-Oriented Software Development. Requirements. Topics. Textbooks. Prerequisite: CSC 416

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Graphical User Interfaces

TATJA: A Test Automation Tool for Java Applets

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

Using A Frame for Output

Lab 9. Spam, Spam, Spam. Handout 11 CSCI 134: Spring, To gain experience with Strings. Objective

WIRIS quizzes web services Getting started with PHP and Java

Java Mouse and Keyboard Methods

Ad Hoc Reporting. Usage and Customization

Search help. More on Office.com: images templates

GUI Components: Part 2

This assignment explores the following topics related to GUI input:

JAVA DEVELOPER S GUIDE TO ASPRISE SCANNING & IMAGE CAPTURE SDK

Action settings and interactivity

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Windows PowerShell Essentials

Sharing Presentations, Documents, and Whiteboards

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

Java (12 Weeks) Introduction to Java Programming Language

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Using IIS and UltraDev Locally page 1

Assignment # 2: Design Patterns and GUIs

JiST Graphical User Interface Event Viewer. Mark Fong

Creating Web Pages with Microsoft FrontPage

CaptainCasa. CaptainCasa Enterprise Client. CaptainCasa Enterprise Client. Feature Overview

Java Appletek II. Applet GUI

Software Design: Figures

BHARATHIAR UNIVERSITY COIMBATORE SCHOOL OF DISTANCE EDUCATION

Contents. Launching FrontPage Working with the FrontPage Interface... 3 View Options... 4 The Folders List... 5 The Page View Frame...

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Smart Connection 9 Element Labels

understand how image maps can enhance a design and make a site more interactive know how to create an image map easily with Dreamweaver

Tutorial Reference Manual. Java WireFusion 4.1

Slide.Show Quick Start Guide

How to create buttons and navigation bars

Mouse Event Handling (cont.)

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Mobile App Tutorial Animation with Custom View Class and Animated Object Bouncing and Frame Based Animation

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

About XML in InDesign

TUTORIAL 4 Building a Navigation Bar with Fireworks

Further web design: HTML forms

Using the Beans Development Kit 1.0. September A Tutorial. Alden DeSoto. Sept Garcia Avenue Mountain View, CA U.S.A.

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

NIS-Elements Viewer. User's Guide

Extending Desktop Applications to the Web

Chapter 14: Links. Types of Links. 1 Chapter 14: Links

Please select one of the topics below.

OUTLOOK WEB APP (OWA): MAIL

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Creating a 2D Game Engine for Android OS. Introduction

Introduction to OpenOffice Writer 2.0 Jessica Kubik Information Technology Lab School of Information University of Texas at Austin Fall 2005

1. Overview of the Java Language

Sharing Software. Chapter 14

Transcription:

Applets - Java Programming for the Web An applet is a Java program designed to run in a Java-enabled browser or an applet viewer. In a browser, an applet is called into execution when the APPLET HTML tag is encountered by the browser while it is executing the HTML script for the Web page. The APPLET tag refers the browser to a Java class file that it will find in the same directory as the HTML script file for the Web page. The Java class file is the compiled bytecode for the applet. Virtually any code can be run in an applet. The main exceptions, for security purposes, is that an applets cannot read or write to any local resource. An example of an Applet tag embedded in an HTML script would look like: <html> <body> <applet code="einstein.class" width=350 height=175> </applet> </body> </html> HTML syntax requires a TAG (such as APPLET)enclosed in < > with its resource description(s) followed by the same named TAG with a preceding /. The minimum TAGs for a HTML script is HTML and BODY. The Applet class is a subclass of Panel which is a subclass of Container. Unlike the Frame class, the Panel class does not create a separate window of its own. Since Panel is a container, we can add any of the AWT or Swing objects to it. The Applet class has many methods, but the most important ones are: init() called, after construction of the applet, by the browser or applet viewer to inform this applet that it has been loaded into the system. It is always called once, before the first time that the start method is called. start()- called when the browser opens the applet s window. It is called after the init method and each time the applet is revisited in a Web page. stop()- called when the browser changes to a new page, making the applet temporarily hidden. destroy()- called when the applet exits, reversing actions taken by init(). Applets use the Graphics class to provide color and other graphics attributes to its Panel parent. The method paint() of the Graphics class is overridden to provide graphic information. It applies methods to the Graphics object that it receives as a parameter. The

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows: public class Einstein extends java.applet.applet //----------------------------------------------------------------- // Draws a quotation by Albert Einstein among some shapes. //----------------------------------------------------------------- public void paint (Graphics page) page.drawrect (50, 50, 40, 40); // square page.drawrect (60, 80, 225, 30); // rectangle page.drawoval (75, 65, 20, 20); // circle page.drawline (35, 60, 100, 120); // line page.drawstring ("Out of clutter, find simplicity.", 110, 70); page.drawstring ("-- Albert Einstein", 130, 100); The above Applet produces the following graphics:

The steps for creating and running an Applet in NetBeans are: 1. File New Project 2. General Java Class Library-Next 3. Type Project name and location and click Finish 4. In Projects subwindow, right click ProjectName 5. New-File/Folder-Java Classes-Applet(or JApplet)-Next 6. Type Applet Class name and Project name, click Finish 7. Type or Cut and Paste your code into the Applet Body. 8. To Compile the code, Right click on the ProjectName and choose Build Project 9. To Run the Applet in the AppletViewer: Right Click on the Applet.java name (under Source Packages) 10. Click Run File. The example above is very basic. If the Applet wants to do any logical, arithmetic, or GUI processing, it must make use of the init and start methods. The init method acts like the constructor for the Applet. You use it to create objects need by the Applet. In the next example we create a panel that will calculate compound interest on an amount input by the user. This panel is created in the init() method. It uses several Java objects, JTextField, JLabel, JButton, JPanel, and a generic JComponent to create the look of the calculator illustrated below: p p1 p2 p3 canvas pane p4

The code to generate the Applet calculator panel above follows: public class CalcInt extends JApplet JTextField getrate = new JTextField(5); JTextField getbalance = new JTextField(8); JTextField getyears = new JTextField(3); JLabel rate = new JLabel("Rate"); JLabel balance = new JLabel("Balance"); JLabel years = new JLabel("Years"); MyCanvas canvas = new MyCanvas(); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JPanel p4 = new JPanel(); JPanel p = new JPanel(); JButton amount = new JButton("Amount"); JButton clear = new JButton("Clear"); String command = " "; String prompt = "Enter Data and Press Button "; NumberFormat nf; public void init() Container pane = getcontentpane(); pane.setlayout(new GridLayout(3,1)); p1.setlayout(new GridLayout(2,1)); p1.add(rate); p1.add(getrate);

p2.setlayout(new GridLayout(2,1)); p2.add(balance); p2.add(getbalance); p3.setlayout(new GridLayout(2,1)); p3.add(years); p3.add(getyears); p.setlayout(new FlowLayout()); p.add(p1); p.add(p2); p.add(p3); amount.setmnemonic('a'); amount.settooltiptext("calculate Amount"); p4.setlayout(new GridLayout(1,2)); p4.add(amount); p4.add(clear); amount.addactionlistener(canvas); clear.addactionlistener(canvas); pane.add(p); pane.add(canvas); pane.add(p4); canvas.setpreferredsize(new Dimension(100,100)); nf = NumberFormat.getCurrencyInstance();

Each panel p1, p2, and p3 is created with a label and textfield and then put into panel p(the entire upper section). The buttons are put into panel p4 with listeners added. Then p, canvas, and p4 are put into pane to complete the content. This example also illustrates the use of the setmnemonic()method, allowing an ALT+Letter to activate the Button, and the settooltiptext()method which provides mouse rollover textual feedback for the Button. Threads Threads are used to provide independent sequences of processing steps to executed concurrently( the processor splits its time processing between steps in each Thread. Each method has its own thread for its own steps, however a new Thread can be created and started that becomes a new independent sequence. Many animations use Threads, especially if they are displaying images that do not require any user interaction. The Thread class implements the Runnable interface and has many methods, but the most important ones are: start()- called when the Thread is to begin execution. run()- called after start is executed. It is the body of the Thread, it begins after start is called and executes until it reaches a return, or when the stop method is called. However, stop() method has been found to be unstable and has been deprecated, so now, Threads are dereferenced when they need to be stopped. The following illustrates an Applet that uses a Thread to animate a String of Text on the Screen. The text is redrawn a successive locations within the Applet s screen area. This Applet also illustrates the use of the gettoolkit()and getscreensize() methods to obtain available screen size info from the hardware platform. This Applet uses the entire screen for the scrolling area, so, maximize the AppletViewer window when you test run it. Also note that the Applet must use the Runnable interface if it needs to create a Thread object.

public class ScrollingString extends Applet implements Runnable Thread thethread; Font myfont; FontMetrics fontmet; String str="the Last Java Example"; boolean loop = true; int strwidth; int xpos; int ypos; int winwidth; public void init() myfont = new Font("TimesRoman",Font.BOLD,22); fontmet = getfontmetrics(myfont); strwidth = fontmet.stringwidth(str); Toolkit tools = this.gettoolkit(); Dimension size = tools.getscreensize(); winwidth=size.width; ypos = (size.height/2 - fontmet.getheight()); ypos=ypos+fontmet.getheight(); setbackground(color.white); public void start() if (thethread==null) thethread=new Thread(this); thethread.start();

public void run() //this method belongs to the Thread while (loop) for (int i=winwidth; i>0-strwidth; i--) xpos=i; repaint(); try thethread.sleep(10); catch (InterruptedException e) public void stop() if (thethread!=null) loop=false; thethread=null; public void paint(graphics g) g.setcolor(color.blue); g.setfont(myfont); g.drawstring(str,xpos,ypos); For other ways a Thread can be created and used in an application, see the Threads Javadocs. See/run Einstein, CalcInt, and ScrlStr project folders in the Instructors folder.