Monday, July 21, 2008

Mouse Events Handling in Java


Here is a sample code in Java to Handle the Mouse Events using Applets....




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


public class mouse extends Applet implements MouseListener,MouseMotionListener
{

public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}

public void mouseClicked(MouseEvent e)
{
showStatus("Mouse Clicked at "+e.getX()+" , "+e.getY());
}
public void mouseEntered(MouseEvent e)
{
showStatus("Mouse Entered at "+e.getX()+" , "+e.getY());
for(int i=0;i<100000;++i);
}
public void mouseExited(MouseEvent e)
{
showStatus("Mouse Exited at "+e.getX()+" , "+e.getY());
}
public void mouseReleased(MouseEvent e)
{
showStatus("Mouse Released at "+e.getX()+" , "+e.getY());
}
public void mouseDragged(MouseEvent e)
{
showStatus("Mouse Dragged at "+e.getX()+" , "+e.getY());
}
public void mouseMoved(MouseEvent e)
{
showStatus("Mouse Moved at "+e.getX()+" , "+e.getY());
}
public void mousePressed(MouseEvent e)
{
showStatus("Mouse Pressed at "+e.getX()+" , "+e.getY());
}
}

⁄*<applet code="mouse.class" width=500 height=400><⁄applet>*⁄

Finding Max number in HTML - Java Script


Here is an Example program to find the maximum number among 3 Numbers in HTML using JAVA Script...




<html>
<title>Mahendra Vakati<⁄title>
<head>
<script type = "text⁄javascript">

<!--

var x=window.prompt("Enter Any Number" , "0");
var y=window.prompt("Enter Any Number" , "0");
var z=window.prompt("Enter Any Number" , "0");

var p=parseInt(x);
var q=parseInt(y);
var r=parseInt(z);

var max = big(p,q,r);
document.write("The order of numbers"+p+" "+q+" "+ r);
document.writeln(" <b>Maximum Number is" +max+ "<⁄b>");

function big(a,b,c)
{
return Math.max(a,Math.max(b,c));
}
⁄⁄-->


<⁄script>
<⁄head>
<body bgcolor='skyblue'><marquee behavior='alternate'><h1>Mahendra Vakati<⁄marquee><⁄h1>
<p>click refresh for run script again<⁄p>
<⁄body>
<⁄html>

Puzzle


HI..


Here is the code for a simple "Puzzle"...



#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<process.h>
void box();
void game();
void check();
void write();
void mainmenu();
void preview();
void scores();

int a[6][6]= { {0,0,0,0,0,0},
{0,14,5,11,4,0},
{0,15,6,10,12,0},
{0,9,8,3,1,0},
{0,100,2,13,7,0},
{0,0,0,0,0,0}
};


int b[6][6]= { {0,0,0,0,0,0},
{0,1,2,3,4,0},
{0,8,7,6,5,0},
{0,9,10,11,12,0},
{0,100,15,14,13,0},
{0,0,0,0,0,0}
};


char menu[5][25] = { " 1.Levels Preview ",
" 2.Heighest Scores ",
" 3.Start Game ",
" 4.Exit "
};

int i,j,row,col,temp,count,nol=0;
union REGS ii,oo;
char ch;

main()
{

_setcursortype(_NOCURSOR);
clrscr();
mainmenu();
}⁄*end of main*⁄

