Tuesday, 22 December 2015

PatternDemo in Java - I

import java.util.*;
class PatternDemo
{
    public static void main(String args[])
    {
        char ch='A';
     
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter value for n: ");
        int n = sc.nextInt();
     
        for(int i=1;i<=n;i++)
        {
           for(int j=n;j>=i;j--)
           {
              System.out.print(" ");
           }
             
           for(int k=1;k<=i;k++)
           {
               System.out.print(ch++);
           }
           ch--;
         
           for(int m=1;m<i;m++)
           {
              System.out.print(--ch);
           }
           System.out.print("\n");
        }
    }
}
Output2:
Enter value for n: 5
           A
        ABA
      ABCBA
    ABCDCBA
 ABCDEDCBA



import java.util.*;
class PatternDemo
{
    public static void main(String args[])
    {
        char ch='A';

        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter value for n: ");
        int n = sc.nextInt();
     
        for(int i=0;i<=(n-1);i++)
        {
            for(int j=0;j<=i;j++)
            {
               System.out.print(ch++);
            }
            System.out.print("\n");
        }
    }
}

Output1:
Enter value for n: 4
A
BC
DEF
GHIJ

Tuesday, 13 October 2015

Program For Array Of Object


import java.util.*;
class Student
{
    String name;
    int id,p,c,m,total;
 
    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter name: ");
        name = sc.next();

        System.out.print("Enter ID: ");
        id = sc.nextInt();

        System.out.print("Enter Physics marks: ");
        p = sc.nextInt();

        System.out.print("Enter Chemistry marks: ");
        c = sc.nextInt();

        System.out.print("Enter Maths marks: ");
        m = sc.nextInt();

        total = p+c+m;
    }

    void display()
    {
        System.out.print("\nName:"+name+"   ID:"+id+"   Physics:"+p+"  Chemistry:"+c+"   Maths:"+m+"   Total:"+total);      
    }
 
    static void sort_marks(Student s[])
    {
        int n = s.length;
        Student temp = new Student();

        for(int i=1; i<n; i++)
        {
            for(int j=0; j<(n-i); j++)
            {
                if(s[j].total<s[j+1].total)
                {
                    temp =s[j];
                    s[j] =s[j+1];
                    s[j+1]=temp;
                }
            }
        }
    }
 
    static void sort_name(Student s[])
    {
        int n = s.length;
        Student temp = new Student();

        for(int i=1; i<n; i++)
        {
            for(int j=0; j<(n-i); j++)
            {
                if(s[j].name.compareTo(s[j+1].name)>0)
                {
                    temp =s[j];
                    s[j] =s[j+1];
                    s[j+1]=temp;
                }
            }
        }
    }
}

class ArrayObjectDemo
{
    public static void main(String soham[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter number of students: ");
        int n = sc.nextInt();

        Student s[]=new Student[n];
       
        for(int i=0; i<=(n-1); i++)
        {
System.out.print("\nEnter Details for Student "+(i+1));
            s[i] = new Student();
            s[i].accept();
        }
     
        //Sorted according to total marks
        Student.sort_marks(s);
        System.out.print("\nSorted according to total marks: ");
        for(int i=0; i<=(n-1); i++)
        {
            s[i].display();
        }
        System.out.println();

        //Sorted according to name
        Student.sort_name(s);
        System.out.print("\nSorted according to name: ");
        for(int i=0; i<=(n-1); i++)
        {
            s[i].display();
        }
        System.out.println();
    }
}

Output: 
Enter number of students: 3

Enter Details for Student 1
Enter name: ABC
Enter ID: 101
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 80

Enter Details for Student 2
Enter name: XYZ
Enter ID: 102
Enter Physics marks: 80
Enter Chemistry marks: 70
Enter Maths marks: 80

Enter Details for Student 3
Enter name: PQR
Enter ID: 103
Enter Physics marks: 90
Enter Chemistry marks: 70
Enter Maths marks: 60

Sorted according to total marks:
Name:ABC   ID:101   Physics:90   Chemistry:70   Maths:80   Total:240
Name:XYZ   ID:102   Physics:80   Chemistry:70   Maths:80   Total:230
Name:PQR   ID:103   Physics:90   Chemistry:70   Maths:60   Total:220

Sorted according to name:
Name:ABC   ID:101   Physics:90   Chemistry:70   Maths:80   Total:240
Name:PQR   ID:103   Physics:90   Chemistry:70   Maths:60   Total:220
Name:XYZ   ID:102   Physics:80   Chemistry:70   Maths:80   Total:230

Monday, 12 October 2015

Program For Tribonacci Series


import java.util.*;
import java.io.*;
class Tribonacci
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number till u want Tribonacci series: ");
        int n=sc.nextInt();

        int a = 0,b = 0,c = 1;
        int d = a+b+c;

        System.out.println("\nTribonacci Series: ");
        System.out.print(a+"\t"+b+"\t"+c);

        for(int i=4;i<=n;i++)
        {
            System.out.print("\t"+d);
            a=b;
            b=c;
            c=d;
            d=a+b+c;
        }

        System.out.println();
    }
}
Output:
Enter number till u want Tribonacci series: 10

Tribonacci Series:
0       0       1       1       2       4       7       13      24      44

Wednesday, 23 September 2015

Program For Pythagorean Triplet


/* 
    Pythagorean Triplet:  
        c*c = a*a + b*b 
*/  
  
public class PythagoreanTriplet  
{  
    public static void main(String s[]) throws Exception  
    {  
        System.out.println("Pythagorean Triplet: ");  
        for(int a=1; a<=50 ; a++)  
        {  
            for(int b=1; b<=50 ; b++)  
            {  
                int csquared = a*a + b*b;  
                double croot = Math.sqrt(csquared);  
  
                if( croot==Math.ceil(croot) )  
                {  
                    System.out.println(a + " " + b + " " + (int)croot);  
                }  
            }  
        }  
    }  
}  
Output:
Pythagorean Triplet:
3 4 5
4 3 5
6 8 10
8 6 10

Monday, 8 June 2015

Program to implement Binary search

//Program to implement Binary Search
import java.io.*;
public class Binary
{
  public static void main(String args[]) throws Exception
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n, item, i, j, mid, top, bottom;
    int a[] = new int[100];

    System.out.print("Enter number of elements: ");
    n = Integer.parseInt(br.readLine());

    System.out.println("Enter "+n+" elements in ascending order: ");
    for (i = 0; i <= (n-1); i++) 
    {
      a[i]=Integer.parseInt(br.readLine());
    }

    System.out.print("\nEnter the element to search: ");
    item=Integer.parseInt(br.readLine());

    bottom = 0;
    top = n; 
    do 
    {
      mid = (bottom + top) / 2;
      if (item < a[mid])
      {
        top = mid - 1;
      }
      else if (item > a[mid])
      {
        bottom = mid + 1;
      }
    }while(item != a[mid] && bottom <= top);
     
    if (item == a[mid]) 
    {
      System.out.print("\n"+item+" is found at position: "+(mid+1));
    } 
    else 
    {
      System.out.print("\n"+item+" is not found");
    }
    System.out.println();
  }
}
//Program to implement Binary search
import java.io.*;  
import java.util.*;
public class Binary  
{  
  public static void main(String args[]) throws Exception  
  {  
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        
    int n, item, i;    
        
    System.out.print("Enter number of elements: ");  
    n = Integer.parseInt(br.readLine());  

    int a[] = new int[n];
        
    System.out.println("Enter "+n+" elements: ");  
    for (i = 0; i <= (n-1); i++)   
    {  
      a[i]=Integer.parseInt(br.readLine());  
    }  
       
    Arrays.sort(a);
    System.out.println("\nThe sorted list:");
    for (i=0; i<=(a.length - 1); i++) 
    {
      System.out.println(a[i]+"\t");
    }
        
    System.out.print("\nEnter the element to search: ");  
    item=Integer.parseInt(br.readLine());  

    int retVal = Arrays.binarySearch(a,item);   
    System.out.println("The index of element "+item+": "+(retVal+1));

    System.out.println();  
  }  
}    
Output:
Enter number of elements: 5
Enter 5 elements:
3
5
9
1
2

