Making Applets ICS 4C / 4U. GUI GUI stands for Graphical User Interface and means that the screen you see in front of you probably has graphics on it.

Slides:



Advertisements
Similar presentations
Made with love, by Zachary Langley Applets The Graphics Presentation.
Advertisements

Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
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.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
A Simple Applet.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 13. Applets and HTML HTML Applets Computer Programming with JAVA.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
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,
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Topics Introduction Scene Graphs
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Copyright © Curt Hill Applets A different type of program.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
Creating a Java Application and Applet
Graphical User Interfaces (GUI). PART ONE About GUI’s.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
1 Java and AWT CPS 470 Spring 1998 Laura Campbell.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Lecture 09 Applets.
Object Oriented Programming
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
AWT Components and Containers
UNIT-5.
Java Programming COMP-417 Applet
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
Advanced GUIs and Graphics
Presentation transcript:

Making Applets ICS 4C / 4U

GUI GUI stands for Graphical User Interface and means that the screen you see in front of you probably has graphics on it (i.e. icons) that you wouldn’t have seen back in the days of DOS based software which only had pull-down menus. Java allows you to make your own GUI with component controls such as menus, radio buttons, checkboxes, scrolling text boxes etc.

EVENT HANDLING Since the Introduction of Windows and other “visual” operating systems (compared to DOS), the basic paradigm used is “event handling” where the user decides on the action of the program, be it :clicking a mouse dragging a mouse choosing from a menu dragging a scrollbar typing in text, etc. selecting item 1 or 2

SO WHAT HAPPENED TO THE MAIN METHOD WHEN USING AN APPLET?? Instead of the “main” (method) line being in control of the events, the operating system is the main line and Java provides an applet class containing a group of standard methods that are handled in response to specific events as the applet is run. The applet class thus allows you to create programs that can be:

1. run in a Web browser by simply typing in the name of your HTML file in the ‘URL address’ line or by typing the name of your HTML file in the ‘open’ line. 2. run by using the applet viewer tool that comes with the JDK by typing at the DOS prompt: appletviewer nameoHTMLfile.html 3. run by creating an Applet in Holt Java Ready software (or other environment) and then running it.

YOUR APPLET IS MERELY AN EXTENSION OF JAVA’S PREDEFINED APPLET CLASS This Applet class has already been created in Java’s predefined library (applet package) and thus has to be imported when creating your own applet since your applet will be an extension of this pre-existing applet.

Applet (PARENT CLASS) //found in the applet package in the java directory import java.awt.Graphics; public class Applet{ Graphics g; //other variable declarations init method( ){ the default init method has nothing in it; it merely allows you to override this method so you can instantiate your own objects (using the new command) and initialize variables }

paint method(Graphics g ){ the default paint method has nothing in it; it merely allows you to override this method so long as you use a parameter of type Graphics } /* there are also the start, stop, and destroy methods, discussed later*/ }// end Applet class

Notice the init and paint methods are both empty. This is referred to as an abstract method. Each subclass (child class) will have these methods in them but each method will be different, so there is no use for creating a base method in the parent class with anything in it.

// The "Applet1a" class. This is the built-in Boilerplate! import java.applet.*; import java.awt.*; public class Applet1a extends Applet{ //Your applet is a child of Java’s Applet class // Place instance variables here public void init () { // Place the body of the initialization method here } // init method public void paint (Graphics g) { // Place the body of the drawing method here } // paint method } // Applet1a class

The following methods are all in the parent class, and so are automatically called when needed (except for repaint, run and sleep). You may or may not use these methods in your own applet. If you use them, then it overrides the parent class’s (Applet) method of the same name. As a bare bones program, you will probably have either init() &/or paint() methods.

declarations before any of your methods, declare any variables that you want to have global to your program They replace this line in the boilerplate: // Place instance variables here

init() This method is the first to be called after the Web page/applet has been loaded or reloaded into memory. It allows you to initialize and instantiate variables, resize the Applet, import or include any resources (i.e. images or frames or layout managers), and set text properties such as font and colors for objects or the screen’s background color and adding any screen user interface component controls. **NOTE** although you can declare variables here, be careful that they do not become local and thus not work elsewhere.

start( ) This method is called when the applet is made active (i.e. whenever the applet’s HTML document path is displayed). This method is put either after the init( ) method or elsewhere if the applet is to be restarted (i.e. you might have gone to a new page and if you went back to your previous page, then you would need to restart the applet. This obviously could occur many times.)

run() The run() method is where the main work of a thread takes place. It is comparable to the main() block statement of a Java application. After the start() method is called, the thread begins executing. The program will remain in the run method until an interruption is detected. Repaint() and sleep() methods are often called within the run() method.