void mainmenu()
{
int pos=9;
textcolor(11);
for(i=1;i<=26;i++)
{
gotoxy(25+i,7); cprintf("%c ",205);
gotoxy(25+i,17); cprintf("%c",205);
}

for(i=1;i<=10;i++)
{
gotoxy(25,7+i); cprintf("%c",186);
gotoxy(52,7+i); cprintf("%c",186);
}

gotoxy(25,7); cprintf("%c",201);
gotoxy(52,7); cprintf("%c",187);

gotoxy(25,17); cprintf("%c",200);
gotoxy(52,17); cprintf("%c",188);

for(i=0;i<4;i++)
{
textcolor(10);
gotoxy(32,9+(i*2));
cprintf("%s",menu[i]);
textcolor(12);
gotoxy(33,9+(i*2));
cprintf("%c",menu[i][1]);
textcolor(11);
gotoxy(35,9+(i*2));
cprintf("%c",menu[i][3]);

}


while(1)
{

for(i=0;i<4;i++)
{
textbackground(0);
gotoxy(27,9+(i*2));
cprintf(" ");
}

gotoxy(27,pos);
textcolor(9);
cprintf(" \3\3\3");

while(!kbhit());
int86(22,&ii,&oo);
ch = oo.h.ah;

switch(ch)
{

case 72 : if(pos==9)
pos = 15;
else
pos-=2;
break;

case 80 : if(pos==15)
pos=9;
else
pos+=2;
break;
case 28 :
if(pos==9)
preview();
else
if(pos==11)
scores();
else
if(pos==13)
clrscr(),game();
else
exit(2);
break;
case 2 :
case 38 :pos=9;break;
case 3 :
case 35 :pos=11;break;
case 4 :
case 31 :pos=13;break;
case 1 :exit(3);
case 5 :
case 18 :pos=15;break;

}⁄⁄end of switch
}⁄⁄end of while

getch();
}

void preview()
{
clrscr();

gotoxy(32,6);textcolor(3);cprintf("LEVEL 1");
gotoxy(32,7);textcolor(13);cprintf("========");
textcolor(6);
gotoxy(28,10);cprintf("1 2 3 4");
gotoxy(28,12);cprintf("5 6 7 8");
gotoxy(28,14);cprintf("9 10 11 12");
gotoxy(28,16);cprintf("13 14 15 ");
gotoxy(30,18);textcolor(15);cprintf("Press Any Key ..");
getch(),clrscr();


gotoxy(32,6);textcolor(3);cprintf("LEVEL 2");
gotoxy(32,7);textcolor(13);cprintf("========");
textcolor(6);
gotoxy(28,10);cprintf(" 15 14 13 ");
gotoxy(28,12);cprintf(" 12 11 10 9 ");
gotoxy(28,14);cprintf(" 8 7 6 5 ");
gotoxy(28,16);cprintf(" 4 3 2 1 ");
gotoxy(30,18);textcolor(15);cprintf("Press Any Key ..");
getch(),clrscr();


gotoxy(32,6);textcolor(3);cprintf("LEVEL 3");
gotoxy(32,7);textcolor(13);cprintf("========");
textcolor(6);
gotoxy(28,10);cprintf("1 5 9 13 ");
gotoxy(28,12);cprintf("2 6 10 14 ");
gotoxy(28,14);cprintf("3 7 11 15 ");
gotoxy(28,16);cprintf("4 8 12 ");
gotoxy(30,18);textcolor(15);cprintf("Press Any Key ..");
getch(),clrscr();

gotoxy(32,6);textcolor(3);cprintf("LEVEL 4");
gotoxy(32,7);textcolor(13);cprintf("========");
textcolor(6);
gotoxy(28,10);cprintf(" 12 8 4 ");
gotoxy(28,12);cprintf(" 15 11 7 3 ");
gotoxy(28,14);cprintf(" 14 10 6 2 ");
gotoxy(28,16);cprintf(" 13 9 5 1 ");
gotoxy(30,18);textcolor(15);cprintf("Press Any Key ..");
getch(),clrscr();

gotoxy(32,6);textcolor(3);cprintf("LEVEL 5");
gotoxy(32,7);textcolor(13);cprintf("========");
textcolor(6);
gotoxy(28,10);cprintf(" 1 2 3 4 ");
gotoxy(28,12);cprintf(" 8 7 6 5 ");
gotoxy(28,14);cprintf(" 9 10 11 12 ");
gotoxy(28,16);cprintf(" 15 14 13 ");
gotoxy(28,18);textcolor(2);cprintf("Press Any Key ..");
getch(),clrscr(),mainmenu();

}⁄* end of Previw function *⁄


