Documente Academic
Documente Profesional
Documente Cultură
Lines
The drawLine() method takes two pair of
coordinates (x1,y1) and (x2,y2) as arguments
and draws a line between them.
Ex: g.drawLine(10,10,50,50);
Rectangles
This method takes four arguments. The first
two represents the x and y coordinates of the
top left corner of the rectangle, and the
remaining two represents the width and height
of the rectangle.
Ex. g.drawRect(10,60,40,30);
g.fillRect(60,10,30,80);
Draw oval
The drawOval method takes four arguments
the first two represent the top left corner of the
imaginary rectangle and the other two
represents the width and height of the oval
itself.
g.drawOval(20,20,200,120);
Draw circle
??
Drawing arcs
The drawArc() designed to draw arcs takes siz
arguments. The first four are the same as the
arguments for drawOval() method and the last
two represent the starting angle of the arc and
the number of degrees around the arc.
g.drawArc(10,10,100,100,0,180)
4.
Rectangle
drawRoundRect() and fillRoundRect() these
two methods are similar to drawRect() and
fillRect() except that they take two extra
arguments representing the width and height of
the angle of corners.
G.drawRoundRect(10,10,10,10,5,5)
Setting font
Font()
Setting Font
import java.applet.*;
import java.awt.*;
/
public class MyApplet extends Applet {
public void paint(Graphics g )
{
Font f=new Font("Times New Roman",Font.ITALIC,22);
g.setFont(f);
g.drawString("APPLET PROGRAMMING", 100, 100);
}
Drawing polygons
Polygons are shapes with many sides.
The end of the first line is the beginning of the
second line, the end of the second is the
beginning of the third, and so on.
bhi
Year
2000
2002
2004
Turnover (Rs
Crores)
110
150
200
AWT controls
Label
Labels are passive controls.
Label()
Label(String str)
Label(String str, int how)
Label.LEFT
Label.RIGHT
Label.CENTER
AWT controls
void setText(String str)
String getText()
AWT controls
public class Demo extends Applet
{
public void init()
{
Label one=new Label("One");
Label two=new Label("Two");
Label three=new Label("Three");
add(one);
add(two);
add(three);
}
}
Check boxes
Checkbox()
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbgroup)
Checkbox(String str, CheckboxGroup cbgroup, boolean on)
Methods
Current state of a checkbox
boolean getState()
To set state
void setState(boolean on)
Retrieve label
String getLabel()
To set label
void setLabel(String str)
Checkbox: Program
public class Checkboxdemo extends Applet implements ItemListener{
String msg="";
Checkbox C,OOP,JAVA;
public void init()
{
C=new Checkbox("C programming",null,true);
OOP=new Checkbox("C++ programming");
JAVA=new Checkbox("JAVA programming");
add(C);
add(OOP);
add(JAVA);
C.addItemListener(this);
OOP.addItemListener(this);
JAVA.addItemListener(this);
}
CheckboxGroup
Mutually exclusive checkboxes in which one
and only one checkbox in the group can be
checked at any one time.
CheckboxGroup: Program
public class Checkboxgroupdemo extends Applet implements ItemListener{
String msg="";
Checkbox C,OOP,JAVA;
CheckboxGroup cbg;
public void init()
{
Cbg=new CheckboxGroup();
C=new Checkbox("C programming",cbg,true);
OOP=new Checkbox("C++ programming,cbg,false);
JAVA=new Checkbox("JAVA programming,cbg,false);
add(C);
add(OOP);
add(JAVA);
C.addItemListener(this);
OOP.addItemListener(this);
JAVA.addItemListener(this);
}
Choice control
It creates a pop-up of items from which the
user may choose.
To add a selection to the list,
void addItem(String name)
void add(String name)
int getItemCount()
void select(int index)
void select(String name)
String getItem(int index)
Using lists
The list class provides a compact, multiplechoice, scrolling selection list.
List()// only one item to be selected
List(int numRows)// no of entries in the list
that always be visible
List(int numRows,boolean multipleSelect)
Methods
Methods
Example List:
public class ListDemo extends Applet implements
ActionListener{
List languages;
String msg="";
public void init()
{
languages=new List(3, true);
languages.add("C");
languages.add("C++");
languages.add("JAVA");
add(languages);
languages.addActionListener(this);
}
Buttons
A push button is a component that contains a
label and that generates an event when it is
pressed.
Button()
Button(String str)
Buttons
void setLabel(String str)
String getLabel()
Each time button is pressed an action event is
generated.
Using textfield
TextField()
TextField(int numchars)
TextField(String str)
TextField(String str,int numchars)
String getText()
void setText(String str)
String getSelectedText()
void select(int startindex, int endindex)
boolean isEditable()
void setEditable(boolean canEdit)
Example
public class TextFieldDemo extends Applet implements ActionListener{
TextField name,pass;
public void init()
{ Label namep=new Label("Name:", Label.RIGHT);
Label passp=new Label("Password:", Label.RIGHT);
name=new TextField(12);
pass=new TextField(10);
pass.setEchoChar('?');
add(namep);
add(name);
add(passp);
add(pass);
name.addActionListener(this);
pass.addActionListener(this);
}
Program
public class buttonex extends Applet implements ActionListener{
Button red,blue;
public void init()
{
red=new Button("Red");
blue=new Button("Blue");
add(red);
add(blue);
red.addActionListener(this);
blue.addActionListener(this);
}
TextArea
TextArea()
TextArea(int numLine, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str , int numLines,int numChars, int
sBars)
Layout manager
Automatically arranges controls within a window.
Flow layout
FlowLayout is the default layout manager.
Text editor
Left to right, top to bottom
FlowLayout()
FlowLayout(int how)
Flow layout
flowLayout is the default layout manager.
Text editor
Left to right, top to bottom
Flow layout
flowLayout is the default layout manager.
Text editor
Left to right, top to bottom
FlowLayout()
FlowLayout(int how)
FlowLayout.Left, FlowLayout.RIGHT,
FlowLayout.CENTER
FlowLayout(int how, int horz, int vert)
BorderLayout
BorderLayout()
BorderLayout(int h,int v);
Grid Layout
GridLayout();
GridLayout(int R,int C);
GridLayout(int R, int C, int h, int v);