OBJECT ORIENTED PROGRAMMING UNITV EVENT DRIVEN PROGRAMMING Prepared
OBJECT ORIENTED PROGRAMMING UNIT-V EVENT DRIVEN PROGRAMMING Prepared By Mr. M. Babu ASP/CSE KIOT, Salem
GRAPHICS PROGRAMMING • Java applets can be written to draw lines, arcs, figures, images and text in different fonts and styles. Different colors can also be incorporated in display. • graphics class defines a number of drawing functions
JAVA AWT Hierarchy
• component object is responsible for – foreground and background colours and the currently selected text font. All the elements like buttons, text fields, scrollbars. . etc • Container – can contain another components like buttons, textfields, labels etc. • Panel – The Panel is the container that doesn't contain title bar and menu bars. – It can have other components like button, textfield etc. • Window Class – creates a top level window and it does not have borders and menu bar • Frame – contains title bar and can have menu bars. It can have other components like button, textfield etc
• • • Components Button (java. awt. Button). . . Checkboxes (java. awt. Checkbox). . . Radio Buttons (java. awt. Checkbox. Group). . . Choice Buttons (java. awt. Choice). . . Labels (java. awt. Label). . . Text. Fields (java. awt. Text. Field). . . We can create a GUI using Frame in two ways: 1) By extending Frame class 2) By creating the instance of Frame class To create and add a component Button b=new Button("click me"); add(b); //adding button into frame
To create a frame import java. awt. *; class First extends Frame { First() { Button b=new Button("click me"); b. set. Bounds(30, 100, 80, 30); // setting button position add(b); //adding button into frame set. Size(300, 300); //frame size 300 width and 300 height set. Layout(null); //no layout manager set. Visible(true); //now frame will be visible, by default not visible } public static void main(String args[]){ First f=new First(); }}
c: jdk 1. 4bin>javac First. java c: jdk 1. 4bin>java First
Using Instance of a frame import java. awt. *; public class Example 2 { Example 2() { //Creating Frame fr=new rame(); Label lb = new Label("User. Id: "); fr. add(lb); Text. Field t = new Text. Field(); fr. add(t); fr. set. Size(500, 300); fr. set. Layout(new Flow. Layout()); fr. set. Visible(true); } public static void main(String args[]) { Example 2 ex = new Example 2(); } }
APPLET • Applet is a Java Program • Embedded into a web page • Runs inside the web browser • Run by Applet Viewer
https: //hajsoftutorial. com/wp-content/uploads/2014/12/fsfdfd. gif
Applet Life Cycle Init(): Variable declaration and initialization operations are performed in this method. start(): The start() method contains the actual code of the applet that should run. The start() method executes immediately after the init() method. It also executes whenever the applet is restored paint(): The paint() method is used to redraw the output on the applet display area.
stop(): The stop() method stops the execution of the applet. The stop() method executes when the applet is minimized or when moving from one tab to another in the browser. destroy(): The destroy() method executes when the applet window is closed or when the tab containing the webpage is closed. stop() method executes just before when destroy() method is invoked. The destroy() method removes the applet object from memory.
import java. awt. *; import java. applet. *; public class My. Applet extends Applet { public void init() { } public void start() { } public void stop() { } public void paint(Graphics g) { g. draw. String("Painting. . . “, 200, 300); } public void destroy() { } }
Graphics Methods • draw. String(String str, int x, int y) – g. draw. String("Hello World", 100); • Setting Background and Foreground Color – void set. Fore. Ground(mycolor) – void set. Back. Ground(mycolor) – set. Fore. Ground(Color. red); • void draw. Rect(int x, int y, int width, int height): • void fill. Rect(int x, int y, int width, int height):
APPLET PROGRAMMING import java. awt. *; import java. applet. *; /* <applet code="Ellipses" width=“ 300” Height=“ 300”> </applet> */ public class Ellipses extends Applet { public void paint(Graphics g)
c: jdk 1. 4binjavac Ellipses. java c: jdk 1. 4binappletviewer Ellipses. java
APPLET PROGRAMMING import java. awt. *; import java. applet. *; public class Ellipses extends Applet { public void paint(Graphics g) { g. draw. Oval(10, 60, 50); } } a. html <html>
c: jdk 1. 4binjavac Ellipses. java c: jdk 1. 4binappletviewer a. html
import java. applet. *; public class Rectangles. Drawing extends Applet { public void paint(Graphics g) { g. set. Color(Color. blue); g. draw. Rect(50, 80, 150, 100); g. set. Color(Color. magenta); g. fill. Rect(230, 80, 150, 100); } } <applet code="Rectangles. Drawing" width="400" height="300"> </applet>
EVENT HANDLING • Event: – Is a change in state of the object (component) – Ex: Clicked, Pressed, Dragged , Tapped , Hold • Event Listener: – Listens an event – responsible to handle events – When receives an event, perform a task and return them – Listeners are group of interface
Sources of Events : Event Source A source is an object that generates an event Button Checkbox Choice List Menu Item Scrollbar Text Components Window Action Event Item. Event Adjustment. Event
Event Class Container. Event Description Generated when a button is pressed, a list is double-clicked, or a menu item is selected. Generated when a scroll bar is manipulated. Generated when a component is hidden, moved, resized, or becomes visible. Generated when a component is added to or removed from a container. Focus. Event Generated when a component gains or loses keyboard focus. Item. Event Generated when a check box or a list item is clicked; also occurs when a choice selection is made or a checkable menu is selected or deselected. Key. Event Generated when input is received from the keyboard. Mouse. Event Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component. Text. Event Generated when the value of a textarea or textfield is changed. Window. Event Generated when a window os activated, closed, deactivated, deiconified, opened, or quit. Action. Event Adjustment. Event Component. Event
event Handling • The Java Action. Listener is notified whenever you click on the button or menu item. It is notified against Action. Event. • How to write Action. Listener – – Import java. awt. event. *; Implement the Action. Listener interface in the class Register the component with the Listener Override the action. Performed() method
Step 1: public class A implements Action. Listener Step 2; Button b=new Button(“Click”); add(b); b. add. Action. Listener(this); Step 3; public void action. Performed(Action. Event e) { //Write the code here }
import java. applet. *; import java. awt. event. *; public class A extends Applet implements Action. Listener { Button b; Text. Field tf; public void init() { tf=new Text. Field(); tf. set. Bounds(30, 40, 150, 20); b=new Button("Click"); b. set. Bounds(80, 150, 60, 50); add(b); add(tf); b. add. Action. Listener(this); set. Layout(null); }
public void action. Performed(Action. Event e) { tf. set. Text("Welcome"); } } A. html <html> <applet code="A. class" width="400" height="400"> </applet> </html>
import java. awt. *; import java. applet. *; import java. awt. event. *; public class A 2 extends Applet implements Action. Listener { Button ok. Button; Button wrong. Button; Text. Field name. Field; Checkbox. Group radio. Group; Checkbox radio 1; Checkbox radio 2; Checkbox radio 3;
public void init() { set. Layout(new Flow. Layout()); ok. Button = new Button("Action!"); wrong. Button = new Button(“Clear"); name. Field = new Text. Field("Type here Something", 35); radio. Group = new Checkbox. Group(); radio 1 = new Checkbox("Red", radio. Group, false); radio 2 = new Checkbox("Blue", radio. Group, true); radio 3 = new Checkbox("Green", radio. Group, false); add(ok. Button); add(wrong. Button); add(name. Field); add(radio 1); add(radio 2); add(radio 3); ok. Button. add. Action. Listener(this); wrong. Button. add. Action. Listener(this); }
public void paint(Graphics g) { if (radio 1. get. State()) g. set. Color(Color. red); else if (radio 2. get. State()) g. set. Color(Color. blue); else g. set. Color(Color. green); g. draw. String(name. Field. get. Text(), 20, 100); }
public void action. Performed(Action. Event evt) { if (evt. get. Source() == ok. Button) repaint(); else if (evt. get. Source() == wrong. Button) { wrong. Button. set. Label(“Reset"); name. Field. set. Text(“ "); repaint(); } } }
- Slides: 37