0% found this document useful (0 votes)
3 views

Unit 3 Java

The document covers core concepts of Java programming related to graphics and GUI, including the lifecycle of Java applets, how to create and run applets, and various GUI components such as frames, labels, text fields, and event handling. It explains the use of AWT for GUI development and provides examples of creating and manipulating GUI elements. Additionally, it discusses event delegation and the importance of listeners in making components responsive to user actions.

Uploaded by

user-724064
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 3 Java

The document covers core concepts of Java programming related to graphics and GUI, including the lifecycle of Java applets, how to create and run applets, and various GUI components such as frames, labels, text fields, and event handling. It explains the use of AWT for GUI development and provides examples of creating and manipulating GUI elements. Additionally, it discusses event delegation and the importance of listeners in making components responsive to user actions.

Uploaded by

user-724064
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Core Java Programming

(BCA 512)
By
Anuj Kumar Raghav
(TMU07531)
Unit 3

Graphics are visual representations on a surface, such as a computer screen. Examples are
photographs, drawing, graphics designs, maps, engineering drawings, or other images. Graphics often
combine text and illustration.

Java Applet
Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs
inside the browser and works at client side.

Drawback of Applet
Plugin is required at client browser to execute applet.

Hierarchy of Applet

Lifecycle of Java Applet


 Applet is initialized.
 Applet is started.
 Applet is painted.
 Applet is stopped.
 Applet is destroyed.

java.applet.Applet class

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.

java.awt.Component class
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.
How to run an Applet?
There are two ways to run an applet

1. By html file.
2. By appletViewer tool (for testing purpose).

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.

1. //First.java
2. import java.applet.Applet;
3. import java.awt.Graphics;
4. public class First extends Applet{
5.
6. public void paint(Graphics g){
7. g.drawString("welcome",150,150);
8. }
9.
10. }
Note: class must be public because its object is created by Java Plugin software that resides on the
browser.

myapplet.html
1. <html>
2. <body>
3. <applet code="First.class" width="300" height="300">
4. </applet>
5. </body>
6. </html>
Appletviewer file.html type in cmd after compile java file

Example of Graphics in applet:

1. import java.applet.Applet;
2. import java.awt.*;
3.
4. public class GraphicsDemo extends Applet{
5.
6. public void paint(Graphics g){
7. g.setColor(Color.red);
8. g.drawString("Welcome",50, 50);
9. g.drawLine(20,30,20,300);
10. g.drawRect(70,100,30,30);
11. g.fillRect(170,100,30,30);
12. g.drawOval(70,200,30,30);
13.
14. g.setColor(Color.pink);
15. g.fillOval(170,200,30,30);
16. g.drawArc(90,150,30,30,30,270);
17. g.fillArc(270,150,30,30,0,180);
18.
19. }
20. }
myapplet.html
1. <html>
2. <body>
3. <applet code="GraphicsDemo.class" width="300" height="300">
4. </applet>
5. </body>
6. </html>

GUI

 GUI stands for Graphical User Interface.


 It is a type of interface which allow user to interact with computer.
 It is used windows, menus , icons which can be manipulated by mouse.

What is GUI programming?


Programming application which used such as windows, menus, icon, labels, this type of
applications development are known as GII programming.

GUI programming in java


We can make GUI application using AWT(Abstract window toolkit), Swing , JavaFx , this is the
readymade API(application interface) which provide a rich set of classes and interface. So the
developer can developed GUI using these API’S.

AWT(Abstract Window tool kit)


 Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based
applications in java.
 Java AWT components are platform-dependent i.e. components are displayed
according to the view of operating system. AWT is heavyweight i.e. its components are
using the resources of OS.
 The java.awt package provides classes for AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
 It is a package in which the collection of classes and interfaces from which developer
can make GUI application

component : -
Component is an object which is displayed pictorially on the screen.
Ex:-
Button,Label,TextField ..... etc
Container:-
Container is a GUI component, it is able to accommodate all other GUI components.
Ex:-
Frame,Applet.
Event:-
The event nothing but a action generated on the component or the change is made on the
state of the object.
Ex:-
Button clicked, Checkboxchecked, Itemselected in the list, Scrollbar scrolled
horizontal/vertically.
Frame
1) Frame is a class which is present in java.awt package.
2) Frame is a Basic component in AWT, because all the components displayed in a Frame.
3) We are displaying pictures on the Frame.
4) It is possible to display some text on the Frame.

Based on the above reasons the frame will become basic component in AWT.
Constructors:-
* create a Frame class object.
Frame f=new Frame();
* create a Frame class object and pass file
Frame f=new Frame("MyFrame");
* Take a subclass to the Frame and create object to the subclass.
class MyFrame extends Frame