void scores()
{
clrscr();

gotoxy(23,9);
textcolor(2);
cprintf("V.MAHENDRA REDDY ---- 50,000");
gotoxy(23,12);
cprintf("MAHI ---- 40,000");
gotoxy(23,15);
cprintf("K.B.NAVEEN KUMAR ---- 30,000");
gotoxy(23,18);
cprintf("G.SAMPATH KUMAR REDDY ---- 30,000");

gotoxy(25,21);
textcolor(7);
cprintf("Presss Any Key ....");
getch(),clrscr();
mainmenu();
}

void game()
{
clrscr();
box();
write();

textcolor(5);
gotoxy(3,20);
cprintf("Esc - Quit");

while(1)
{
⁄* Printing Values*⁄
textcolor(12);
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
{
gotoxy(22+(j*5),10+(i*2));
if(a[i][j]==100)
{
printf(" ");
row=i;
col=j;
}
else
cprintf("%d",a[i][j]);
}

check();

⁄* Reading Key*⁄
while(!kbhit());
int86(22,&ii,&oo);
ch = oo.h.ah;

switch(ch)
{
case 77:if(col==1) ⁄*Right*⁄
{
gotoxy(55,21);
textcolor(4);
cprintf("Invalid Choice");
}
else
{
gotoxy(55,20);
textcolor(4);
cprintf(" ");
temp = a[row][col-1];
a[row][col]= temp;
a[row][col-1]=100;
}

break;
case 75: if(col==4) ⁄*Left*⁄
{
gotoxy(55,21);
textcolor(4);
cprintf("Invalid Choice");
}
else
{
gotoxy(55,21);
textcolor(4);
cprintf(" ");
temp = a[row][col+1];
a[row][col]= temp;
a[row][col+1]=100;
}

break;
case 72: if(row==4) ⁄*Top*⁄
{
gotoxy(55,21);
textcolor(4);
cprintf("Invalid Choice");
}
else
{
gotoxy(55,21);
textcolor(4);
cprintf(" ");
temp = a[row+1][col];
a[row][col]= temp;
a[row+1][col]=100;

}
break;
case 80:if(row==1) ⁄*Bottom*⁄
{
gotoxy(55,21);
textcolor(4);
cprintf("Invalid Choice");
}
else
{
gotoxy(55,21);
textcolor(4);
cprintf(" ");
temp = a[row-1][col];
a[row][col]= temp;
a[row-1][col]=100;
}
break;

case 1 :gotoxy(22,23);
textcolor(7);
cprintf("Are You Sure To Quit (Y⁄N)::");
ch = getche();
if(ch=='y'||ch=='Y')
clrscr(),mainmenu();
else
{
gotoxy(22,23),textbackground(0);
cprintf(" ");
}

}
}⁄* End of While*⁄
}⁄* End of Game *⁄

void write()
{
if(nol==0)
{
gotoxy(32,9);textcolor(3);cprintf("Level 1");
textcolor(6);
gotoxy(3,2);cprintf("1 2 3 4");
gotoxy(3,4);cprintf("5 6 7 8");
gotoxy(3,6);cprintf("9 10 11 12");
gotoxy(3,8);cprintf("13 14 15 ");
}

if(nol==1)
{
gotoxy(32,9);textcolor(3);cprintf("Level 2");
textcolor(6);
gotoxy(3,2);cprintf(" 15 14 13 ");
gotoxy(3,4);cprintf(" 12 11 10 9 ");
gotoxy(3,6);cprintf(" 8 7 6 5 ");
gotoxy(3,8);cprintf(" 4 3 2 1 ");
}

if(nol==2)
{
gotoxy(32,9);textcolor(3);cprintf("Level 3");
textcolor(6);
gotoxy(3,2);cprintf("1 5 9 13 ");
gotoxy(3,4);cprintf("2 6 10 14 ");
gotoxy(3,6);cprintf("3 7 11 15 ");
gotoxy(3,8);cprintf("4 8 12 ");
}

if(nol==3)
{
gotoxy(32,9);textcolor(3);cprintf("Level 4");
textcolor(6);
gotoxy(3,2);cprintf(" 12 8 4 ");
gotoxy(3,4);cprintf(" 15 11 7 3 ");
gotoxy(3,6);cprintf(" 14 10 6 2 ");
gotoxy(3,8);cprintf(" 13 9 5 1 ");
}

if(nol==4)
{
gotoxy(32,9);textcolor(3);cprintf("Level 5");
textcolor(6);
gotoxy(3,2);cprintf(" 1 2 3 4 ");
gotoxy(3,4);cprintf(" 8 7 6 5 ");
gotoxy(3,6);cprintf(" 9 10 11 12 ");
gotoxy(3,8);cprintf(" 15 14 13 ");
}

}

