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

No comments:

Post a Comment