follow

Friday, 30 August 2013

Painting in Applet : JAVA Programming by "Gopal Krishna"

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

public class pnt extends Applet implements MouseMotionListener
{

public void init()
{
addMouseMotionListener(this); 
}
public void MouseDraged(MouseEvent e)   

{
Graphics g=getGraphics(); 
g.fillOval(e.getX(),e.getY(),5,5);
}
}


/*

<applet code=pnt.class height=800 width=1200>
</applet>

*/

Event handling in Applet : JAVA Programming by "Gopal Krishna"

Single Button :


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

public class mst extends Applet implements ActionListener
{
TextField t1;
Button b1;
public void init()
{
t1=new TextField(10);
b1=new Button("ok");
add (t1);
add (b1);
b1.addActionListener(this);

}
public void actionPerformed(ActionEvent e)  //error 
{
if (e.getSource()==b1)
{
t1.setText("welcome");
}
}
}


/*

<applet code=mst.class height=800 width=1200>
</applet>

*/




Multi Button :

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

public class mst extends Applet implements ActionListener
{
TextField t1;
Button b1;
Button b2;
public void init()
{
t1=new TextField(15);
b1=new Button("CLICK IFPAGAL HO KYA ");
b2=new Button("CLICK IF NAHI HO");
add (t1);
add (b1);
add (b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)  //error 
{
if (e.getSource()==b1)
{
t1.setText("PTAA THA");
}
if (e.getSource()==b2)
{
t1.setText("Pagal mt bnaao");
}





}
}


/*

<applet code=mst.class height=800 width=1200>
</applet>

*/

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

    public class mst1 extends Applet implements ActionListener
    {
        TextField t1;
        TextField t2;
        TextField t3;
        Button b1;
        Button b2;
       
        public void init()
        {
            t1=new TextField(10);
            t2=new TextField(10);
            t3=new TextField(10);
            b1=new Button("sum");
            b2=new Button("sub");
            add(t1);add(t2);add(t3);
            add(b1);
            add(b2);
            t1.setText("0");
            t2.setText("0");
            t3.setText("0");
            b1.addActionListener(this);
            b2.addActionListener(this);
        }
        public void actionPerformed(ActionEvent e)
        {
             try
            {
                String s1,s2,s;
                int a=0;int b=0;int c=0;int d=0;
                s1=t1.getText();
                s2=t2.getText();
                a=Integer.parseInt(s1);
                b=Integer.parseInt(s2);
                c=a+b;
                d=a-b;
           
       
           
                if (e.getSource()==b1)
            {
           
                t3.setText(""+c);
            }
            if (e.getSource()==b2)
            {
                t3.setText(""+d);
            }
           
            }
            catch (Exception ex)
            {
                System.out.println("kuch to gadbad h");
            }
        }
}


/*

<applet code=mst1.class height=800 width=1200>
</applet>

*/

Parameter passed in Applet : JAVA Programming by "Gopal Krishna"

import java.applet.Applet;
import java.awt.Graphics;

public class gk extends Applet
{

public void paint(Graphics g)
{
String str=getParameter("msg");
g.drawString(str,50, 50);
}

}

/*

<applet code=gk.class height=800 width=1200>
</applet>

*/

Moving CAR in Applet : JAVA Programming by "Gopal Krishna"

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

public class car extends Applet
{
Image pic;
public void init()
{
pic=getImage(getDocumentBase(),"cl.jpg"); 
}

public void paint(Graphics g)

for(int i=0;i<1000;i++)
{
g.drawImage(pic,i,10,this);
try
{
Thread.sleep(10);

}
catch(Exception e)
{

}
}
}
}

/*

<applet code=car.class height=800 width=1200>
</applet>

*/

Saturday, 24 August 2013

Get image in Applet : JAVA Programming by "Gopal Krishna"

