12/22/2011

Java applet (mouse events)

Java applet (mouse events)
Java applet (mouse events)


/*Java applet (mouse events)*/

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

public class MouseActions extends Applet implements MouseListener,MouseMotionListener
{
int mouseX=0,mouseY=0; //Coordinates
String msg="";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.blue);
setForeground(Color.yellow);
}
public void mouseClicked(MouseEvent me)
{
//mouseX=30;
//mouseY=30; 
mouseX=me.getX();
mouseY=me.getY();
msg="Mouse is clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=30;
mouseY=30;
msg="Mouse is entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX=30;
mouseY=30;
msg="Mouse exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
//mouseX=me.getX(); //Mouse event
//mouseY=me.getY();
mouseX=30;
mouseY=30;
msg="Mouse is pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=me.getX(); //Mouse event
mouseY=me.getY();
msg="Mouse is Released";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX=me.getX(); //Mouse event
mouseY=me.getY();
msg="*";
showStatus("Mouse is dragged at X: " + mouseX + " , Y: " + mouseY);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("Mouse is moving at X: " + me.getX() + " , Y: " + me.getY());

}
public void paint(Graphics g)
{
Font f = new Font("Arial",Font.BOLD,25); 
g.setFont(f); 
g.drawString(msg,mouseX,mouseY);
} 

} 
//<applet code="MouseActions.class" height=500 width=500></applet>

Java applet (marquee text)



/* Java applet (marquee text) */

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

public class MovingBanner extends Applet implements Runnable
{

public String str=" Moving Banner ";
Thread t=null;
boolean stopflag;
public void init()
{

setBackground(Color.cyan);
setForeground(Color.blue);
}
public void start()
{
// str=getParameter("msg");
// if(str==null)
// str=" Message not found ";
// str=" " +str;
t= new Thread(this);
stopflag=false;
t.start();
}
public void stop()
{
stopflag=true;
t=null;
}
public void run()
{
char ch;
for(;;)
{
try{
repaint();
Thread.sleep(150);
ch=str.charAt(0);
str=str.substring(1,str.length());
str+=ch;
if(stopflag)
break; 
}//try
catch(InterruptedException e){}
}//for loop

}

public void paint(Graphics g)
{
Font f = new Font("Arial",Font.BOLD,25); 
g.setFont(f); 
g.drawString(str,50,30);
} 

} 
//<applet code="MovingBanner.class" height=500 width=500></applet>