void check()
{
int ok=0;

⁄* Checking*⁄

if (nol==0)
{
for(i=1,temp=1,count=0;i<=4;i++)
for(j=1;j<=4;j++,temp++)
if(a[i][j]==temp)
count++;

if(count>=15)
{
gotoxy(28,22);
textcolor(2+BLINK);
cprintf("CONGRATULATIONS");
nol++;
ok=1;
getch();
}
}

if(nol==1)
{
for(i=4,temp=1,count=0;i>=1;i--)
for(j=4;j>=1;j--,temp++)
if(a[i][j]==temp)
count++;

if(count>=15)
{
gotoxy(28,22);
textcolor(2+BLINK);
cprintf("CONGRATULATIONS");
ok=1;
nol++;
getch();
}
}

if(nol==2)
{
for(i=1,temp=1,count=0;i<=4;i++)
for(j=1;j<=4;j++,temp++)
if(a[j][i]==temp)
count++;

if(count>=15)
{
gotoxy(28,22);
textcolor(2+BLINK);
cprintf("CONGRATULATIONS");
ok=1;
nol++;
getch();
}
}

if(nol==3)
{
for(i=4,temp=1,count=0;i>=1;i--)
for(j=4;j>=1;j--,temp++)
if(a[j][i]==temp)
count++;

if(count>=15)
{
gotoxy(28,22);
textcolor(2+BLINK);
cprintf("CONGRATULATIONS");
ok=1;
nol++;
getch();
}
}

if(nol==4)
{
for(i=1,count=0;i<=4;i++)
for(j=1;j<=4;j++)
if(a[i][j]==b[i][j])
count++;

if(count==16)
{
gotoxy(28,22);
textcolor(2+BLINK);
cprintf("CONGRATULATIONS");
ok=1;
nol++;
getch();
}
}

if(ok==1)
{

if(nol==5)
{
gotoxy(20,23);
textcolor(14);
cprintf(" You Have Finished All The Levels ");
getch();
exit(1);
}
else
{
gotoxy(20,23);
textcolor(14);
cprintf("You Have To Finish %d Levels Only ...",5-nol);
getch();
}

gotoxy(20,23);textbackground(0);
cprintf(" ");
gotoxy(28,23);cprintf(" ");
game();
}
}⁄*end of function*⁄


void box()
{
int i;
textcolor(10);

for(i=1;i<=20;i++)
{
gotoxy(25+i,11); cprintf("%c",205);
gotoxy(25+i,13); cprintf("%c",205);
gotoxy(25+i,15); cprintf("%c",205);
gotoxy(25+i,17); cprintf("%c",205);
gotoxy(25+i,19); cprintf("%c",205);
}

for(i=1;i<=7;i++)
{
gotoxy(25,11+i); cprintf("%c",186);
gotoxy(30,11+i); cprintf("%c",186);
gotoxy(35,11+i); cprintf("%c",186);
gotoxy(40,11+i); cprintf("%c",186);
gotoxy(45,11+i); cprintf("%c",186);
}


gotoxy(25,11); cprintf("%c",201);
gotoxy(45,11); cprintf("%c",187);

gotoxy(25,19); cprintf("%c",200);
gotoxy(45,19); cprintf("%c",188);

gotoxy(30,11); cprintf("%c",203);
gotoxy(35,11); cprintf("%c",203);
gotoxy(40,11); cprintf("%c",203);

gotoxy(30,19); cprintf("%c",202);
gotoxy(35,19); cprintf("%c",202);
gotoxy(40,19); cprintf("%c",202);

gotoxy(25,13); cprintf("%c",204);
gotoxy(25,15); cprintf("%c",204);
gotoxy(25,17); cprintf("%c",204);

gotoxy(45,13); cprintf("%c",185);
gotoxy(45,15); cprintf("%c",185);
gotoxy(45,17); cprintf("%c",185);


for(i=0;i<6;i+=2)
{
gotoxy(30,13+i); cprintf("%c",206);
gotoxy(35,13+i); cprintf("%c",206);
gotoxy(40,13+i); cprintf("%c",206);
}
}⁄⁄end of box