import java.applet.*;
import java.awt.*;
public class apple extends Applet
{
Image pic;
public void init()
{
pic=getImage(getDocumentBase(),"fn.jpg");
}
public void paint(Graphics g)
{
g.drawImage(pic,20,20,this);
} }
/*
<applet code=apple.class height=800 width=600>
</applet>
*/

Methods of graphics class of Applet : JAVA Programming by "Gopal Krishna"


import java.applet.*;
import java.awt.*;
public class apple extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("welcome anonimous",150,200);
g.drawRect(30,30,50,50);
g.setColor(Color.blue);
g.fillRect(50,50,50,50);
g.drawOval(50,190,50,40);
g.fillOval(190,50,50,70);
g.drawLine(250,90,50,40);
}
}
/*
<applet code=apple.class height=300 width=300>
</applet>
*/

Thursday, 22 August 2013

How we run a simple Applet program :JAVA Programming by "Gopal Krishna"

How we run Applet:


1.By HTML file.

2.By Applet viewer.

One example for both :

import java.applet.*;

import java.awt.*;


public class apple extends Applet

{

public void paint(Graphics g)

{

g.drawString("hello",90,100);

}

}


/*<applet code=apple.class height=300 width=300>

</applet>*/

Or write html program for external browser and no need to write comment applet code ...  


<html>
<head><title>applet eg.</title>
</head>
<body>
<applet code=apple.class height=300 width=300>
</applet>>
</body>
</html>

Applet Introduction :JAVA Programming by "Gopal Krishna"

comming soon .........

Garbage Collector :JAVA Programming by "Gopal Krishna"

Comming sooonnnn ..........

Daemon Thread :JAVA Programming by "Gopal Krishna"

coming soooon ....

Setting a Thread priority :JAVA Programming by "Gopal Krishna"

comming sooooooooooooooon ........

Joining a Thread :JAVA Programming by "Gopal Krishna"

// sleep method


public class thr4 extends Thread{ int i; public void run() { for (i=1;i<6;i++) { System.out.println(i); try { Thread.sleep(1000); } catch(Exception e) { System.out.println(e); } } } public static void main (String arg[]) { thr3 x1= new thr3(); thr3 x2= new thr3(); thr3 x3= new thr3(); x1.start(); try { x1.join(); } catch(Exception e) { System.out.println(e); } x2.start(); try { x2.join(); } catch(Exception e) { System.out.println(e); } x3.start();
} }


Thread Scheduler :JAVA Programming by "Gopal Krishna"

// sleep method

public class thr3 extends Thread
{ int i;
public void run()
{
for (i=1;i<6;i++)
{
System.out.println(i);

try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}

}
public static void main (String arg[])
{
thr3 x= new thr3();
x.start();

}
}

Thread life cycle and creation :JAVA Programming by "Gopal Krishna"


// by extending thread class

public class thr1 extends Thread
{
public void run()
{
System.out.println("thread runing");
}
public static void main (String arg[])
{
thr1 x= new thr1();
x.start();
}
}



// by implementig runnable interface 


public class thr2 implements Runnable 

{

public void run()

{

System.out.println("thread runing");

}

public static void main (String arg[])

{

thr2 x= new thr2();

Thread t=new Thread(x);

t.start();

}

}

Multi Threading :JAVA Programming by "Gopal Krishna"

comming soon ...

Custom Exception :JAVA Programming by "Gopal Krishna"

comin soon ............

Friday, 9 August 2013

Scanner class :JAVA Programming by "Gopal Krishna"



import java.util.*;

public class ut
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);

System.out.println("enterthe 1st no.=");
int a=s.nextInt();

System.out.println("enter the 2st no.=");
int b=s.nextInt();

int c=a+b;

System.out.println("enterthe 1st no.="+c);
}
}

Use of "throw and throws" for handling the Exception block :JAVA Programming by "Gopal Krishna"

If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.

You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the different in throws and throw keywords.

The following method declares that it throws a Remote Exception:

// errord :

