How to use Button in java applet with the use of ActionListener?

Hello Friends, 

Today I will explain how can we use Button in java applet.
In this  program I used simple Button name but you can write or put different Buttons in your java applet program.
In this program i used ActionListener event hendling class.

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

/*


*/

public class nine extends Applet implements ActionListener
{
String msg="";
Button yes,no,maybe;
public void init()
{
yes=new Button("Yes");
no=new Button("No");
maybe=new Button("MayBe");

add(yes);
add(no);
add(maybe);

yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();

if(str.equals("Yes"))
msg="You press Yes....";
else if(str.equals("No"))
msg="You press No....";
else
msg="You press Maybe....";

repaint();
}

public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}

How to use Label in java applet?

Hello Friends, 

Today I will explain how can we use Label in java applet.
In this  program I used simple label name but you can write or put different labels in your java applet program.

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

/*


*/

public class sixth extends Applet
{
String msg="";
public void init()
{
Label one=new Label("one");
Label two=new Label("two");
Label three=new Label();

three.setText("three");

add(one);
add(two);
add(three);

msg=one.getText();
}
public void paint(Graphics g)
{
setBackground(Color.black);
setForeground(Color.white);

g.setFont(new Font("Corbel",Font.BOLD,30));
g.drawString(msg,20,80);
}
}

How to use keyPressed & keyReleased Method of KeyListener in java applet?

Hello Friends, 

Today I will explain how can we write the keylistener program in java applet.
In this  program I used keyPressed & keyReleased methods of KeyListener.

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

/*


*/

public class fifth extends Applet implements KeyListener
{
String msg="";
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
msg +=ke.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
setBackground(Color.black);
setForeground(Color.blue);

g.setFont(new Font("Lucida Sans Typewriter",Font.BOLD,25));
g.drawString(msg,20,50);
}
}

How to change Font size,style & type in java applet?

Hello Friends, 

Today I will explain how can we change font size,style,type in java applet.
In this  program I have change font size,style,type.

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

/*


*/

public class fourth extends Applet
{
String msg="";
public void init()
{
msg="hay....";
setBackground(Color.pink);
setForeground(Color.black);

msg +="whts up???";
}
public void paint(Graphics g)
{
g.setFont(new Font("Comic Sans MS",Font.BOLD,30));
g.drawString(msg,50,70);
}
}

How to print color text in java applet?

Hello Friends, 


Today I will explain how can we print color text in java applet.
In this  program I have used different methods to print text in java applet.
for example in Strings, with veriables etc.


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


*/

public class third extends Applet
{
String msg="";

public void init()
{
setBackground(Color.blue);
setForeground(Color.red);

msg="hi...";
System.out.print("nt"+msg);
}
public void start()
{
msg=msg+"whts up????";
}
public void paint(Graphics g)
{
msg=msg+"i hope u r enjoying an applet...";
g.drawString(msg,25,25);
}
}

how to write Java Applet lifecycle program (init,start,stop,distroy).

Hello Friends, 


Today I will explain how can we write Java Applet lifecycle program.
In this  program I have print init,start,stop & distroy method.


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


*/

public class second extends Applet
{
String str1="";

public void init()
{
str1 +="init::";
System.out.println("nt "+str1);
}
public void start()
{
str1 +="start::";
System.out.println("nt "+str1);
}
public void stop()
{
str1 +="stop::";
System.out.println("nt "+str1);
}
public void destroy()
{
str1 +="destroy.";
System.out.println("nt "+str1);
}

public void paint(Graphics g)
{
g.drawString(str1,10,25);
}
}

How to print Text in Java Applet?

Hello Friends, 


Today I will explain how can we print “HELLO” or Any text in Java Applet.
In this  program I have print only “HELLO” but you can choose any text you like.

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


*/

public class first extends Applet
{
public void paint(Graphics g)
{
g.drawString("first applet..........",100,200);
}
}

How to write a program for Insertion Sort & Bubble Sort in Core Java ?

Hello Friends, 

Today I will give you an example for writing a program for Insertion Sort. 

Java Code :

public class ins_sort {
public static void main(String[] args) {
int i;
 int a[] = {40,20,60,30,50,10};
System.out.println(” insertion sorting…..nn”); 
System.out.println(“Before the sort:n”);  
 for(i = 0; i < a.length; i++)
System.out.print( a[i]+”  “);
System.out.println();
 insertion_srt(a, a.length);  
 System.out.print(“Values after the sort:n”);  
 for(i = 0; i
 System.out.print(a[i]+”  “);
System.out.println(); 
 }

 public static void insertion_srt(int a[], int n){
 
 for (int i = 1; i < n; i++){
 
 int j = i;
 
 int B = a[i];
 
 while ((j > 0) && (a[j-1] > B)){
 a[j] = a[j-1];
 j–;
 }
 a[j] = B;
 }
 
}
}

Output :

 insertion sorting…..


Before the sort:

40  20  60  30  50  10  
Values after the sort:

10  20  30  40  50  60  



And this example is  for writing a program for Bubble Sort in java.



Java Code :

public class bubble {
static int a[]={40,50,20,10,30,60};
public static void main(String[] args) {
System.out.println(“Before sorting…”);
display();
bubblesort();
System.out.println(“After sorting…”);
display();
}
public static void bubblesort(){
int temp;
for(int i=0;i
{
for(int j=0;j
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
public static void display()
{
for(int i=0;i
System.out.println(” “+a[i]);
}
}
}

Output :

Before sorting…
 40
 50
 20
 10
 30
 60
After sorting…
 10
 20
 30
 40
 50
 60

How to write a program for Method ,print simple array in core java.?

Program for Method demo in core java :

Hello Friends, 

Today I will give you an example for writing a program for display how to use method in core java.

Code:

public class Methoddemo {
public static void main (String[] args)
{
int a=80;
int b=290;
int c=Sum(a,b);
System.out.println(“The sum of “+a+” and “+b+”  is =”+c);
}
public static int Sum(int num1,int num2)
{
int ans;
ans=num1+num2;
return ans;
}

}

Output :

The sum of 80 and 290  is =370

Program for print simple array in core java :

Hello Friends, 

Today I will give you an example for writing a program for print simple array in core java.

Code:

public class sample2darray {

public static void main(String args[])

{

int a[][]=new int [3][3];

int k=1;

System.out.print(“nStoring the numbers in array”);

for(int i=0;i<3 div="" i="">

{
for(int j=0;j<3 div="" j="">

{
a[i][j]=k+1;
k=k+1;
}
}
System.out.println(“nYou have stored…”);
for(int i=0;i<3 div="" i="">

{
for(int j=0;j<3 div="" j="">

{
System.out.print(” “+a[i][j]);
}
System.out.println();
}
}
}

Output :

Storing the numbers in array
You have stored…
 2 3 4
 5 6 7
 8 9 10

Write a program for Linear Searching & Binary Searching Array in Java.

Program for linear searching array in core java :

Hello Friends, 

Today I will give you an example for writing a program for  Linear Search array in core java.

Code :

public class linear {
public static void main(String[] args) {
int a[]={1,4,2,6,-4,8,5};
System.out.println(“The element is at location”+Find(a,4));
System.out.println(“The element is at location”+Find(a,-4));
}
public static int Find(int[]a,int key)
{
int i;
for(i=0;i
{
if(a[i]==key)
return i+1;

}
return i-1;
}

}

Output:

The element is at location2
The element is at location5


Write a program for binary searching array in java :

Hello Friends, 

Today I will give you an example for writing a program for Binary Search array in core java.

Code :

public class binary {

public static void main(String[] args) {

int a[]={10,20,30,40,50,60};
int key=40;
Find(a,0,5,key);

}
public static void Find(int[]a,int low,int high,int key)
{
int mid;
if(low>high)
{
System.out.println(“Please Arrange elements in order…!!!”);
return;
}
mid=(low+high)/2;
if(key==a[mid])
System.out.println(“n the element is at the position=”+(mid+1));
else
if(key
Find(a,low,mid-1,key);
else
if(key>a[mid])
Find(a,mid+1,high,key);
}

}

Output :

 the element is at the position=4