Single Linked Lists

Hi All,

Many people know about Data Structures. In that Linked Lists and Trees will be the crucial concepts.... I know most of u people know this... but Here iam posting this for those who are not able to write code....





#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

class S_List
{
float info;
S_List *link;
S_List *first;
public :
S_List()
{
first= NULL;
}
void Create();
void Insert();
void Delete();
void Display();
};⁄⁄end of S_List class

void S_List :: Create()
{
char ch;
S_List *temp;

first=NULL;
temp = new(S_List);

do
{
if(first==NULL)
first = temp;

cout<<"\n Enter Information :: ";
cin>>temp->info;

cout<<"Do You Want To Insert Another Element (Y⁄N) :: ";
cin>>ch;
if(ch=='Y'||ch=='y')
{
temp->link = new(S_List);
temp = temp->link;
}
else
temp->link=NULL;
}while(ch=='Y'||ch=='y');
}⁄⁄end of Create

void S_List :: Insert()
{
int pos,i;
S_List *temp,*New,*pre;

New = new(S_List);

if(first==NULL)
{
cout<<"\n The List Is Not Created...";
getch();
return;
}

cout<<"Enter Information :: ";
cin>>New->info;
cout<<"Enter Position :: ";
cin>>pos;

if(pos==1)
{
New->link = first;
first = New;
}⁄⁄end of if
else
{
i=1;
temp = first;
while(temp!=NULL && i<pos)
{
pre = temp;
temp = temp->link;
i++;
}⁄⁄end of While
if(pos>i)
{
cout<<"\n The Position Exceeds The List Size ...";
getch();
return;
}
else
if(temp==NULL&&pos==i)
{
New->link = NULL;
pre->link = New;
}
else
{
New->link=pre->link;
pre->link=New;
}⁄⁄end of else
}⁄⁄end of else
cout<<"\n The Value Inserted At "<<pos<<" Position";
getch();
}⁄⁄end of Insert

void S_List :: Delete()
{
int pos;
float key;
S_List *temp,*pre;

if(first==NULL)
{
cout<<"\n There Is No List Created to Delete Any Element...";
getch();
return;
}

cout<<"\n Enter Key Element To Delete :: ";
cin>>key;

temp = pre = first;
pos=1;

while(temp!=NULL && temp->info!=key)
{
pre = temp;
temp = temp->link;
pos++;
}⁄⁄end of While

if(temp->info==key)
{
if(pos!=1)
{
pre->link = temp->link;
delete(temp);
}
else
{
first = first->link;
delete(temp);
}
cout<<"The Key Element Deleted at "<<pos<<" Position...";
}
else
cout<<"The Key Element Is Not Exist in the List....";
getch();
}⁄⁄end of Delete

void S_List :: Display()
{
S_List *temp;
temp = first;

if(temp==NULL)
{
cout<<"\n\n *** The List Is Empty *** ";
getch();
return;
}

cout<<"\n\n The List Is :: ";
while(temp!=NULL)
{
cout<<" "<<temp->info;
temp = temp->link;
}
getch();
}⁄⁄end of Display

void main()
{
int ch;
S_List obj;

while(1)
{
clrscr();
cout<<" 1.CREATE \n";
cout<<" 2.INSERT \n";
cout<<" 3.DELETE \n";
cout<<" 4.DISPLAY \n";
cout<<" 5.EXIT \n";
cout<<"Choice :: ";
cin>>ch;
switch(ch)
{
case 1 : obj.Create(); break;
case 2 : obj.Insert(); break;
case 3 : obj.Delete(); break;
case 4 : obj.Display(); break;
case 5 : exit(0);
default : cout<<"\n Invalid Choice"; getch();
}⁄⁄end of switch
}⁄⁄end of while
}⁄⁄end of main()