Archive

Archive for the ‘Java’ Category

Java Program to write an applet with Mouse Event Handling using inner class which extends MouseAdapter – Q51

January 26, 2009 Leave a comment

Q51: Java Program to write an applet which does mouse event handling using concept of inner class which extends MouseAdapter.

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

public class Ap_MousePressedDemo extends Applet{
	public void init(){
		addMouseListener(new MyMouseAdapter(this));
	}
}

class MyMouseAdapter extends MouseAdapter{
	Ap_MousePressedDemo mousePressedDemo;
	public MyMouseAdapter(Ap_MousePressedDemo mousePressedDemo){
		this.mousePressedDemo = mousePressedDemo;
	}
	public void mousePressed(MouseEvent me){
		mousePressedDemo.showStatus("Mouse Pressed.");
	}
} 

… from College notes (BCA/MCA assignments):


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:

Java Program to write an applet with message on Status Bar – Q48

January 24, 2009 Leave a comment

Q48: Java Program to write an applet which displays its code base and document base and show a message on the status bar and display the received parameters which were passed to this applet.

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

public class Ap_Paths extends Applet{
	
	String msg;
	
	public void paint(Graphics g){
	
		URL url_cb = getCodeBase();
		msg = "CodeBase: " + url_cb.toString();
		g.drawString(msg, 20, 30);
		
		URL  url_db = getDocumentBase();
		msg = "Doc. Base: " + url_db.toString();
		g.drawString(msg, 20, 60);
		
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to display a moving banner on the applet window – Q47

January 24, 2009 Leave a comment

Q47: Java Program to display a moving banner on the applet window.

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

public class Ap_Banner extends Applet implements Runnable{
	
	String msg = "My name is Manoj Pandey";
	Thread th;
	int state;
	boolean stopflag;
	
	public void init(){
		setBackground(Color.blue);
		setForeground(Color.yellow);
	}
	
	public void start(){
		th = new Thread(this);
		stopflag = false;
		th.start();
	}
	
	public void run(){
		char ch;
		for(;;){
			try{
				repaint();
				Thread.sleep(500);
				ch = msg.charAt(0);
				msg = msg.substring(1, msg.length());
				msg += ch;
				if(stopflag) break;
			}
			catch(InterruptedException e){
			}
		}
	}
	
	public void sleep(){
		stopflag = true;
		th = null;
	}
	
	public void paint(Graphics g){
		g.drawString(msg, 50, 50);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags: