Mouse click example
Q. Write a Java program to
define an applet , which will contain a label and when we click on the applet ,
the message will appear on the label.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet
code="MouseDemo1" height=200 width=200>
</applet>
*/
public class MouseDemo1 extends
Applet implements MouseListener
{
Label
lbl;
public
void init( )
{
lbl=new
Label(" ");
add(lbl);
addMouseListener(this);
}
public
void mousePressed(MouseEvent me)
{
int val = me.getModifiers();
if(val==16)
lbl.setText("You
clicked the Left Mouse Button.");
else
lbl.setText("You
clicked the Right Mouse Button.");
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
}
The methods getModifiers( ) is
used to check whether we click on the left mouse button or not .
The
general form is ,
int
getModifiers( )
If we click on the left mouse
button , it will return the value 16 and
if we clicked on the right mouse button it will return the value 4.
Using images in the Applet
Consider the following code ,
import java.awt.*;
import java.applet.*;
/*
<applet
code="ImageDemo" height=200 width=200>
</applet>
*/
public class ImageDemo extends
Applet
{
Image
imap;
public
void init()
{
imap=getImage(getCodeBase(),"BOYHOME.GIF");
}
public
void paint(Graphics g)
{
g.drawImage(imap,10,10,160,120,this);
}
Using images in the Applet
Comments
Post a Comment