package GuIBasedProgram; import java.applet.*; import java.awt.*; import java.awt.event.*; public class Boxes extends Applet implements ActionListener { Button b1; Checkbox name1; Checkbox name2; public void init() { name1=new Checkbox("Red",null,false); name2=new Checkbox("Blue",null,false); b1=new Button("Submit"); add(name1); add(name2); add(b1); b1.addActionListener(this); } |
CheckBox Demonstration
Button Demonstration
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonClass extends Applet implements ActionListener {
Button red,white,blue;
Label hit;
public void init()
{
red=new Button("Red");
white=new Button("White");
blue=new Button("Blue");
hit=new Label("Hit a Button to Change the Screen Color");
add(red);
add(blue);
add(white);
add(hit);
import java.awt.*;
import java.awt.event.*;
public class ButtonClass extends Applet implements ActionListener {
Button red,white,blue;
Label hit;
public void init()
{
red=new Button("Red");
white=new Button("White");
blue=new Button("Blue");
hit=new Label("Hit a Button to Change the Screen Color");
add(red);
add(blue);
add(white);
add(hit);
Use of MouseMotionListner
import java.applet.*; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; public class MouseMotionEx extends Applet implements MouseMotionListener { int xcord; int ycord; public void init() { addMouseMotionListener(this); } |
Playing audio clips
import java.applet.*; import java.lang.*; import java.net.URL; public class AudioDemo extends Applet { AudioClip aud_clip; public void init() { aud_clip=getAudioClip(getDocumentBase(),"chicken.au"); } |
Constructor Overloading
//Demonstration Of Constructor OverLoading
public class Rect {
int l,b;
Rect() //Constructor 1
{
l=10;
b=5;
}
Rect(int l,int b) //Constructor 2
{
this.b=b;
this.l=l;
}
public class Rect {
int l,b;
Rect() //Constructor 1
{
l=10;
b=5;
}
Rect(int l,int b) //Constructor 2
{
this.b=b;
this.l=l;
}
Usage of Font and Color class
import java.awt.*;
import java.applet.Applet;
public class ColorFont extends Applet
{
public void init()
{
Color color1=new Color(230,220,0);
setBackground(color1);
}
public void paint(Graphics g)
{
String str="";
String FontList[];
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
FontList = ge.getAvailableFontFamilyNames();
g.drawString("Font Available ARE :", 5, 30);
for(int i=0;i {
str+=FontList[i]+",";
g.drawString(str, 5, 50);
}
import java.applet.Applet;
public class ColorFont extends Applet
{
public void init()
{
Color color1=new Color(230,220,0);
setBackground(color1);
}
public void paint(Graphics g)
{
String str="";
String FontList[];
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
FontList = ge.getAvailableFontFamilyNames();
g.drawString("Font Available ARE :", 5, 30);
for(int i=0;i
str+=FontList[i]+",";
g.drawString(str, 5, 50);
}
Simple Event Handing
import java.awt.*;
import java.awt.event.*;
class EventHandling extends Frame implements ActionListener{
TextField tf;
EventHandling(){
tf=new TextField();
tf.setBounds(60,50,170,20);
import java.awt.event.*;
class EventHandling extends Frame implements ActionListener{
TextField tf;
EventHandling(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Bubble Sort
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
Largest among Three Numbers
import java.util.Scanner;
class Largest
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
class Largest
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
Multiple Class
class Computer {
Computer() {
System.out.println("Constructor of Computer class.");
}
void computer_method() {
System.out.println("Power gone! Shut down your PC soon...");
}
public static void main(String[] args) {
Computer my = new Computer();
Laptop your = new Laptop();
my.computer_method();
your.laptop_method();
}
}
Computer() {
System.out.println("Constructor of Computer class.");
}
void computer_method() {
System.out.println("Power gone! Shut down your PC soon...");
}
public static void main(String[] args) {
Computer my = new Computer();
Laptop your = new Laptop();
my.computer_method();
your.laptop_method();
}
}
Static Block
class StaticBlock {
public static void main(String[] args) {
System.out.println("Main method is executed.");
}
static {
System.out.println("Static block is executed before main method.");
}
}
public static void main(String[] args) {
System.out.println("Main method is executed.");
}
static {
System.out.println("Static block is executed before main method.");
}
}
Even Or Odd
import java.util.Scanner;
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
Get Input From User using Scanner
import java.util.Scanner;
class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;
Scanner in = new Scanner(System.in);
class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;
Scanner in = new Scanner(System.in);
Try-Catch-Finally
class trycatchfinally
{
public static void main(String[] args) {
try {
int a,b,c;
a=5;
b=0;
c=a/b;
}
catch (Exception e) {
System.out.println("Divide By Zero");
}
{
public static void main(String[] args) {
try {
int a,b,c;
a=5;
b=0;
c=a/b;
}
catch (Exception e) {
System.out.println("Divide By Zero");
}
Try-Catch Block
public class trycatch {
public static void main(String args[]){
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
public static void main(String args[]){
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
Addition of Two Numbers Using Applet
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Label;
public class add extends Applet implements ActionListener{
TextField text1,text2,output;
Label label1,label2,label3,title;
Button button,clear;
public void init(){
setLayout(null);
import java.applet.*;
import java.awt.event.*;
import java.awt.Label;
public class add extends Applet implements ActionListener{
TextField text1,text2,output;
Label label1,label2,label3,title;
Button button,clear;
public void init(){
setLayout(null);
Draw lines,Rectangles And Ovals
import java.awt.*;
import java.applet.*;
public class draw extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<=250;i++)
{
Color c1=new Color(35-i,55-i,110-i);
g.setColor(c1);
import java.applet.*;
public class draw extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<=250;i++)
{
Color c1=new Color(35-i,55-i,110-i);
g.setColor(c1);
Suspend And Resume Thread
class susresThread implements Runnable
{
String n;
Thread thrd;
boolean suspended;
susresThread()
{
thrd=new Thread(this,"Suspend Resume Thread");
suspended=false;
thrd.start();
}
{
String n;
Thread thrd;
boolean suspended;
susresThread()
{
thrd=new Thread(this,"Suspend Resume Thread");
suspended=false;
thrd.start();
}
Hello Java
class Hello
{
public static void main(String args[])
{
System.out.println("Hello Java..");
}
}
{
public static void main(String args[])
{
System.out.println("Hello Java..");
}
}
Command Line
class comline
{
public static void main(String args[])
{
int count,i=0;
String s;
count=args.length;
System.out.println("Number of Argument ="+count);
{
public static void main(String args[])
{
int count,i=0;
String s;
count=args.length;
System.out.println("Number of Argument ="+count);
Thread Priority
class ThreadOne extends Thread
{
public void run()
{
try
{
for(int i=0;i<5;i++)
{
System.out.println("Thread 1 : i ="+i);
Thread.sleep(500);
}
}
catch(Exception e)
{
}
}
}
{
public void run()
{
try
{
for(int i=0;i<5;i++)
{
System.out.println("Thread 1 : i ="+i);
Thread.sleep(500);
}
}
catch(Exception e)
{
}
}
}
Thread Methods : yield(),stop(),sleep()
class D extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
if(i==0) yield(); //Transfer Control to B when i=0
System.out.println("From Class D i ="+i);
}
}
}
{
public void run()
{
for(int i=0;i<5;i++)
{
if(i==0) yield(); //Transfer Control to B when i=0
System.out.println("From Class D i ="+i);
}
}
}
Thread Test
class A extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("From Thread A :"+i);
}
}
}
Creating Thread
public class PingPong extends Thread {
private String word; // What word to print
private int delay; // how long to pause
public PingPong(String whatToSay, int delayTime) {
word = whatToSay;
delay = delayTime;
}
private String word; // What word to print
private int delay; // how long to pause
public PingPong(String whatToSay, int delayTime) {
word = whatToSay;
delay = delayTime;
}
Smiley
import java.awt.*;
import java.applet.*;
public class smiley extends Applet
{
public void init()
{
setBackground(Color.gray);
}
import java.applet.*;
public class smiley extends Applet
{
public void init()
{
setBackground(Color.gray);
}
Banner
import java.awt.*;
import java.applet.*;
public class Banner extends Applet implements Runnable{
String str="This is a Banner.........";
Thread t;
boolean b;
public void init()
{
setBackground(Color.gray);
setBackground(Color.yellow);
}
public void start()
{
import java.applet.*;
public class Banner extends Applet implements Runnable{
String str="This is a Banner.........";
Thread t;
boolean b;
public void init()
{
setBackground(Color.gray);
setBackground(Color.yellow);
}
public void start()
{
Subscribe to:
Posts (Atom)