Characteristics of the Frame:-


1) When we create a Frame class object the Frame will be created automatically with the
invisible mode. To provide visible mode to following method.

public void setVisible(boolean b)


where b==true means visible mode.
where b==false means invisible mode.
Ex: f.setVisible(true);

public void setSize(int width,int height)


Ex: f.setSize(400,500);

3) To provide title to the Frame explicitly we have to use the following method

public void setTitle(String Title)


Ex: f.setTitle("MyFrame");

4) When we create a Frame, the default background color of the Frame is white. If you want to
provide particular color to the Frame we have to use the following method.

public void setBackground(color c)


Ex: f.setBackground(Color.red);

********CREATION OF FRAME**********
import java.awt.*;
class Demo
{
public static void main(String[] args)
{
//frame creation
Frame f=new Frame();
//set visibility
f.setVisible(true);
//set the size of the frame
f.setSize(400,400);
//set the background
f.setBackground(Color.red);
//set the title of the frame
f.setTitle("java");
}
};

;Preparation of the components

Label
1) Label is a constant text which is displayed along with a TextField or TextArea.
2) Label is a class which is present in java.awt package.
3) To display the label we have to add that label into the frame for that purpose we have to
use add() method present in the Frame class.

Constructor
Label l=new Label();
Label l=new Label(“user name”);
Ex :-
import java.awt.*;
class Test
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setVisible(true);
f.setTitle("Aims");
f.setBackground(Color.red);
f.setSize(400,500);
Label l=new Label("user name:");
f.add(l);
}
}

TextField
1) TextField is an editable area.
2) In TextField we are able to provide single line of text.
3) Enter Button doesn’t work on TextField. To add TextField into the Frame we have to use
add() method.
Ex :-
import java.awt.*;
class Test
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setVisible(true);
f.setTitle("Aims");
f.setBackground(Color.red);
f.setSize(400,500);
//TextField tx=new TextField(); empty TextField
TextField tx=new TextField("Manish");
//TextField with data
f.add(tx);
}
}

TextArea
1) TextArea is a class present in java.awt.package.
2) TextArea is a Editable Area. Enter button will work on TextArea.
3) To add the TextArea into the frame we have to use the add()
.
EX:

import java.awt.*;
class Test
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setVisible(true);
f.setTitle("AimsTutorial");
f.setBackground(Color.red);
f.setSize(400,500);
f.setLayout(new FlowLayout());
Label l=new Label("user name:");
TextArea tx=new TextArea(4,10);//4 character height 10 character width
tx.appendText("Manish");
tx.setText("Soni");
System.out.println(tx.getText());
f.add(l);
f.add(tx);
}
}

Choice
1) Choice is a class present in java.awt package.
2) List is allows to select multiple items but choice is allow to select single Item.
Constructor:-
Choice ch=new Choice();

Methods :-
1. To add items to the choice we have to use following method.

ch.add(“HYD”);
ch.add(“Chennai”);
ch.add(“BANGALORE”);

2. To remove item from the choice based on the string.

ch.remove(“HYD”);
ch.remove(“BANGALORE”);

3. To remove the all elements

ch.removeAll();

4. To inset the data into the choice based on the particular position.

ch.insert(2,”ratan”);

Ex:-
import java.awt.*;
class Test
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setVisible(true);
f.setTitle("AimsTutorial");
f.setBackground(Color.red);
f.setSize(400,500);
Choice ch=new Choice();
ch.add("c");
ch.add("cpp");
ch.add("java");
ch.add(".net");
ch.remove(".net");
ch.remove(0);
ch.insert("Manish",0);
f.add(ch);
System.out.println(ch.getItem(0));
System.out.println(ch.getSelectedItem());
System.out.println(ch.getSelectedIndex());
//ch.removeAll();
}
}
List

1) List is a class it is present in java.awt.package

2) List is providing list of options to select. Based on your requirement we can select any
number of elements. To add the List to the frame we have to use add() method.

CONSTRUCTOR:-

1) List l=new List();


It will creates the list by default size is four elements. And it is allow selecting the only one
item at a time.

2) List l=new List(3);

It will display the three items size and it is allow selecting the only single item.

3) List l=new List(5,true);

It will display the five items and it is allow selecting the multiple items.
Methods:-

1. To add the elements to the List we have to use following method.

