Archive

Archive for January 25, 2009

Java Program to write an applet with Mouse Event Handling – Q50

January 25, 2009 Leave a comment

Q50: Java Program to write an applet which does mouse event handling by extending MouseAdapter class.

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

public class Ap_AdapterDemo extends Applet{
	public void init(){
		addMouseListener(new MyMouseAdapter(this));
		addMouseMotionListener(new MyMouseMotionAdapter(this));
	}
}

class MyMouseAdapter extends MouseAdapter{
	Ap_AdapterDemo adapterDemo;
	public MyMouseAdapter(Ap_AdapterDemo adapterDemo){
		this.adapterDemo = adapterDemo;
	}
	public void mouseClicked(MouseEvent me){
		adapterDemo.showStatus("Mouse clicked");
	}
}

class MyMouseMotionAdapter extends MouseMotionAdapter{
	Ap_AdapterDemo adapterDemo;
	public MyMouseMotionAdapter(Ap_AdapterDemo adapterDemo){
		this.adapterDemo = adapterDemo;
	}
	public void mouseDragged(MouseEvent me){
		adapterDemo.showStatus("Mouse Draged");
	}
}

… from College notes (BCA/MCA assignments):


Java Program to write an Applet with Event Handling – Q49

January 25, 2009 Leave a comment

Q49: Java Program to write an applet which does the event handling of keys by implementing keyListener Interface.

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

public class Ap_SimpleKey extends Applet implements KeyListener {
	
	String msg = "";
	int x = 10,y = 20;
	
	public void init(){
		addKeyListener(this);
		requestFocus();
	}
	
	public void keyPressed(KeyEvent Ke){
		showStatus("Key Down");
		}
	
	public void keyReleased(KeyEvent Ke){
		showStatus("Key Up");
		}
	
	public void keyTyped(KeyEvent ke){
		msg += ke.getKeyChar();
		repaint();
	}
	
	public void paint(Graphics g){
		g.drawString(msg, x, y);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags: