1 / 32

Making Applets

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.

otylia
Download Presentation

Making Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Making Applets ICS 4C / 4U

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

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

  4. 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:

  5. 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. • run by using the applet viewer tool that comes with the JDK by typing at the DOS prompt: appletviewer nameoHTMLfile.html • run by creating an Applet in Holt Java Ready software (or other environment) and then running it.

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

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

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

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

  10. // The "Applet1a" class. This is the built-in Boilerplate! import java.applet.*; import java.awt.*; public class Applet1aextendsApplet{ //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

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

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

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

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

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

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

  17. 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 ofc.println() or c.drawOval() etc. (since the Console class was designed for applet commands to be used in applications.)

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

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

  20. destroy( ) This method is called to remove the applet (its variables and objects) frommemoryand 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.

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

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

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

  24. 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. intnum=15; String numstring=String.valueOf(num); g.drawString(numstring,30,40)

  25. 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:

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

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

  28. 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…

  29. TEXT FIELD INPUT TextField, has a method in it calledgetText(). 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; }

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

More Related