l.add(“c”);
l.add(“cpp”);
l.add(“java”);
l.add(“Manish”,0);
2. To remove element from the List we have to use following method.
l.remove(“c”);
l.remove(2);
3. To get selected item from the List we have to use following method.
String x=l.get SelectedItem();
4. To get selected items from the List we have to use following method.
String[] x=s.get SelectedItems()
Ex:-
import java.awt.*;
class Test
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setVisible(true);
f.setTitle("ratan");
f.setBackground(Color.red);
f.setSize(400,500);
f.setLayout(new FlowLayout());
List l=new List(4,true);
l.add("c");
l.add("cpp");
l.add("java");
l.add(".net");
l.add("ratan");
l.add("arun",0);
l.remove(0);
f.add(l);
System.out.println(l.getSelectedItem());
}
}

Checkbox:-

1) Checkbox is a class present in java.awt package.

2) The user can select more than one checkbox at a time. To add the checkbox to the frame we
have to use add() method.

Constructor:-

1) Checkbox cb1=new CheckBox();

cb1.setLable(“BTECH”);

2) Checkbox cb1=new CheckBox(“MCA”);

3) Checkbox cb3=new CheckBox(“BSC”,true);

Ex:-
import java.awt.*;
class Test
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setVisible(true);
f.setTitle("Aims Tutorial");
f.setBackground(Color.red);
f.setSize(400,500);
Checkbox cb1=new Checkbox("BTECH",true);
f.add(cb1);
System.out.println(cb1.getLabel());
System.out.println(cb1.getState());
}
}
RADIO BUTTON:-

1) AWT does not provide any predefined support to create RadioButtons.

2) It is possible to select Only item is selected from group of item. To add the RadioButton to
the frame we have to use add() method.

By using two classes we create Radio Button those are


a)CheckBoxgroup
b)CheckBox
step 1:- Create CheckBox group object.
CheckBoxGroup cg=new CheckBoxGroup();
step 2:- pass Checkbox object to the CheckboxGroup class then the radio buttons are created.
CheckBox cb1=new CheckBox(“male”,cg,false);
CheckBox cb2=new CheckBox(“female”,cg,false);

Layout Managers:-
import java.awt.*; class
Test
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setVisible(true);
f.setTitle("Aims Tutorial");
f.setBackground(Color.red);
f.setSize(400,500);
Label l1=new Label("user name:");
TextField tx1=new TextField();
Label l2=new Label("password:");
TextField tx2=new TextField();
Button b=new Button("login");
f.add(l1);
f.add(tx1);
f.add(l2);
f.add(tx1);
f.add(b);
}
}
Event delegation model:-

1. When we create a component the components visible on the screen but it is not possible to
perform any action for example button.

2. Whenever we create a Frame it can be minimized and maximized and resized but it is not
possible to close the Frame even if we click on Frame close Button.

3. The Frame is a static component so it is not possible to perform actions on the Frame.
4. To make static component into dynamic component we have to add some actions to the
Frame.

5. To attach actions to the Frame component we need event delegation model.

Whenever we click on button no action will be performed clicking like this is called event.
Event: - Event is nothing but a particular action generated on the particular component.

1. When an event generates on the component the component is unable to respond because
component can't listen the event.

2. To make the component listen the event we have to add listeners to the component.

3. Wherever we are adding listeners to the component the component is able to respond
based on the generated event.

4. A listener is a interface which contain abstract methods and it is present in java.awt.event


package

5. The listeners are different from component to component.

A component delegate event to the listener and listener is designates the event to appropriate
method by executing that method only the event is handled. This is called Event Delegation
Model.

Note: -
To attach a particular listener to the Frame we have to use following method
Public void AddxxxListener(xxxListener e)
Where xxx may be ActionListener,windowListener
The Appropriate Listener for the Frame is “windowListener”

ScrollBar:-

1. ScrollBar is a class present in the java.qwt.package

2. By using ScrollBar we can move the Frame up and down.

ScrollBar s=new ScrollBar(int type)


Type of scrollbar

1. VERTICAL ScrollBar
2. HORIZONTAL ScrollBar

To create a HORIZONTAL ScrollBar:-


ScrollBar sb=new ScrollBar(ScrollBar.HORIZONTAL);
To get the current position of the scrollbar we have to use the following method.
public int getValue()
To create a VERTICAL ScrollBar:-
ScrollBar sb=new ScrollBar(ScrollBar.VERTICAL);
Appropriate Listeners for Components:-
GUI Component Event Name Listner Name Lisener Methods

1. Frame Window Event Window Listener


