...::: Recent Updates :::...

Sunday, September 18, 2011

Java Program Which Show a Rectangle That Will Change Color When The Mouse Moves Over It.

------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
JAVA APPLET PROGRAM
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
2. Write a java program to create applet which will show a rectangle that will change color when the mouse moves over it.
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------ 
NAME : SAHIN NISAR RAJ
ENROLL : 105200693009
COLLEGE : LAXMI INSTITUTE OF COMPUTER APPLICATIONS
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------ 
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
 
public class S9Q2 extends Applet implements ActionListener,KeyListener,MouseMotionListener
{
   
    int rectx;
    int recty;
    int rectwidth;
    int rectheight;
    int mx,my,mw,mh;
     @Override
    public void init()
    {
        rectx = 20;
        recty = 20;
        rectwidth = 200;
        rectheight = 100;
        addMouseMotionListener(this);
    }
    public void paint(Graphics g)
    {
       if((mx>rectx && my>recty) && (mx<rectx+rectwidth && my<recty+rectheight))
       {
            g.setColor(Color.MAGENTA);
            g.fillRect(rectx,recty,rectwidth,rectheight);
       }
       else
       {
            g.setColor(Color.red);
            g.fillRect(rectx,recty,rectwidth,rectheight);
       }
    }
    public void mouseMoved(MouseEvent e)
    {
            mx = e.getX();
            my = e.getY();   
            repaint();
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------
Download Source : Click Here...
------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment