making applets

32
Making Applets ICS 4C / 4U

Upload: otylia

Post on 22-Feb-2016

60 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Making Applets

Making AppletsICS 4C / 4U

Page 2: Making Applets

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. 

Page 3: Making Applets

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 scrollbartyping in text, etc.selecting item 1 or 2

 

Page 4: Making Applets

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: 

Page 5: Making Applets

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.

 

Page 6: Making Applets

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.

Page 7: Making Applets

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

 } 

Page 8: Making Applets

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

Page 9: Making Applets

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.

Page 10: Making Applets

// 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

Page 11: Making Applets

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. 

Page 12: Making Applets

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

Page 13: Making Applets

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. 

Page 14: Making Applets

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.) 

Page 15: Making Applets

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. 

Page 16: Making Applets

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. 1000 represents 1 second. 

Page 17: Making Applets

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.) 

Page 18: Making Applets

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. 

Page 19: Making Applets

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 

Page 20: Making Applets

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. 

Page 21: Making Applets

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#); 

 

Page 22: Making Applets

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)

You can print variables and strings, just like the println() method, by using the + sign.eg. g.drawString(“x = ”+ xvalue ,10, 10);

Page 23: Making Applets

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() 

Page 24: Making Applets

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.  eg. int num=15;

String numstring=String.valueOf(num);

g.drawString(numstring,30,40) 

Page 25: Making Applets

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: 

Page 26: Making Applets

O Button (“click here”)O Canvas (to display images or draw

on)O CheckboxO 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))

 

Page 27: Making Applets

O List (a scrollable menu)O ScrollbarO TextArea (more than one line, there is

a border around this rectangle)O TextField (only one line, there is a

border around this) 

Page 28: Making Applets

 Type of

Component

Declaring it Instantiating It(creating the object with the new

command)

Adding it to the screen

BUTTON Button 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);

Page 29: Making Applets

 Type of

Component

Declaring it Instantiating It(creating the object with the new

command)

Adding it to the screen

CHECKBOX Checkbox cb;

(notice b is a small letter, not cap)

cb=new Checkbox(); //where false is assumedcb=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 falsecb=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);

Page 30: Making Applets

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…

Page 31: Making Applets

TEXT FIELD INPUT 

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

Page 32: Making Applets

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