1. Public Void WindowOpened(WindowEvent e)
2. Public Void WindowActivated(WindowEvent e)
3. Public Void WindowDeactivated(WindowEvent e)
4. Public Void WindowClosing(WindowEvent e)
5. Public Void WindowClosed(WindowEvent e)
6. Public Void WindowIconfield(WindowEvent e)
7. Public Void WindowDeiconified(WindowEvent e)
2.Textfield ActionEvent ActionListener
1. Public Void Actionperformed(ActionEvent ae)
3. TextArea ActionEvent ActionListener
1. Public Void Actionperformed(ActionEvent ae)
4. Menu ActionEvent ActionListener
1. Public Void Actionperformed(ActionEvent ae)
5. Button ActionEvent ActionListener
1. Public Void Actionperformed(ActionEvent ae)
6. Checkbox ItemEvent ItemListener
1. Public Void ItemStatechanged(ItemEvent e)
7. Radio ItemEvent ItemListener
1. Public Void ItemStatechanged(ItemEvent e)
8. List ItemEvent ItemListener
1. Public Void ItemStatechanged(ItemEvent e)
9. Choice ItemEvent ItemListener
1. Public Void ItemStatechanged(ItemEvent e)
10.Scrollbar AdjustmentEvent AdjustmentListener
1. Public Void AdjustementValueChanged (AdjustementEvent e)
11.Mouse MouseEvent MouseListener
1. Public Void MouseEntered(MouseEvent e)
2. Public Void MouseExited(MouseEvent e)
3. Public Void MousePressed(MouseEvent e)
4. Public Void MouseReleased(MouseEvent e)
5. Public Void MouseClicked(MouseEvent e)
12.Keyboard KeyEvent KeyListener
1. Public Void KeyTyped(KeyEvent e)
2. Public Void KeyPressed(KeyEvent e)
3. Public Void KeyReleased(KeyEvent e)
----------------------------------------------------
*****BORDERLAYOUT**********
import java.awt.*;
class MyFrame extends Frame
{
Button b1,b2,b3,b4,b5;
MyFrame()
{
this.setBackground(Color.green);
this.setSize(400,400);
this.setVisible(true);
this.setLayout(new BorderLayout());
b1=new Button("Boys");
b2=new Button("Girls");
b3=new Button("management");
b4=new Button("Teaching Staff");
b5=new Button("non-teaching staff");
this.add("North",b1);
this.add("Center",b2);
this.add("South",b3);
this.add("East",b4);
this.add("West",b5);
}
}
class Demo
{
public static void main(String[] args)
{
MyFrame f=new MyFrame();
}
};
******MENUITEMS************
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements ActionListener
{
String label="";
MenuBar mb;
Menu m1,m2,m3;
MenuItem mi1,mi2,mi3;
MyFrame()
{
this.setSize(300,300);
this.setVisible(true);
this.setTitle("myFrame");
this.setBackground(Color.green);
mb=new MenuBar();
this.setMenuBar(mb);
m1=new Menu("new");
m2=new Menu("option");
m3=new Menu("edit");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mi1=new MenuItem("open");
mi2=new MenuItem("save");
mi3=new MenuItem("saveas");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
}
public void actionPerformed(ActionEvent ae)
{
label=ae.getActionCommand();

repaint();
}
public void paint(Graphics g)
{
Font f=new Font("arial",Font.BOLD,25);
g.setFont(f);
g.drawString("Selected item .... "+label,50,200);
}
}
class Demo
{
public static void main(String[] args)
{
MyFrame f=new MyFrame();
}
};
------------------------------------------------------------------------------------------------------------------------
----------------
*****MOUSELISTENER INTERFACE**********
import java.awt.*;
import java.awt.event.*;
class myframe extends Frame implements MouseListener
{
String[] msg=new String[5];
myframe()
{
this.setSize(500,500);
this.setVisible(true);
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
msg[0]="mouse clicked ..... ("+e.getX()+","+e.getY()+")";
repaint();
}
public void mousePressed(MouseEvent e)
{
msg[1]="mouse pressed ..... ("+e.getX()+","+e.getY()+")";
repaint();
}
public void mouseReleased(MouseEvent e)
{
msg[2]="mouse released ..... ("+e.getX()+","+e.getY()+")";
repaint();
}
public void mouseEntered(MouseEvent e)
{
msg[3]="mouse entered ..... ("+e.getX()+","+e.getY()+")";
repaint();
}
public void mouseExited(MouseEvent e)
{
msg[4]="mouse exited ..... ("+e.getX()+","+e.getY()+")";
repaint();
}
public void paint(Graphics g)
{
int X=50;
int Y=100;
for(int i=0;i<msg.length;i++)
{
if (msg[i]!=null)
{
g.drawString(msg[i],X,Y);
Y=Y+50;
}
}
}
};
class Demo
{
public static void main(String[] args)
{
myframe f=new myframe();
}
};
------------------------------------------------------------------------------------------------------------------------
----------------
*******ITEMLISTENER INTERFACE**********
import java.awt.*;
import java.awt.event.*;
class myframe extends Frame implements ItemListener
{
String qual="",gen="";
Label l1,l2;
CheckboxGroup cg;
Checkbox c1,c2,c3,c4,c5;
Font f;
myframe()
{
this.setSize(300,400);
this.setVisible(true);
this.setLayout(new FlowLayout());
l1=new Label("Qualification: ");
l2=new Label("Gender: ");
c1=new Checkbox("BSC");
c2=new Checkbox("BTECH");
c3=new Checkbox("MCA");
cg=new CheckboxGroup(); c4=new Checkbox("Male",cg,false);
c5=new Checkbox("Female",cg,true);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);
c5.addItemListener(this);
this.add(l1);
this.add(c1);
this.add(c2);
this.add(c3);
this.add(l2);
this.add(c4);
this.add(c5);
}
public void itemStateChanged(ItemEvent ie)
{
if(c1.getState()==true)
{
qual=qual+c1.getLabel()+",";
}
if(c2.getState()==true)
{
qual=qual+c2.getLabel()+",";
}
if(c3.getState()==true)
{
qual=qual+c3.getLabel()+",";
}
if(c4.getState()==true)
{
gen=c4.getLabel();
}
if(c5.getState()==true)
{
gen=c5.getLabel();
}
repaint();
}
public void paint(Graphics g)
{
Font f=new Font("arial",Font.BOLD,20);
g.setFont(f);
this.setForeground(Color.green);
g.drawString("qualification ----- >"+qual,50,100);
g.drawString("gender ------------- >"+gen,50,150);
qual="";
gen="";

}
}
class rc
{
public static void main(String[] args)
{
myframe f=new myframe();
}
};
------------------------------------------------------------------------------------------------------------------------
----------------
*********KEYLISTENER INTERFACE***********
import java.awt.*;
import java.awt.event.*;
class myframe extends Frame
{
myframe()
{
this.setSize(400,400);
this.setVisible(true);
this.setBackground(Color.green);
this.addKeyListener(new keyboardimpl());
}
};
class keyboardimpl implements KeyListener
{
public void keyTyped(KeyEvent e)
{
System.out.println("key typed "+e.getKeyChar());
}
public void keyPressed(KeyEvent e)
{
System.out.println("key pressed "+e.getKeyChar());
}
public void keyReleased(KeyEvent e)
{
System.out.println("key released "+e.getKeyChar());
}
}
class Demo
{
public static void main(String[] args)
{
myframe f=new myframe();
}
};

