Unit 3 Java
Unit 3 Java
(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
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).
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
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
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
3) To provide title to the Frame explicitly we have to use the following method
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.
********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");
}
};
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”);
ch.remove(“HYD”);
ch.remove(“BANGALORE”);
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
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:-
It will display the three items size and it is allow selecting the only single item.
It will display the five items and it is allow selecting the multiple items.
Methods:-
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:-
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:-
cb1.setLable(“BTECH”);
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:-
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.
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.
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.
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. VERTICAL ScrollBar
2. HORIZONTAL ScrollBar
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();
}
};
*********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();
}
};