The sorted list:
1
2
3
5
9

Enter the element to search: 5
The index of element 5: 4

Arithmetic Operator Demo in Java

public class arithmetic-op
{
  public static void main(String args[])
  {
    i = 5;
    System.out.print("The Value of i: "+ i);
    
    i = i + 5;
    System.out.print("\nThe Value of i: "+ i);
    
    i = i - 2;
    System.out.print("\nThe Value of i: "+ i);
    
    i = i * 2;
    System.out.print("\nThe Value of i: "+ i);
    
    i = i / 4;
    System.out.print("\nThe Value of i: "+ i);
    
    i++;
    System.out.print("\nThe Value of i: "+ i);
    
    i++;
    System.out.print("\nThe Value of i: "+ i);
    
    i --;
    System.out.print("\nThe Value of i: "+ i);
    System.out.println();
  }
}
Output:
The Value of i: 5
The Value of i: 10
The Value of i: 8
The Value of i: 16
The Value of i: 4
The Value of i: 5
The Value of i: 6
The Value of i: 5

Sunday, 7 June 2015

Program to check whether two strings are anagrams of each other

import java.io.*;
public class Anagrams
{
  public static void main(String args[]) throws Exception
  {
    Anagrams a = new Anagrams();
    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Enter first string: ");
    String str1 = br.readLine();
     
    System.out.print("Enter second string: ");
    String str2 = br.readLine();
    
    int flag = a.Anagram(str1, str2);

    if (flag==0)
    {
      System.out.print("\nResult: "+str1+" and "+str2+" are anagrams.");
    }
    else
    {
      System.out.print("\nResult: "+str1+" and "+str2+" are not anagrams.");
    }
    
    System.out.println();
  }


  public int Anagram(String str1,String str2)
  {
    if (str1.equals(str2))
    {
      return 0;
    }

    return 1;
  }
}
Output:
Enter first string: ana
Enter second string: ana

Result: ana and ana are anagrams.

Output2:
Enter first string: aana
Enter second string: ana

Result: aana and ana are not anagrams.

Program to Encrypt and Decrypt a Password

import java.io.*;
public class encrypt_decrypt
{
  public static void main(String args[]) throws Exception
  {
    encrypt_decrypt ed = new encrypt_decrypt();

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
    System.out.print("\nEnter the password: ");
    String password = br.readLine();

    //encryption process...
    StringBuilder str = new StringBuilder(password);
    password = ed.encrypt(str,0xFACA);
    System.out.print("\nEncrypted Password: " + password);

    //decryption process...
    str = new StringBuilder(password);
    password = ed.decrypt(str,0xFACA);
    System.out.print("\nDecrypted Password: " + password);

    System.out.println();
  }

  public String encrypt(StringBuilder str,int key)
  {
    for(int i=0; i<=(str.length() - 1); i++)
    {
      char c = (char)(str.charAt(i) - key);
      str.setCharAt(i,c);
    }
    return new String(str);
  }

  public String decrypt(StringBuilder str,int key)
  {
    for(int i=0; i<=(str.length() - 1); i++)
    {
      char c = (char)(str.charAt(i) + key);
      str.setCharAt(i,c);
    }
    return new String(str);
  }
}
Output:
Enter the password: admin

Encrypted Password: ?????
Decrypted Password: admin

Saturday, 6 June 2015

Program to generate prime numbers in a given range

import java.io.*;
public class Prime_no
{
  public static void main(String args[]) throws Exception
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int M, N, i, j, flag, temp, count = 0;
   
    System.out.print("\nEnter the value of M and N:\n");
    M = Integer.parseInt(br.readLine());
    N = Integer.parseInt(br.readLine());
   
    if(N < 2)
    {
      System.out.print("\nThere are no primes upto\n"+N);
    }

    System.out.print("\nPrime numbers are:");
    temp = M;
   
    if ( M % 2 == 0)
    {
      M++;
    }
    
    for (i=M; i<=N; i=i+2)
    {
      flag = 0;
   
      for (j=2; j<=i/2; j++)
      {
        if( (i%j) == 0)
        {
          flag = 1;
          break;
        }
      }
     
      if(flag == 0)
      {
          System.out.print("\n"+i);
          count++;
      }
    }
   System.out.println();
  }
}
Output
Enter the value of M and N:
1
10

Prime numbers are:
1
3
5
7

Conversion of numbers into words in Java

import java.io.*;  
public class num_to_words  
{  
  public static void main(String soham[]) throws Exception  
  {  
    long num,div,num1;  
    int flag=0,digit,position,total_digit;  
  
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  
    System.out.print("\nEnter number: ");  
    num = Integer.parseInt(br.readLine());  
  
    if(num==0)  
    {  
      System.out.print("Zero\n\n");  
    }  
  
    if(num>99999)  
    {  
      System.out.print("please enter number between 0 and 100000\n\n");  
    }  
  
    total_digit = 0;  
    div = 1;  
    num1 = num;  
    while ( num1 > 9 )  
    {  
      num1 = num1 / 10;  
      div = div * 10;  
      total_digit++;  
    }  
      
    total_digit++;  
    position = total_digit;  
    while ( num != 0 )  
    {  
      digit= (int)(num / div);  
      num = num % div;  
      div = div / 10;
        
      switch(position)  
      {  
        case 2:  
        case 5: 
            if ( digit == 1 )
            {
              flag = 1;  
            }
            else  
            {  
              flag = 0;  
              switch(digit)  
              {  
                case 2: System.out.print("twenty ");break;  
                case 3: System.out.print("thirty ");break;  
                case 4: System.out.print("forty ");break;  
                case 5: System.out.print("fifty ");break;  
                case 6: System.out.print("sixty ");break;  
                case 7: System.out.print("seventy ");break;  
                case 8: System.out.print("eighty ");break;  
                case 9: System.out.print("ninty ");  
              }  
            }  
            break;

        case 1:  
        case 4:    
            if ( flag == 1 )  
            {  
              flag = 0;  
              switch(digit)  
              {  
                case 0 : System.out.print("ten ");break;  
                case 1 : System.out.print("eleven ");break;  
                case 2 : System.out.print("twelve ");break;  
                case 3 : System.out.print("thirteen ");break;  
                case 4 : System.out.print("fourteen ");break;  
                case 5 : System.out.print("fifteen ");break;  
                case 6 : System.out.print("sixteen ");break;  
                case 7 : System.out.print("seventeen ");break;  
                case 8 : System.out.print("eighteen ");break;  
                case 9 : System.out.print("ninteen ");  
              }  
            }  
            else  
            {  
              switch(digit)  
              {  
                case 1 : System.out.print("one ");break;  
                case 2 : System.out.print("two ");break;  
                case 3 : System.out.print("three ");break;  
                case 4 : System.out.print("four ");break;  
                case 5 : System.out.print("five ");break;  
                case 6 : System.out.print("six ");break;  
                case 7 : System.out.print("seven ");break;  
                case 8 : System.out.print("eight ");break;  
                case 9 : System.out.print("nine ");  
              }  
            }  
            if ( position == 4 )  
            System.out.print("thousand ");  
            break;

        case 3:  
            if ( digit> 0 )  
            {  
              switch(digit)  
              {  
                case 1 : System.out.print("one ");break;  
                case 2 : System.out.print("two ");break;  
                case 3 : System.out.print("three ");break;  
                case 4 : System.out.print("four ");break;  
                case 5 : System.out.print("five ");break;  
                case 6 : System.out.print("six ");break;  
                case 7 : System.out.print("seven ");break;  
                case 8 : System.out.print("eight ");break;  
                case 9 : System.out.print("nine ");  
              }  
              System.out.print("hundred ");  
            }  
            break;  
      }  
      position--;  
    }  
  
    if ( position == 4 && flag == 0)  
    {  
      System.out.print("thousand");  
    }   
    else  
    {  
      if ( position == 4 && flag == 1)  
      {  
        System.out.print("ten thousand");  
      }  
    }  
      
    if ( position == 1 && flag == 1 )  
    {  
      System.out.print("ten ");  
    }  
      
    System.out.println();  
  }  
}  
Output1:
Enter number: 101
one hundred one

