Sunteți pe pagina 1din 46

**************************

Area and perimeter of a circle


**************************

import java.io.*;
abstract class cal
{
final float PI=3.14f; //Declaring PI
}
//implementing interface
interface functions
{
//Declaring methods
public float area();
public float perimeter();
}
//Class for circle
class Circle extends cal implements functions
{
float rad;
void cir(float rad)
{
this.rad=rad;
}
public float area()
{
return PI*rad*rad;
}
public float perimeter()
{
return 2*PI*rad;
}
}
//Main class
class area
{
public static void main(String args[])
{
String s;
float radius;
try
{
System.out.println("Enter the value for radius: ");
//Creating Buffered Reader to get value during runtime
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

s=br.readLine();
radius=Float.parseFloat(s); //Typecasting String to Float
Circle c=new Circle(); //Creating an object
c.cir(radius);
float ar=c.area();
float pr=c.perimeter();

//Printing result
System.out.println("Area of a circle : "+ar);
System.out.println("Perimeter of a circle : "+pr);
}
catch(IOException ie){System.out.println(ie);}
}
}
OUTPUT:

>java area
Enter the value for radius
5
Area of a circle : 78.5
Perimeter of a circle : 31.400002

>java area
Enter the value for radius
6
Area of a circle : 113.04
Perimeter of a circle : 37.68

>java area
Enter the value for radius
7
Area of a circle : 153.86002
Perimeter of a circle : 43.960003

>java area
Enter the value for radius
8
Area of a circle : 200.96
Perimeter of a circle : 50.24
***********************
Using String Buffer class
***********************

import java.io.*;
class strbuf
{
public static void main(String ar[])
{
StringBuffer sb=new StringBuffer("jav");
System.out.println("Before insert:");
System.out.println(sb);
System.out.println("After insert:");
System.out.println(sb.insert(0,"Welcome to "));
System.out.println("After Append:");
System.out.println(sb.append('a'));
System.out.println("Length of the string:");
System.out.println(sb.length());
System.out.println("Change the char");
System.out.println(sb.substring(0,5));
System.out.println("After delete: "+sb.delete(0,5));
System.out.println("After replace: "+sb.replace(0,2,"Welcome"));
}
}
OUTPUT:

>java strbuf
Before insert:
jav
After insert:
Welcome to jav
After Append:
Welcome to java
Length of the string:
15
Change the char
Welco
After delete: me to java
After replace: Welcome to java
***************************
Usage of Random Class
***************************

import java.util.*;
import java.lang.*;
public class rand
{
public static void main(String arg[])
{
int e;
Random n=new Random();
int y[]=new int[6];
int a;
int no=Integer.parseInt(arg[0]);
for(int i=0;i<5;i++)
{
e=Math.abs(n.nextInt());
a=e%10;
y[i]=a;
System.out.println("a="+a);
}
for(int i=0;i<5;i++)
{
if(y[i]==no)
{
System.out.println("You are a Lucky Winner");
System.exit(0);
}
}
System.out.println("You are not a lucky winner");
}
}
OUTPUT:

>java rand 3
a=0
a=0
a=3
a=4
a=6
You are a Lucky Winner

>java rand 9
a=1
a=8
a=2
a=5
a=0
You are not a lucky winner

>java rand 2
a=9
a=0
a=8
a=9
a=2
You are a Lucky Winner
***************************
Implementation of Point Class
****************************

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*<applet code="impoint" width=200 height=200>


</applet>*/

public class impoint extends Applet


{
Point p;
int w=0;
int h=0;
int x1=100;
int y1=100;
Image theImage;
public void init()
{
p=new Point(10,10);
theImage=getImage(getDocumentBase(),"Lotus.jpg");
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
p.move(me.getX(),me.getY());
x1=p.x;
y1=p.y;
w+=5;
h+=5;
repaint();
}
});
}
public void paint(Graphics g)
{
g.drawImage(theImage,x1,y1,w,h,this);
showStatus(getCodeBase().toString());
}
}
OUTPUT:
********************************
Implementation of Calendar Class
********************************