import java.io.*;
public class abc
{
public int disp(int age) throws invalidageException
{
if (age>18)

{
system.out.println("valid voter");
}

else
{
throw new invalidageException("not valid");

}

}
public static void main (String arg[])


{
abc x= new abc();
x.disp(15);
}
}

Use of "finally" for handling the Exception block :JAVA Programming by "Gopal Krishna"


The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.
Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.

A finally block appears at the end of the catch blocks and has the following syntax:


try 
{
   //Protected code
}catch(ExceptionType1 e1)
{
   //Catch block
}catch(ExceptionType2 e2)
{
   //Catch block
}catch(ExceptionType3 e3)
{
   //Catch block
}finally
{
   //The finally block always executes.

}




public class ab
{
public static void main(String arg[])
{
System.out.println("Enter the amount"+arg[0]);
try
{
int a=Integer.parseInt(arg[0]);
if (a>100)
{
System.out.println("valid amount");
}

}
catch(Exception e)
{
System.out.println("enter the valid amount which is more then 100");
}
finally
{
System.out.println("thnks you for using our servie");
}

}
}

Thursday, 8 August 2013

Use of "try catch"" for handling the Exception block :JAVA Programming by "Gopal Krishna"

Catching Exceptions:


A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try
{
   //Protected code
}catch(ExceptionName e1)
{
   //Catch block

}


1.

public class ex1
{
public static void main(String arg[])
{
String s1="abc";
try
{
int a =Integer.parseInt(s1);
System.out.println(a);
}
catch(Exception e)
{
System.out.println(e);
}
}

}


2.

public class ab
{
public static void main(String arg[])
{
try
{
int a=Integer.parseInt(arg[0]);
}
catch(Exception e)
{
System.out.println("no invalid");
}
System.out.println("no valid");
}
}

Multiple catch Blocks:

A try block can be followed by multiple catch blocks. The syntax for multiple catch blocks looks like the following:

try
{
   //Protected code
}catch(ExceptionType1 e1)
{
   //Catch block
}catch(ExceptionType2 e2)
{
   //Catch block
}catch(ExceptionType3 e3)
{
   //Catch block
}


Wednesday, 7 August 2013

Handling Exception : JAVA Programming by "Gopal Krishna"

1. Try .
2. Catch .
3. Finally .
4. Throws
5. Throws .

Herarkey of exception : JAVA Programming by "Gopal Krishna"


Exception handling in JAVA : JAVA Programming by "Gopal Krishna"

An exception is a problem that arises during the execution of a program. 

An exception can occur for many different reasons, including the following:

1. A user has entered invalid data.

2. A file that needs to be opened cannot be found.

A network connection has been lost in the middle of communications or the JVM has run out of memory.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

To understand how exception handling works in Java, you need to understand the three categories of exceptions:

1. Checked exceptions.
2. Runtime exceptions.
3. Errors.

Checked exceptions: 

A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

Runtime exceptions: 

A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.

Errors: 

These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

Math class : JAVA Programming by "Gopal Krishna"

public class mt
{
public static void main(String arg[])
{

int result=Math.abs(-98);
System.out.println("the value="+result);
System.out.println("the abs(-199)value="+Math.abs(-199));
System.out.println("the ciel(3.1)value="+Math.ceil(3.1f));
System.out.println("the floor(3.1value="+Math.floor(3.1f));
System.out.println("the max(1,99)value="+Math.max(1,99));
System.out.println("the min(1,99)value="+Math.min(1,99));
System.out.println("the round(38.975));value="+Math.round(38.975));
System.out.println("the random(0,1)value="+Math.random()*1);
System.out.println("the sqrt(16)value="+Math.sqrt(16));
System.out.println("the sin(45)value="+Math.sin(45));
System.out.println("the cos(45)value="+Math.cos(45));
System.out.println("the tan(45)value="+Math.tan(45));
System.out.println("the value="+Math.pow(2,4));

}
}