Output2:
Enter number: 1001
one thousand one

Pyramid Design in Java

    1
   232
  34543
 4567654
567898765
public class PyramidDesign
{
  public static void main(String soham[])
  {
    String str = "123456789";
    
    PyramidDesign pd = new PyramidDesign();

    for(int i=0; i<=4; i++)
    {
      pd.display(i,str);
      System.out.println();
    }
  }
  
  public void display(int start,String str)
  {
    int i,j;
    for(i=start; i<=(start+start); i++)
    {
      System.out.print(""+str.charAt(i));
    }
    
    for(j=(i-2); j >= start; j--)
    {
      System.out.print(""+str.charAt(j));
    }
  }
}

Saturday, 25 April 2015

Adapter class Demo in Java

//save as AdapterDemo.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AdapterDemo extends Applet
{
    int x,y;
    public void init()
    {
        addMouseListener(new MyMouseAdapter(this));
    }

    class MyMouseAdapter extends MouseAdapter
    {
        AdapterDemo obj;

        MyMouseAdapter(AdapterDemo obj)
        {
            this.obj = obj;
        }
 
        public void mouseClicked(MouseEvent me)
        {
           obj.showStatus("Mouse Clicked...");
           x = me.getX();
           y = me.getY();
           repaint();
        }
    }

    public void paint(Graphics g)
    {
        g.drawString("Mouse Clicked at: " + x + "," + y,10,10);
    }
}

/*


*/
Output

ColorSelector in Java

//save as ChangeColor.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ChangeColor extends JFrame implements AdjustmentListener
{
    JScrollBar red;
    JScrollBar green;
    JScrollBar blue;
    int cr = 0;
    int cg = 0;
    int cb = 0;

    ChangeColor()
    {
        super("SCROLLBAR DEMO");
    
        setLayout(new BorderLayout());
        setVisible(true);
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        red = new JScrollBar(JScrollBar.VERTICAL,0,10,0,255);
        red.setBackground(Color.red);

        green = new JScrollBar(JScrollBar.HORIZONTAL,0,10,0,255);
        green.setBackground(Color.green);

        blue = new JScrollBar(JScrollBar.VERTICAL,0,10,0,255);
        blue.setBackground(Color.blue);

        add(red,BorderLayout.WEST);
        add(green,BorderLayout.NORTH);
        add(blue,BorderLayout.EAST);

        red.addAdjustmentListener(this);
        green.addAdjustmentListener(this);
        blue.addAdjustmentListener(this);
    }

    public void adjustmentValueChanged(AdjustmentEvent ae)
    {
        if(ae.getSource()==red)
        {
            cr=ae.getValue();
        }
        else if(ae.getSource()==green)
        {
            cg=ae.getValue();
        }
        else if(ae.getSource()==blue)
        {
            cb=ae.getValue();
        }
        System.out.println(cr + ":" + cg + ":" + cb);

        // add color to content pane
        this.getContentPane().setBackground(new Color(cr,cg,cb));
    }

    public static void main(String soham[])
    {
        ChangeColor obj = new ChangeColor();
    }
}
Output

Friday, 6 February 2015

JTabbedPaneDemo in Java

import java.awt.*;
import javax.swing.*;
public class JTabbedPaneDemo extends JApplet
{
    public void init()
    {
        Container c = getContentPane();
        c.setLayout(new FlowLayout());

        JTabbedPane jtp = new JTabbedPane();
        jtp.addTab("Cities",new panel1());
        jtp.addTab("Color",new panel2());
        jtp.addTab("Taste",new panel3());

        c.add(jtp);
    }
}