***********CHECK LIST AND CHOICE************


import java.awt.*;
import java.awt.event.*;
class myframe extends Frame implements ItemListener
{
Label l1,l2;
List l;
Choice ch;
String[] tech;
String city="";
myframe()
{
this.setSize(300,400);
this.setVisible(true);
this.setLayout(new FlowLayout());
l1=new Label("Technologies: ");
l2=new Label("City: ");
l=new List(3,true);
l.add("c");
l.add("c++");
l.add("java");
l.addItemListener(this);
ch=new Choice();
ch.add("hyd");
ch.add("chenni");
ch.add("Banglore");
ch.addItemListener(this);
this.add(l1);
this.add(l);
this.add(l2);
this.add(ch);
}
public void itemStateChanged(ItemEvent ie)
{
tech=l.getSelectedItems();
city=ch.getSelectedItem();
repaint();
}
public void paint(Graphics g)
{
Font f=new Font("arial",Font.BOLD,20);
g.setFont(f);
String utech="";
for(int i=0;i<tech.length ;i++ )
{
utech=utech+tech[i]+" ";
}
g.drawString("tech --------"+utech,50,200);
g.drawString("city --------- "+city,50,300);
utech="";
}}
class Demo
{
public static void main(String[] args)
{
myframe f=new myframe();
}};

*********AdjustmentListener**********
import java.awt.*;
import java.awt.event.*;
class myframe extends Frame implements AdjustmentListener
{
Scrollbar sb;
int position;
myframe()
{
this.setSize(400,400);
this.setVisible(true);
this.setLayout(new BorderLayout());
sb=new Scrollbar(Scrollbar.VERTICAL);
this.add("East",sb);
sb.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
position=sb.getValue();
}
public void paint(Graphics g)
{
g.drawString("position:"+position,100,200);
repaint();
}
}
class scrollbarex
{
public static void main(String[] args)
{
myframe f=new myframe();
}
};

You might also like