import java.io.*;
import java.util.*;
class calend
{
public static void main(String args[])
{
int numdays=0;
Calendar c=Calendar.getInstance();
GregorianCalendar gc=new GregorianCalendar();
String month[ ]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
String m=Integer.toString(c.get(Calendar.MONTH)+1);
String d=Integer.toString(c.get(Calendar.DATE));
String y=Integer.toString(c.get(Calendar.YEAR));
System.out.println("Calendar for the Year - "+y);
System.out.print("Month of ");
System.out.println(month[gc.get(Calendar.MONTH)]);
System.out.print("Current Date: ");
System.out.println(m+"-"+d+"-"+y);
String h=Integer.toString(c.get(Calendar.HOUR));
String i=Integer.toString(c.get(Calendar.MINUTE));
String s=Integer.toString(c.get(Calendar.SECOND));
System.out.print("Current Time: ");
System.out.println(h+" : "+i+" : "+s);
System.out.println();
if(gc.isLeapYear(c.get(Calendar.YEAR)))
System.out.println("The current year is a leap year"+"\n");
else
System.out.println("The current year is not a leap year"+"\n");
switch(c.get(Calendar.MONTH)+1)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
numdays=31;
break;
}
case 4:
case 6:
case 9:
case 11:
{
numdays=30;
break;
}
case 2:
{
if(gc.isLeapYear(gc.get(Calendar.YEAR)))
numdays=29;
else
numdays=28;
break;
}
}

System.out.println("SU MO TU WE TH FR SA");
int dow=gc.get(Calendar.DAY_OF_WEEK);
int td=gc.get(Calendar.DATE);
int diff=(dow-(td%7));
if(diff<0)
{ diff=diff+7; }
switch(diff)
{
case 1:
{
System.out.print(" ");
break;
}
case 2:
{
System.out.print(" ");
break;
}
case 3:
{
System.out.print(" ");
break;
}
case 4:
{
System.out.print(" ");
break;
}
case 5:
{
System.out.print(" ");
break;
}
case 6:
{
System.out.print(" ");
break;
}
}
for(int day=1;day<=numdays;day++)
{
if(((day+diff)%7)==0)
{
if(day<10){System.out.println(" "+day+" ");}
else {System.out.println(day+" ");}
}
else
{
if(day<10){System.out.print(" "+day+" ");}
else{System.out.print(day+" ");}
}
}
}
}
OUTPUT:

>java calend
Calendar for the Year - 2008
Month of February
Current Date: 2-21-2008
Current Time: 5: 25: 27
The current year is a leap year

SU MO TU WE TH FR SA
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29
*********************************
String Manipulation using char Array
********************************

//String Manipulation using char array


import java.lang.*;

//Creating stringclass
class strmanip
{
public static void main(String args[])
{
System.out.println("Welcome 2 String Manipulation");
String s1="Welcome 2 ";
char ch[]={'j','a','v','a'};
String s2=new String(ch);
String s3="Live";
System.out.println("Length:"+s1.length());
System.out.println("Given String : "+s2);
System.out.println("Length:"+s2.length());
System.out.println("Concatenated String : "+s1.concat(s2));
StringBuffer s=new StringBuffer("Be Always");
System.out.println("Before Insertion:"+s);
s.insert(2," Happy");
System.out.println("After insertion:"+s);
System.out.println("Before Replace:"+s3);
String s4=s3.replace('v','f');
System.out.println("After Replace : "+s4);
String upper=s2.toUpperCase();
String lower=s2.toLowerCase();
System.out.println("Upper Case : "+upper);
System.out.println("Lower Case : "+lower);
}
}
OUTPUT:

>java strmanip
Welcome 2 String Manipulation
Length:10
Given String : java
Length:4
Concatenated String : Welcome 2 java
Before Insertion:Be Always
After insertion:Be Happy Always
Before Replace:Live
After Replace : Life
Upper Case : JAVA
Lower Case : java
*****************
Database creation
*****************