sleep(#) The sleep() method is often called within the run() method. The number inside the brackets represents the amount of time the applet waits before executing the code in the run() method represents 1 second.

paint(Graphics g ) Occurs whenever your display area needs to be drawn or redrawn (i.e. when scrolling the Web page or in response to a repaint() request). This method occurs after the start, resize and restore methods. Notice it takes a parameter (call it g if you like) that allows access to the Graphics class so you can use methods like g.drawString() or g.drawOval() instead of of c.println() or c.drawOval() etc. (since the Console class was designed for applet commands to be used in applications.)

repaint( ) This method will cause the paint( ) method to be called again. You’ll most likely call the repaint() method only when the applet’s display will change as a result of something your program’s code has done, such as event activation.

stop( ) This method is called when the applet is made inactive, even if it is just temporarily out of action, i.e. when the user switches to another Web page (and yet you need to continue running your applet in the background,in case you need to recall it again.) This method is used to terminate any executing threads and for cleaning up. This method must be called before, not after, the destroy method **NOTE** the start and stop methods will have the most use in animation programs

destroy( ) This method is called to remove the applet (its variables and objects) from memory and completely terminate its execution. This method is rarely overridden by yourself, but may be needed when something has been changed during a program and should be restored to its original state. Again, this would often apply when using animation. You can put cleanup code here to clean up whatever resources are being held. Typically, the stop() method should call the destroy() method after the thread is stopped.

STRING OUTPUT Unlike an application, an applet cannot use the system.out.println() and so you can’t use the c.println( ) method either. In an applet, we use instead a method from the Graphics class: g.drawString(“stringname”,Xpixel #, Ypixel#);

This line would be found inside the paint(Graphics g) method and so the g (or whatever you want to call your parameter) is a variable representing an object from the Graphics class and the point (1,1) is in the top left corner. (example) (example) You can print variables and strings, just like the println() method, by using the + sign. eg. g.drawString(“x = ”+ xvalue,10, 10);

OTHER GRAPHICS COMMANDS The Console class was primarily designed to allow you to use graphics in an application, that are normally used in an applet. As well, the Console class simplifies user input and string output etc. With an applet, you can draw shapes using the Graphics class in the paint() method i.e. g.drawOval()i.e.

HOW TO GET DRAWSTRING() TO PRINT NUMBERS As the name implies, drawString can ONLY PRINT STRINGS. Therefore, to do any number calculations, you would need to do them first using an integer or float type variables, and then convert them to string to print them. egeg.int num=15; String numstring=String.valueOf(num); g.drawString(numstring,30,40)

COMPONENTS/CONTROLS In the awt package, there is a Component class that is abstract, and so you don’t need to instantiate the class with the ‘new’ command. You DO need the ‘new’ command though for instantiating objects from its child classes, made up of various screen components such as:

O Button (“click here”) O Canvas (to display images or draw on) O Checkbox O Choice (a drop-down menu) O Container (contains a bunch of components) O Label (words appear, but with no border around it, they attach themselves to the left of another component))

O List (a scrollable menu) O Scrollbar O TextArea (more than one line, there is a border around this rectangle) O TextField (only one line, there is a border around this)

Type of Component Declaring itInstantiating It (creating the object with the new command) Adding it to the screen BUTTONButton click;click=new Button(); click=new Button(“picture.gif“); click=new Button(actionvariable); click=new Button(“please click here when done”); (you can type any string to label your button) add(click);

Type of Component Declaring itInstantiating It (creating the object with the new command) Adding it to the screen CHECKBOXCheckbox cb; (notice b is a small letter, not cap) cb=new Checkbox(); //where false is assumed cb=new Checkbox(actionvariable); cb=new Checkbox(“picture,gif”); cb=new Checkbox(“picture,gif”,true); cb=new Checkbox(“yes?”,“picture,gif”); cb=new Checkbox(“yes?”,“picture,gif”,true); cb=new Checkbox(“yes?”,true); // or false cb=new Checkbox(“yes?”);//false is assumed (“yes?“ is a String that will appear as a label beside your checkbox. You can type any label to your checkbox, not just yes?) (if you have true, then box starts out with a check in it, if false, then the box will appear empty) add(cb);

There is a full list of different components and a description of how to declare, intantiate, and add them to your applets on pages 16-6 to 16-9 in your binder…

TEXT FIELD INPUT TextField, has a method in it called getText(). Here is an example of how it is used:example !!! Add this method to the program in your binder!!! //the action method allows you to react to such events as button clicks, etc. public boolean action (Event evt, Object arg) { // you always need these 2 parameters repaint (); return true; }

NOTE ** if you are merely writing out your strings, and not needing their values for anything, then have just 1 variable that can be updated, instead of creating a variable for every single string (saves memory) i.e.: public void paint(Graphics g){ g.drawString(“Type your name and age above.”,25,75); String s=tf1.getText(); g.drawString(“Your name is: “ + s, 40,100); s=tf2.getText(); g.drawString(“Your age is: “ + s,40,120); } // end paint method