Total Pageviews

Friday, May 3, 2019

keyboard handling

1.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Key" width=300 height=400>
</applet>
*/
public class Key extends Applet
implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents--->";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}


<applet code="Key" width=200 height=60>
</applet>







Example2:


import java.awt.*;

import java.applet.*;



public class Key1 extends Applet

{

    int keyPressed;



    public void init()

    {

        keyPressed = -1;



        Font font =  new Font("TimesRoman", Font.BOLD, 144);

        setFont(font);



        resize(200, 200);

    }



    public void paint(Graphics g)

    {

        String str = "";



        if (keyPressed != -1)

        {

            str += (char)keyPressed;

            g.drawString(str, 40, 150);

        }

    }



    public boolean keyDown(Event evt, int key)

    {

        keyPressed = key;

        repaint();

        return true;

    }

}

<applet code="Key1" width=200 height=60>
</applet>


mouse handling

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Mouse" width=500 height=500>
</applet>
*/
public class Mouse extends Applet
implements MouseListener,MouseMotionListener
{
int X=0,Y=20;
String msg="MouseEvents";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.black);
setForeground(Color.red);
}
public void mouseEntered(MouseEvent m)
{
setBackground(Color.magenta);
showStatus("Mouse Entered");
repaint();
}
public void mouseExited(MouseEvent m)
{
setBackground(Color.black);
showStatus("Mouse Exited");
repaint();
}
public void mousePressed(MouseEvent m)
{
X=10;
Y=20;
msg="mouse pressed";
setBackground(Color.green);
repaint();
}
public void mouseReleased(MouseEvent m)
{
X=10;
Y=20;
msg="mouse released";
setBackground(Color.blue);
repaint();
}
public void mouseMoved(MouseEvent m)
{
X=m.getX();
Y=m.getY();
msg="mouse moved";
setBackground(Color.white);
showStatus("Mouse Moved");
repaint();
}
public void mouseDragged(MouseEvent m)
{
msg="mouse dragged";
setBackground(Color.yellow);
showStatus("Mouse Moved"+m.getX()+" "+m.getY());
repaint();
}
public void mouseClicked(MouseEvent m)
{
msg="mouse clicked";
setBackground(Color.pink);
showStatus("Mouse Clicked");
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}








mouse.html
<applet code="Mouse" width=200 height=60>
</applet>


user defined exception

Example 1:


class clexc extends Exception
{
public clexc(String s)
{

super(s);
}
}

public class clex
{

public static void main(String args[])
{
try
{

throw new clexc("my exception");
}
catch (clexc ex)
{
System.out.println("exception Caught");

System.out.println(ex.getMessage());
}
}
}


output::

D:\>javac clex.java

D:\>java clex
exception Caught
my exception

D:\>




Example 2:

class clexc extends Exception
{    private int v;
public clexc(String s)
{

super(s);
}
     public clexc(int a){v=a;}
     public String toString(){ return "exc"+v+"pp";}


}

public class clex
{
static void compute(int a) throws clexc
        { System.out.println("compute function"+a);
          if(a>10)
             throw new clexc(a);
          System.out.println("no throw");
           
        }
public static void main(String args[])
{
try
{

compute(1);
}
catch (clexc ex)
{
System.out.println("exception Caught");

System.out.println(ex.getMessage());
}
}
}



D:\>javac clex.java

D:\>java clex
compute function1
no throw

D:\>


Example 3:

class clexc extends Exception
{    private int v;
public clexc(String s)
{

super(s);
}
     public clexc(int a){v=a;}
     public String toString(){ return "exc"+v+"pp";}


}

public class clex
{
static void compute(int a) throws clexc
        { System.out.println("compute function"+a);
          if(a>10)
             throw new clexc(a);
          System.out.println("no throw");
           
        }
public static void main(String args[])
{
try
{

compute(11);
}
catch (clexc ex)
{
System.out.println("exception Caught");

System.out.println(ex.getMessage());
}
}
}

output:


D:\>javac clex.java

D:\>java clex
compute function11
exception Caught
null

D:\>



Wednesday, April 24, 2019

sub package



step1:: go to D:\p1\p2

step2: p.java


package p1.p2;
import java.util.Scanner;
public class p
{
public void f(int x,int y){
System.out.println(x);
System.out.println(y);
}
}

step3:
D:\

step4 ::cl5.java

import p1.p2.p;
import java.util.Scanner;
class cl4 extends p
{
}


public class cl5
{
public static void main(String args[])
{
cl4 ob=new cl4();
ob.f(2,3);
}
}



step5:

D:\>cd p1

D:\p1>cd p2

D:\p1\p2>javac p.java

D:\p1\p2>cd ..

D:\p1>cd ..

D:\>javac cl5.java

D:\>java cl5
2
3

D:\>java cl5

package

1. single level package -                        text - click here


                                                               video - step by step video 



2.       two level package                            text - click here                                                                                                                                                                                        




Example 3 ::  two level                                                                                                                                                                                                                                      



4. difference between default and public                                                                                                                                                                                                             

                                                                          






hierarchical interface


                                       



interface A
{
public void input();
}


interface B extends A
{
void output();
}

interface c extends A
{
void output();
}




class cl1 implements B
{
int a,b;
public void input()
{
a=3;
b=7;
}
public void output()
{
int min;
min=a+b;
System.out.println(min);
}
}




class cl2 implements c
{
int a,b;
public void input()
{
a=4;
b=8;
}
public void output()
{
int min;
min=a-b;
System.out.println(min);
}
}
public class diya1
{
public static void main(String args[])
{

cl1 ob1=new cl1();
ob1.input();
ob1.output();

cl2 ob2=new cl2();
ob2.input();
ob2.output();
}
}