import java.util.*;
import java.sql.*;
public class database
{
public static void main(String a[]) throws SQLException
{
Connection c;
Statement s;
ResultSet r;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:Employee.dsn");
s=c.createStatement();
r=s.executeQuery("select * from Emp");
System.out.println("Number\tName\t\tEmail\t\tContact\n");
while(r.next())
{
System.out.println(r.getString("Emp_No")+
"\t"+r.getString("Emp_name")+"\t\t"+r.getString("Emp_email")+
"\t\t"+r.getString("Emp_tel"));
}
}
catch(Exception e){System.out.println("Table not found");}
}
}
OUTPUT:

>java database

Number Name Email Contact No


1000 Lakshmi luck_mi@hotmail.com 22598730
1001 Vignesh wickey@yahoo.com 22396510
1002 Abirami abi_24@yahoo.com 22466108
**********************
Usage of Vector classes
**********************

//Program to illustrate Vector class

import java.util.*;
import java.io.*;

class vect
{
public static void main(String a[])
{
String s;
int rad;
Vector v=new Vector(5,2);
System.out.println("Initial size:"+v.size());
System.out.println("Initial capacity:"+v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Double(5));
v.addElement(new Double(6));

System.out.print("After adding 6 elements ");


System.out.println("The initial size is:"+v.size());
System.out.println("The current capacity is:"+v.capacity());

v.addElement(new Integer(7));
v.addElement(new Float(8));
v.addElement(new Integer(9));
v.addElement(new Integer(10));
v.addElement(new Integer(11));

System.out.print("After adding 5 elements ");


System.out.println("The initial size is:"+v.size());
System.out.println("Current capacity:"+v.capacity());
System.out.println("First Element : "+(Integer)v.firstElement());
v.removeElementAt(0);
System.out.println("After Deleting the first Element:");

System.out.println("First Element : "+(Integer)v.firstElement());


System.out.println("Last Element : "+(Integer)v.lastElement());
System.out.println("Inserting the element into the 6th position:6.5");
v.insertElementAt(new Float(6.5),5);
System.out.println("Enter the integer value to be searched in your vector");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine();
rad=Integer.parseInt(s);
if(v.contains(new Integer(rad)))
System.out.println("Vector contains "+rad);
else
System.out.println("Vector does not contain "+rad);
}
catch(IOException ie){ }

Enumeration venum = v.elements();


System.out.println("\n Elements in vector");
while(venum.hasMoreElements())
System.out.println(venum.nextElement());
}
}
OUTPUT:

>java vect
Initial size:0
Initial capacity:5
After adding 6 elements The initial size is:6
The current capacity is:7
After adding 5 elements The initial size is:11
Current capacity:11
First Element : 1
After Deleting the first Element:
First Element : 2
Last Element : 11
Enter the integer value to be searched in your vector
3
Vector contains 3
Elements in vector
2
3
4
5.0
6.0
7
8.0
9
10
11
*****************************************************
Implementing thread based applications and Exception handling
*****************************************************

import java.util.*;
import java.lang.*;

class clicker implements Runnable


{
int click=0;
Thread t;
private volatile boolean running=true;

public clicker(int p)
{
t=new Thread(this);
t.setPriority(p);
}
public void run()
{
while(running)
{
click++;
}
}
public void start()
{
t.start();
}
public void stop()
{
running=false;
}
}

class prior
{
public static void main(String a[])
{
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
clicker hi=new clicker(Thread.MAX_PRIORITY-2);
clicker lo=new clicker(Thread.MIN_PRIORITY+5);
lo.start();
hi.start();
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
lo.stop();
hi.stop();

try
{
hi.t.join();
lo.t.join();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException Caught");
}
System.out.println("Low_priority thread: "+lo.click);
System.out.println("High_priority thread: "+hi.click);
}
}
OUTPUT:

>java prior
Low_priority thread: 481470775
High_priority thread: 599937989

>java prior
Low_priority thread: 5990756
High_priority thread: 621690068

>java prior
Low_priority thread: 463016430
High_priority thread: 597236667

>java prior
Low_priority thread: 471146769
High_priority thread: 597067840

>java prior
Low_priority thread: 458286114
High_priority thread: 597245940

>java prior
Low_priority thread: 414278057
High_priority thread: 593878183
****************************
Application using Synchronization
****************************

