Search This Blog

Monday, February 15, 2010

Fun With ROBOT class...

java.awt.Robot is a class which can make the mouse move,trigger key press,trigger key release,take screen capture etc..

I would like to show a simple example with Robot class.....................



import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Main extends JFrame implements MouseListener{

/**
Mouse listener interface is implemented to get mouse events like mouse clicked,pressed,released,entered and exited events.
*/
Robot R;
JButton intel;
JButton notintel;
JLabel lab;
JPanel p;
Main()
{
try {
R = new Robot(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
/*
constructor of robot class takes a graphic device as argument.We supply the default screen device
*/
} catch (AWTException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
intel=new JButton("INTELLIGENT");
notintel=new JButton("NOT INTELLIGENT");
lab=new JLabel("SELECT THE OPTION WHICH SUIT YOU");
p=new JPanel(new FlowLayout());
//flow layout is applied to the panel....
this.add(p);
p.add(lab);
p.add(intel);
p.add(notintel);
intel.addMouseListener(this);
//mouse listener registered with buttons
notintel.addMouseListener(this);
this.setAlwaysOnTop(true);
this.setUndecorated(true);//to avoid close bar
this.setSize(300,75);
this.setVisible(true);//to display the JFrame
this.setLocation(500, 500);
}
public static void main(String[] args) {
// TODO code application logic here
new Main();
}

public void mouseClicked(MouseEvent e) {
JButton b=(JButton) e.getSource();
if(b.getText().equals("NOT INTELLIGENT")) {
System.exit(0);
}

}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

JButton b=(JButton) e.getSource();
if(b.getText().equals("INTELLIGENT")) {
R.mouseMove(400,400);//mouse is moved using object of robot class
}
}

public void mouseExited(MouseEvent e) {

}

}

Sunday, February 14, 2010

Some Experiments With JAVA For Beginners....




I am Using NETBEANS IDE 6.8 for developing java programs. I am not a professional Java programmer, but just a beginner. So i thought sharing some programs with others will be fun and also their responses will be a great benefit..

First I am going to develop an application which will float on your desktop.



For that Create a new project in the netbeans ide. And then add a new package named "resource" drag and drop the one image(in my case its bella.jpg) to this package...

Then code the Main.java ........
/*import necessary classes*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JWindow


public class Main extends JWindow { /*this JWindow is floating on the screen*/

Image ico; /*this holds the image that we want to draw on the screen*/
int width=200; int height=300; /*specify the height and width of the application*/
Dimension d;

Main()
{
ico=new ImageIcon(this.getClass().getResource("/resource/bella.jpg")).getImage();
/* The image is retrieved from the package using this.getClass().getResource(String filename)
so that the application become portable. */


this.setSize(width, height); /*height and width of the JWindow is set.*/
this.setVisible(true);
d= Toolkit.getDefaultToolkit().getScreenSize(); /*a dimension consist of a width & height
Toolkit.getDefaultToolkit().getScreenSize() gives the current screen size*/

d.height=d.height-height; /*to get bottom limit of the screen*/
d.width=d.width-width; /*to get right limit of the screen*/
}
public void paint(Graphics g) /*this paint method is invoked automatically*/
{
if(g!=null)
g.drawImage(ico, 0, 0, width, height,null);
/* Above code draws image ico at (0,0) and scale the image to the width and height specified*/
else
System.out.println("null");
}



public static void main(String[] args) throws InterruptedException {

new Main().flow(0,0,3,3);
/*Initial positon of the window when execution started and the increment factor of both x and
y. It can be chaned to experiment your self */
}


void flow(int x,int y,int incx,int incy) throws InterruptedException {
while(true)
{
x=x+incx; y=y+incy; /*new x and y position for window is calculated*/
this.setLocation(x, y); /*change the location of the window to the new location*/
Thread.sleep(30); /*put a small delay between two adjacent relocation of window
remember 'main' it self starts as a thread. */

if(x<0)>
{
incx=-incx;//reverse the increment value so that window goes to opposite x direction
}
else if(x>d.width)
{
incx=-incx;//reverse the increment value so that window goes to opposite x direction
}
if(y<0)
{
incy=-incy;//reverse the increment value so that window goes to opposite y direction
}
if(y>d.height)
{
incy=-incy;//reverse the increment value so that window goes to opposite y direction
}
}
}

}