D:\>javac diya1.java

D:\>java diya1
10
-4

D:\>

                                                                                                                                                                            edited by diya

interface in java



1. 


                    click


2.



                    click



3.


                  click


4. 

     
                         click

multilevel interface

     





interface A
{
void input();
}
interface B extends  A
{
public void output();
}
class cl implements B
{
int a,b;
public void input()
{

a=2;
b=3;}
public void output()
{
int sum;
sum=a+b;
System.out.println(sum);
}
}
public class diya1
{
public static void main(String args[])
{
cl ob=new cl();
ob.input();
ob.output();
}}


D:\>javac diya1.java

D:\>java diya1
5

D:\>
                                                                                                                     edited by diya

multiple inheritence






interface A
{
void input();
}
interface B
{
public void output();
}
class cl implements A,B
{
int a,b;
public void input()
{

a=2;
b=3;}
public void output()
{
int sum;
sum=a+b;
System.out.println(sum);
}
}
public class diya1
{
public static void main(String args[])
{
cl ob=new cl();
ob.input();
ob.output();
}
}

D:\>javac diya1.java


D:\>java diya1
5

D:\>
                                                                                                                                   edited by diya

one level interface


interface A
{
void input();
void sum();
}

 class cl implements A
{
int a,b;
public void input()
{
a=2;
b=3;
}

public void sum()
{
int c;
c=a+b;
System.out.println(c);
}
}
public class p2
{
public static void main(String args[])
{
cl ob=new cl();
ob.input();
ob.sum();
}
}

Output::

D:\>javac p2.java

D:\>java p2
5

D:\>javac diya1.java
                                                                                                                                   edited by diya

Tuesday, April 23, 2019

basics of event handling



1.  label: 

 l=new Label();  
 l.setText("string");



2. textbox:

TextField tf;
String s=tf.getText();
tf3.setText(text);


3. text area:

area=new TextArea();
 String text=area.getText();

    textArea = new TextArea(2, 10);
    textArea.setSize(300, 100);

    textArea.setText("123456 789123");


4. checkbox:

Checkbox checkbox2 = new Checkbox("Java"true);  



5. checkbox group :
  
CheckboxGroup cbg = new CheckboxGroup(); 
Checkbox checkBox1 = new Checkbox("C++", cbg, false);                       Checkbox checkBox2 = new Checkbox("Java", cbg, false); 



6. list box:

Choice c=new Choice();  
        c.setBounds(100,100, 75,75);  
        c.add("Item 1");  
        c.add("Item 2");  
        c.add("Item 3");  
        c.add("Item 4");  

        c.add("Item 5"); 


print string using textbox and commandbutton (method 2)

import java.awt.*; 
import java.awt.event.*; 
class p1 extends Frame implements ActionListener{ 
TextField tf; 
p1(){ 
 
//create components 
tf=new TextField(); 
tf.setBounds(60,50,170,20); 
Button b=new Button("click me"); 
b.setBounds(100,120,80,30); 
 
//register listener 
b.addActionListener(this);//passing current instance 
 
//add components and set size, layout and visibility 
add(b);add(tf); 
setSize(300,300); 
setLayout(null); 
setVisible(true); 

public void actionPerformed(ActionEvent e){ 
tf.setText("Welcome"); 

public static void main(String args[]){ 
new p1(); 



output::





reference -- javapoint.com
  • Button
    • public void addActionListener(ActionListener a){}
  • MenuItem
    • public void addActionListener(ActionListener a){}
  • TextField
    • public void addActionListener(ActionListener a){}
    • public void addTextListener(TextListener a){}
  • TextArea
    • public void addTextListener(TextListener a){}
  • Checkbox
    • public void addItemListener(ItemListener a){}
  • Choice
    • public void addItemListener(ItemListener a){}
  • List
    • public void addActionListener(ActionListener a){}
    • public void addItemListener(ItemListener a){}





reference -- javapoint.com

print string using text box and command buttton

import java.awt.*; 
import java.awt.event.*; 
public class p1 { 
public static void main(String[] args)
 { 
    Frame f=new Frame("Button Example"); 
    final TextField tf=new TextField(); 
    tf.setBounds(50,50, 150,20); 
    Button b=new Button("Click Here"); 
    b.setBounds(50,100,60,30); 
 
 b.addActionListener(new ActionListener()
                                       { 
                                       public void actionPerformed(ActionEvent e)
                                        { 
                                           tf.setText("this is a text box");
                                        } 
                                      }
                                   ); 
    f.add(b);f.add(tf); 
    f.setSize(400,400); 
    f.setLayout(null); 
    f.setVisible(true); 




output:::



set bound() method in java

public void setBounds(int x, int y, int width, int height)

Friday, April 12, 2019

exception handling

1. divide by zero without exception  -link
2. divide by zero with exception - link
3. null pointer - link
4. number format exception - link
5. array index out of bound- link
6. own exception - link
7. nested try -link


threads

1. thread using thread class -link
2. Java Thread Example by implementing Runnable inter...  link
3. multithreading with run() -- link
4. multithreading with start() -- link
5. multithreading with join method -- link
6. thread setname(),getname(),getid() -- link
7. current thread name - link
8. thread priority - link
9. set priority of thread - link
10. daemon thread -  link
11. thread suspend()/resume() -- link
12. without synchronization -- link
13. synchronization - i) synchronized method -- link
                                  ii) synchronized block -- link