“Jobs and bosses will come and go, but your education will always help you to grow.” 1 Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide 1. What is an applet ? An applet is a small Internet-based program written in Java, a programming language for the Web, which can be downloaded by any computer. The applet is also able to run in HTML. The applet is usually embedded in an HTML page on a Web site and can be executed from within a browser. 2. What is the purpose of appletviewer ? AppletViewer is a standalone command-line program from Sun to run Java applets. Appletviewer is generally used by developers for testing their applets before deploying them to a website. 3. How does applet differ from stand alone program? Applets do not use main() method for initiating the execution of the code .Applets when loaded automatically call certain methods of applets class to start and execute the apllet code. Unlike stand alone applications , apllets cannot be run independently .They are run from inside a webpage using a special feature known as HTML tag. Applets cannot read from or write to the files in the local computer. Applets cannot communicate with other servers on the network. Applets cannot run any program from the local computer. 4. Write a note on applet skeleton? An Applet skeleton is as follows Import java awt.*; Import java.applet.*; /* <applet code=”Appleskel” width=300 height=300> </applet> /* Public class Appleskel extends Applet { Public void init() { //initialization }
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
“Jobs and bosses will come and go, but your education will always help you to grow.” 1
Call repaint( ) when you have changed something and want your changes to show up on
the screen
o You do not need to call repaint() when something in Java’s own components
(Buttons, TextFields, etc.)
o You do need to call repaint() after drawing commands (drawRect(...), fillRect(...),
drawString(...), etc.)
repaint( ) is a request--it might not happen
When you call repaint( ), Java schedules a call to update(Graphics g)
6. What is an update method in java applet?
The update() method is defined by the AWT and is called when your applet has requested that a portion of its window be redrawn. The problem is that the default version of update() first fills an applet with the default background colour and then calls paint(). You can override the update() method. The paint() in this case will simply call update(). public void update(Graphic g) { //Redisplay your window here. } public void paint(Graphics g) { update(g); // call to the update()method. }
“Jobs and bosses will come and go, but your education will always help you to grow.” 3
For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized. It is used
to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.
The Component class provides 1 life cycle method of applet.
1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class
object that can be used for drawing oval, rectangle, arc etc.
9. How to run Applet?
There are two ways to run an applet
1. By html file.
2. By appletViewer tool (for testing purpose).
// 1st way Simple example of Applet by html file:
To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in html file. Now click the html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
“Jobs and bosses will come and go, but your education will always help you to grow.” 5
// 2nd way :Simple example of Applet by appletviewer tool:
To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is for testing purpose only.
Event describes the change of state of any object . Example : Pressing a button, Entering a character in Textbox.
11. What are the components of Event handling ?
Event handling has three main components,
Events : An event is a change of state of an object.
Events Source : Event source is an object that generates an event.
Listeners : A listener is an object that listens to the event. A listener gets notified when
an event occurs.
12. How events are handled in java? Explain few Event handlers classes n listener
interfaces in java ?
Note : for both Event Classes and Listener Interfaces the description are same .
Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events
Events are supported by a number of Java packages, like java.util, java.awt and java.awt.event.
Important Event Classes and Interface
Event Classes Description Listener Interface
ActionEvent generated when button is pressed, menu-item is selected, list-item is double clicked
ActionListener
“Jobs and bosses will come and go, but your education will always help you to grow.” 7
Any applet that contains Swing components must be implemented with a subclass of JApplet. JApplet is a subclass of java.applet.Applet .
Example
In this example, We embed a Swing applet into web browser. This applet contains two buttons, when you click on any one of it, it shows which one is clicked.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingApplet extends JApplet { JButton jbtnOne; JButton jbtnTwo; JLabel jlab; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { guiInit(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can't create because of "+ exc); } } // Called second, after init(). Also called // whenever the applet is restarted. public void start() { // Not used by this applet. } // Called when the applet is stopped. public void stop() { // Not used by this applet. }
“Jobs and bosses will come and go, but your education will always help you to grow.” 17
// Called when applet is terminated. This is // the last method executed. public void destroy() { // Not used by this applet. } // Setup and initialize the GUI. private void guiInit() { // Set the applet to use flow layout. setLayout(new FlowLayout()); // Create two buttons and a label. jbtnOne = new JButton("One"); jbtnTwo = new JButton("Two"); jlab = new JLabel("Press a button."); // Add action listeners for the buttons. jbtnOne.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { jlab.setText("Button One pressed."); } }); jbtnTwo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { jlab.setText("Button Two pressed."); } }); // Add the components to the applet's content pane. getContentPane().add(jbtnOne); getContentPane().add(jbtnTwo); getContentPane().add(jlab); } }
For invoking it into a browser, the following Html code is needed :