Sunteți pe pagina 1din 65

Graphics programming

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)

Write applets to draw the following shapes:


1. Square inside a circle
2. Circle inside a square
3. Human face

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.

First method to draw polygon


import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet {
public void paint(Graphics g )
{
g.drawLine(10,20,170,40);
g.drawLine(170,40,80,140);
g.drawLine(80,140,10,20);
}
}

Second method to draw Polygon


import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet {
public void paint(Graphics g )
{
g.setColor(Color.GREEN);
int x[]={10,100,50};
int y[]={10,100,60};
int k=x.length;
g.drawPolygon(x, y, k);
}
}

Second method to draw Polygon


import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet {
public void paint(Graphics g )
{
g.setColor(Color.GREEN);
int x[]={10,100,50};
int y[]={10,100,60};
int k=x.length;
Polygon p=new Polygon(x,y,k);
g.drawPolygon( p);
}
}

Third method to draw Polygon


addPoint(x,y);

Third method to draw Polygon


import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet {
public void paint(Graphics g )
{
Polygon p=new Polygon();
p.addPoint(20,20);
p.addPoint(60,20);
p.addPoint(60,60);
p.addPoint(20,60);
g.drawPolygon(p);
}
}

Write applets to draw the following shapes:


1. Square inside a circle
2. Circle inside a square
3. Write a program to display your name
continuously on increasing size of fonts as
shown below.
sura

bhi

Write applets to draw the following shapes:


1. Draw a barchart

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);
}

public void itemStateChanged(ItemEvent ie){


repaint();
}
public void paint(Graphics g)
{
msg="Current state:";
g.drawString(msg,10,100);
msg="C Programming" + C.getState();
g.drawString(msg,20,100);
msg= "C++ Programming:" + OOP.getState();
g.drawString(msg,30,100);
msg="JAVA:" +JAVA.getState();
g.drawString(msg,40,100);
}
}

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);
}

public void itemStateChanged(ItemEvent ie){


repaint();
}
public void paint(Graphics g)
{
msg="Current selection:";
Msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,40,100);
}
}

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)

To determine which item is currently selected,


you may call either getSelectedItem() or
getSelectedIndex().
String getSelectedItem()
int getSelectedIndex()

int getItemCount()
void select(int index)
void select(String name)
String getItem(int index)

public class Choicedemo extends Applet


implements ItemListener{
Choice languages;
String msg="";
public void init(){
languages=new Choice();
languages.add("C");
languages.add("C++");
languages.add("Java");
add(languages);
languages.addItemListener(this);
}

public void itemStateChanged(ItemEvent ie){


repaint();
}
public void paint(Graphics g)
{
msg="Current language:";
msg+=languages.getSelectedItem();
g.drawString(msg,6,120);
}
}

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

void add(String name)


void add(string name, int index)
String getSelectedItem()
Int getSelectedIndex()
Int getItemCount()
void select(int index)
String getItem(int index)

Methods

void remove(int position)


void remove(String item)
void removeAll()
void replaceItem(String newvalue,int index)

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);
}

public void actionPerformed(ActionEvent ae){


repaint();
}

public void paint(Graphics g){


int idx[];
msg="current language:";
idx=languages.getSelectedIndexes();
for(int i=0;i<idx.length;i++)
msg+=languages.getItem(idx[i])+ " ";
g.drawString(msg,6,120);
}

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.

public class button1 extends Applet implements


ActionListener {
Button b1,b2;
String msg="";
public void init() {
Button b1=new Button("YES");
Button b2=new Button("NO");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)


{
String str=ae.getActionCommand();
if(str.equals("YES"))
msg="User has pressed YES";
else if(str.equals(NO))
msg="User has pressed NO";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,40,10);
}

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)

void setEchoChar(char ch)


boolean echoCharIsSet()
char getEchoChar()

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);
}

public void actionPerformed(ActionEvent ae){


repaint();
}
public void paint(Graphics g){
g.drawString("Name:"+ name.getText(),6,60);
g.drawString("Selected text", +
name.getSelectedText(),6,80);
g.drawString("password:", +pass.getText(), 6,100);
}

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);
}

public void actionperformed(ActionEvent ae)


{
String str=ae.getActionCommand();
if(str.equals("Red")){
setBackground(Color.red);
}
else if(str.equal("Blue")){
setBackground(Color.blue);
}
repaint();
}

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

FlowLayout()//leaves 5 pixels between


components
FlowLayout(int how)

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)

public class flowlayoutex extends Applet implements


ItemListener{
String msg=" ";
Checkbox C,CPP,Java;
public void init(){
setLayout (new FlowLayout(FlowLayout.LEFT));
C=new Checkbox("C",null,true);
CPP=new Checkbox("C++");
Java=new Checkbox("JAVA");
add(C);
add(CPP);
add(Java);
C.addItemListener(this);
CPP.addItemListener(this);
Java.addItemListener(this);}

public void itemStateChanged(ItemEvent ie){


repaint();
}
public void paint(Graphics g)
{
msg="Current language:";
g.drawString(msg,6,80);
msg="C:" + C.getState();
g.drawString(msg,6,100);
msg="C++:" + C++.getState();
g.drawString(msg,6,120);
msg="Java:" + Java.getState();
g.drawString(msg,6,140);
}

BorderLayout
BorderLayout()
BorderLayout(int h,int v);

Border layout defines the following constants


that specify the regions.
BorderLayout.CENTER
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH

void add(Component compObj, Object region)

public class borderlayoutex extends Applet{


public void init(){
setLayout(new BorderLayout());
add(new Button("TOP"),BorderLayout.NORTH);
add(new Button("BOTTOM"),BorderLayout.SOUTH);
add(new
TextArea("CENTER,BorderLayout.CENTER);
}
}

Grid Layout
GridLayout();
GridLayout(int R,int C);
GridLayout(int R, int C, int h, int v);

public void init(){


setLayout(new GridLayout(2,2,10,10));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
}

S-ar putea să vă placă și