import java.io.*;
class Q
{
int n;
boolean value=false;
synchronized int get()
{
if(!value)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("Got:"+n);
value=false;
notify();
return n;
}
synchronized void put(int n)
{
if(value)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
this.n=n;
value=true;
System.out.println("Put:"+n);
notify();
}
}
class producer implements Runnable
{
Q q;
producer(Q q)
{
this.q=q;
new Thread(this,"producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class consumer implements Runnable
{
Q q;
consumer(Q q)
{
this.q=q;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class THsynch
{
public static void main(String a[])
{
Q q=new Q();
new producer(q);
new consumer(q);
System.out.println("PRESS CTRL+C TO STOP.");
}
}
OUTPUT:

>java THsynch
PRESS CTRL+C TO STOP.
Put:0
Got:0
Put:1
Got:1
Put:2
Got:2
Put:3
Got:3
Put:4
Got:4
Put:5
Got:5
Put:6
Got:6
Put:7
Got:7
Put:8
Got:8
Put:9
Got:9
Put:10
Got:10
***********************************
Working with Frames and Various Controls
***********************************
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*<applet code="comp.java" width=600 height=500>


</applet>*/

class control extends Frame implements ActionListener, ItemListener


{
String m="",msg="",msg1="",m1="",m2="",m3="";
String nm="",str1="",str2="";
Label l,l1,l2;
Button b,b1,b2;
Checkbox c1,c2,c3;
TextField t,t1;
CheckboxGroup cbg;
Panel p1,p2,p3,p4;
control(String st)
{
super(st);
MyWindAdap adapter = new MyWindAdap(this);
addWindowListener(adapter);
setLayout(new FlowLayout(FlowLayout.CENTER));
setBackground(Color.cyan);
setForeground(Color.red);
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
l=new Label("Name :",Label.CENTER);
p1.add(l);
t=new TextField(20);
p1.add(t);
l1=new Label("Password :",Label.CENTER);
p2.add(l1);
t1=new TextField(20);
t1.setEchoChar('*');
p2.add(t1);
add(p1);
add(p2);
l2=new Label("Area of interest :",Label.CENTER);
p3.add(l2);
cbg=new CheckboxGroup();
c1=new Checkbox("Games",cbg,false);
c2=new Checkbox("Listening to Music",cbg,true);
c3=new Checkbox("Reading books",cbg,false);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
p3.add(c1);
p3.add(c2);
p3.add(c3);
add(p3);
b=new Button("Ok");
b1=new Button("Cancel");
b2=new Button("Quit");
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
p4.add(b);
p4.add(b1);
p4.add(b2);
add(p4);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
nm=t.getText();
str1=t.getSelectedText();
str2=t1.getText();
if(str.equals("Ok"))
{
m="You pressed:"+str;
m1="Your Name : "+nm;
m2="You have selected : "+str1;
m3="Your Password :"+str2;
}
if(str.equals("Cancel"))
{
t.setText("");
t1.setText("");
}
if(str.equals("Quit"))
{
dispose();
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(m,120,120);
g.drawString(m1,120,140);
g.drawString(m2,120,160);
g.drawString(m3,120,180);
g.drawString(msg,120,200);
}
public void itemStateChanged(ItemEvent ie)
{
msg="You have checked : ";
msg+=cbg.getSelectedCheckbox().getLabel();
repaint();
}
}
class MyWindAdap extends WindowAdapter
{
control frame1;
public MyWindAdap(control frame1)
{
this.frame1=frame1;
}
public void windowClosing(WindowEvent we)
{
frame1.setVisible(false);
}
}
public class comp extends Applet
{
Frame fr;
public void init()
{
fr=new control("Using Frames and Various Controls");
fr.setSize(500,300);
}
public void start()
{
fr.setVisible(true);
}
public void stop()
{
fr.setVisible(false);
}
}
OUTPUT:
******************************
Working with Dialogs and Menus
******************************

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MenuDemo" width=600 height=600>
</applet>
*/

//create a subclass of Frame


class open extends Frame
{
open(String s)
{
super(s);//false represents modeless dialog box->active to all.
}
}

class sd extends Dialog implements ActionListener


{
sd(Frame parent,String tit)
{
super(parent,tit,false);
setLayout(new FlowLayout());
setSize(300,200);
add(new Label("Press this button"));
Button b;
add(b=new Button("Cancel"));
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
public void paint(Graphics g)
{
g.drawString("This is in the dialog box",10,70);
}
}
class MenuFrame extends Frame
{
String msg=" ";
CheckboxMenuItem debug, test;
MenuFrame(String title)
{
super(title);
//create menu bar and add it to frame
MenuBar mbar = new MenuBar();
setMenuBar(mbar);

//create the menu items


Menu file = new Menu("File");
MenuItem item1, item2, item3, item4, item5;
file.add(item1= new MenuItem("New..."));
file.add(item2= new MenuItem("open..."));
file.add(item3= new MenuItem("close"));
file.add(item4=new MenuItem("-"));
file.add(item5=new MenuItem("quit..."));
mbar.add(file);

Menu edit = new Menu("Edit");


MenuItem item6, item7,item8,item9;
edit.add(item6 = new MenuItem("cut"));
edit.add(item7 = new MenuItem("copy"));
edit.add(item8 = new MenuItem("paste"));
edit.add(item9 = new MenuItem("-"));

Menu sub = new Menu("special");


MenuItem item10, item11, item12;
sub.add(item10 =new MenuItem("First"));
sub.add(item11 =new MenuItem("second"));
sub.add(item12 =new MenuItem("Third"));
edit.add(sub);

//these are checkable menu items


debug = new CheckboxMenuItem("Debug");
edit.add(debug);
test = new CheckboxMenuItem("Testing");
edit.add(test);
mbar.add(edit);

//create an object to handle action and item events


MyMenuHandler handler = new MyMenuHandler(this);
//register is to receive those events
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
item11.addActionListener(handler);
item12.addActionListener(handler);
debug.addItemListener(handler);
test.addItemListener(handler);
//create an object to handle window events
MyWindowAdapter adapter = new MyWindowAdapter(this);

//register is to receive those events


addWindowListener(adapter);
}
public void paint(Graphics g)
{
g.drawString(msg,10,200);
if(debug.getState())
g.drawString("Debug is on.",10,220);
else
g.drawString("Debug is off.",10,220);
if(test.getState())
g.drawString("Testing is on.",10,240);
else
g.drawString("Testing is off.",10,240);
}
}
class MyWindowAdapter extends WindowAdapter
{
MenuFrame menuFrame;
open ss;
public MyWindowAdapter(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
public MyWindowAdapter(open ss)
{
this.ss=ss;
}
public void windowClosing(WindowEvent we)
{
menuFrame.setVisible(false);
ss.setVisible(false);
}
}
class MyMenuHandler implements ActionListener, ItemListener
{
MenuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
//Handle action events
public void actionPerformed(ActionEvent ae)
{
Dialog d;Frame f;
String msg="you selected ";
String arg= (String)ae.getActionCommand();
if(arg.equals("New..."))
{
msg += "New.";
f=new open("Dialogs");
f.setSize(100,100);
d=new sd(f,"Dialogs and Menus");
d.setVisible(true);
}
else if(arg.equals("open..."))
{
msg +="open.";
f=new open("FileDialog");
FileDialog fd=new FileDialog(f,"FileDialog");
fd.setVisible(true);
}
else if(arg.equals("close"))
msg +="Close";
else if(arg.equals("quit..."))
{
msg +="quit.";
menuFrame.setVisible(false);
}
else if(arg.equals("edit"))
msg +="edit.";
else if(arg.equals("cut"))
msg +="cut.";
else if(arg.equals("copy"))
msg +="copys.";
else if(arg.equals("paste."))
msg +="paste.";
else if(arg.equals("Find"))
msg +="Find.";
else if(arg.equals("Find Next"))
msg +="Find Next.";
else if(arg.equals("Replace"))
msg +="Replace.";
else if(arg.equals("Debug"))
msg +="Debug.";
else if(arg.equals("Testing"))
msg +="Testing.";
menuFrame.msg=msg;
menuFrame.repaint();
}
//Handle item events
public void itemStateChanged(ItemEvent ie)
{
menuFrame.repaint();
}
}

//create frame window.


public class MenuDemo extends Applet
{
Frame f;
public void init()
{
f = new MenuFrame("Menu Demo");
int width =Integer.parseInt(getParameter("width"));
int height=Integer.parseInt(getParameter("height"));
setSize(new Dimension(width, height));
f.setSize(width, height);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{
f.setVisible(false);
}
}
OUTPUT:
***************************
Working with Panel and Layout
***************************
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="lay.java" width=300 height=300>
</applet>
*/
public class lay extends Applet implements ActionListener
{
Panel p1,p2;
Button b1,b2;
Button b[]=new Button[6];
String msg="";
public void init()
{
p1=new Panel();
p2=new Panel();
b1=new Button("First");
b2=new Button("Second");
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
b1.addActionListener(this);
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
p2.setLayout(new GridLayout(2,3));
for(int i=0;i<6;i++)
{
b[i]=new Button("Panel2: "+(i+1));
b[i].addActionListener(this);
p2.add(b[i]);
}
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae)
{
String st=ae.getActionCommand();
if(st.equals("First"))
{
msg="you pressed first";
}
else if(st.equals("second"))
{
msg="u pressed second";
}
else
{
msg="u pressed"+ae.getActionCommand();
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}
OUTPUT:
**************************
Implementation of Graphics class
***************************

import java.awt.*;
import java.applet.*;

/*
<applet code="smile.java" width=750 height=450>
</applet>
*/

public class smile extends Applet


{
public void paint(Graphics g)
{
Font f=new Font("TimesNewRoman",Font.BOLD,30);
setBackground(Color.blue);
g.setFont(f);
g.setColor(Color.white);
g.drawString("My Smiling Face",80,90);
g.setColor(Color.yellow);
g.fillOval(200,150,200,200);
g.setColor(Color.black);
g.fillOval(240,200,20,20);
g.fillOval(335,200,20,20);
g.drawLine(300,210,280,270);
g.drawLine(280,270,310,270);
g.setColor(Color.red);
g.drawArc(250,250,100,60,180,180);
g.setColor(Color.yellow);
g.fillOval(393,200,28,28);
g.fillOval(179,200,28,28);
}
}
OUTPUT:
***************************
Working with Colors and Fonts
***************************

//Fonts and Colors


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="fc" width=750 height=700>
</applet> */
public class fc extends Applet implements ItemListener
{
Label f;
Choice li,li1;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.blue);
String FontChoice[];
li=new Choice();
li1=new Choice();
f=new Label("Fonts and Colors");
add(f);
GraphicsEnvironment
ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
FontChoice=ge.getAvailableFontFamilyNames();
for(int i=0;i<FontChoice.length;i++)
li.addItem(FontChoice[i]);

add(li,"center");
li1.addItem("red");
li1.addItem("blue");
li1.addItem("green");
li1.addItem("yellow");
li1.addItem("cyan");
li1.addItem("pink");
add(li1,"center");
li.addItemListener(this);
li1.addItemListener(this);
f.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent me)
{
Font fo=new Font("TimesNewRoman",Font.BOLD,10);
f.setFont(fo);
}
public void mouseExited(MouseEvent me)
{
Font fo1=new Font("CourierNew",Font.BOLD,12);
f.setFont(fo1);
}
});
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
String st="Currently selected in the first Choice :
"+li.getSelectedItem();
g.drawString(st,15,100);
String st1="Currently selected in the second Choice:
"+li1.getSelectedItem();
g.drawString(st1,15,120);
switch(li1.getSelectedIndex())
{
case 0:
setBackground(Color.red);
break;
case 1:
setBackground(Color.blue);
break;
case 2:
setBackground(Color.green);
break;
case 3:
setBackground(Color.yellow);
break;
case 4:
setBackground(Color.cyan);
break;
case 5:
setBackground(Color.pink);
break;
}
}
}
OUTPUT:

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