class panel1 extends JPanel
{
    public panel1()
    {
        JButton b1 = new JButton("Mumbai");
        JButton b2 = new JButton("Banglore");
        JButton b3 = new JButton("Pune");
        add(b1);
        add(b2);
        add(b3);
    }
}

class panel2 extends JPanel
{
    public panel2()
    {
        JCheckBox c1 = new JCheckBox("Red");
        JCheckBox c2 = new JCheckBox("Green");
        JCheckBox c3 = new JCheckBox("Blue");
        add(c1);
        add(c2);
        add(c3);
    }
}

class panel3 extends JPanel
{
    public panel3()
    {
        JComboBox jcb = new JComboBox();
        jcb.addItem("Vanilla");
        jcb.addItem("Strawberry");
        jcb.addItem("Chocolate");
        add(jcb);
    }
}

/*


*/
Output

Sunday, 4 January 2015

GridBagLayout in Java


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class GridBaglayoutDemo extends Applet
{
    protected void makeButton(String name,GridBagLayout gridbag,GridBagConstraints gbc)
    {
        Button b = new Button(name);
        gridbag.setConstraints(b,gbc);
        b.setBackground(Color.lightGray);
        add(b);
    }

    public void init()
    {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        setFont(new Font("Elephanta",Font.PLAIN,20));
        setLayout(gridbag);

        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;       //first column
        gbc.gridy = 0;       //first row
        makeButton("Button1",gridbag,gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        makeButton("Button2",gridbag,gbc);

        gbc.gridx = 2;
        gbc.gridy = 0;
        makeButton("Button3",gridbag,gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.ipady = 40;         //make this component tall
        gbc.gridwidth = 3;     //3 column wide
        gbc.gridx = 0;       
        gbc.gridy = 1;      //second row
        makeButton("Button4", gridbag,gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.ipady = 0;       //reset to default
        gbc.anchor = GridBagConstraints.PAGE_END; //bottom of space
        gbc.insets = new Insets(20,0,0,0);   //top padding
        gbc.gridx = 1;       //aligned with button 2
        gbc.gridwidth = 2;      //2 columns wide
        gbc.gridy = 2;       //third row
        makeButton("Button5",gridbag,gbc);
    }

    public static void main(String s[])
    {
        Frame f = new Frame("GridBagLayout Demo");
        GridBaglayoutDemo gbl = new GridBaglayoutDemo();

        gbl.init();
        f.add("Center",gbl);
        f.pack();
        f.setSize(f.getPreferredSize());
        f.show();
 
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    }
}
Output

Thursday, 1 January 2015

MouseListener in Java


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseListenerDemo extends Applet implements MouseListener,MouseMotionListener
{
    String s = "";
    int x = 0,y = 0;

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

    //MouseListener method
    public void mouseClicked(MouseEvent me)
    {
        x = me.getX();
        y = me.getY();
        showStatus("Mouse Clicked.");
        repaint();
    }

    //MouseListener method
    public void mousePressed(MouseEvent me)
    {
        showStatus("Mouse Pressed.");
        repaint();
    }

     //MouseListener method
     public void mouseReleased(MouseEvent me)
     {
        showStatus("Mouse Released.");
        repaint();
     }

    //MouseListener method
    public void mouseEntered(MouseEvent me)
    {
        showStatus("Mouse Entered.");
        repaint();
    } 

    //MouseListener method
    public void mouseExited(MouseEvent me)
    {
        showStatus("Mouse Exited.");
        repaint();
    }

    //MouseMotionListener method
    public void mouseMoved(MouseEvent me)
    {
        x = me.getX();
        y = me.getY();
        showStatus("Mouse Moved.");
        repaint();
    }

    //MouseMotionListener method
    public void mouseDragged(MouseEvent me)
    {
        x = me.getX();
        y = me.getY();
        showStatus("Mouse Dragged.");
        repaint();
    }

    public void paint(Graphics g)
    {
        s = "Moving mouse at " + x + "," + y;
        g.drawString(s,10,10);
    }
}

/*


*/
Output