Search This Blog

Thursday, March 18, 2010

A simple Game!

In this game you have to click on a button with your mouse........But its not that simple as you think..Try..this!!!

Source Code
=========
Set your Project as Shown in the above picture...
on.jpg and off.jpg are given below...........

on.jpg


off.jpg



And code:


package canvasclass;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JWindow;


/**
 *
 * @author sreenath
 */

class button extends JPanel implements MouseListener
{
    boolean mousepos;
    Image img;
    int x,y,width,height;
    button(int locx,int locy,int wid,int heig)
    {
        x=locx;
        y=locy;
        width=wid;
        height=heig;
        mousepos=false;
        this.setSize(width, height);
        this.setLocation(x, y);
        this.setVisible(true);
        this.addMouseListener(this);
    }
    public void paint(Graphics g)
    {
        g.clearRect(x, y, width, height);
        if(mousepos)
        {
           img =new ImageIcon(this.getClass().getResource("off.jpg")).getImage();
        }
        else
        {
           img =new ImageIcon(this.getClass().getResource("on.jpg")).getImage();
        }
        g.drawImage(img,0,0,width,height, null);
    }

    public void mouseClicked(MouseEvent e) {
        System.exit(0);
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {
      
        mousepos=true;
        this.repaint();
    }

    public void mouseExited(MouseEvent e) {
        mousepos=false;
        this.repaint();
    }
}
public class Main extends JWindow {
int[] pixels = new int[16 * 16];//Inorder to create invisible cursor
button spot;

    Main() throws AWTException
    {
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
        this.setAlwaysOnTop(true);
         this.setVisible(true);
        Robot r=new Robot();
        r.mouseMove(0, 0); //move cursor to top left position of screen
        spot=new button(750,500,75,75);
        this.add(spot);
      
      
        java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().createImage(new java.awt.image.MemoryImageSource(16, 16, pixels, 0, 16));
        Cursor mycursor =Toolkit.getDefaultToolkit().createCustomCursor(image,new Point(0,0), "mycursor");     //transparent cursor
        this.setCursor(mycursor);
      
      
    }

    public static void main(String[] args) throws IOException, AWTException {
        // TODO code application logic here
       new Main();
    }





}