Friday, October 31, 2008
In this example you'll see how we can convert a BigInteger number into another radix such as binary, octal and hexadecimal.
import java.math.BigInteger;
public class BigIntegerConversion {
public static void main(String[] args) {
BigInteger number = new BigInteger("2008");
System.out.println("Number = " + number);
System.out.println("Binary = " + number.toString(2));
System.out.println("Octal = " + number.toString(8));
System.out.println("Hexadecimal = " + number.toString(16));
number = new BigInteger("FF", 16);
System.out.println("Number = " + number);
System.out.println("Number = " + number.toString(16));
}
}
The result of our examples:
Number = 2008
Binary = 11111011000
Octal = 3730
Hexadecimal = 7d8
Number = 255
Number = ff
import java.math.BigInteger;
public class BigIntegerConversion {
public static void main(String[] args) {
BigInteger number = new BigInteger("2008");
System.out.println("Number = " + number);
System.out.println("Binary = " + number.toString(2));
System.out.println("Octal = " + number.toString(8));
System.out.println("Hexadecimal = " + number.toString(16));
number = new BigInteger("FF", 16);
System.out.println("Number = " + number);
System.out.println("Number = " + number.toString(16));
}
}
The result of our examples:
Number = 2008
Binary = 11111011000
Octal = 3730
Hexadecimal = 7d8
Number = 255
Number = ff
Wednesday, October 29, 2008
import java.math.BigDecimal;
public class BigDecimalOperation {
public static void main(String[] args) {
BigDecimal decimalA = new BigDecimal("98765432123456789");
BigDecimal decimalB = new BigDecimal("10");
decimalA = decimalA.add(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.multiply(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.subtract(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.divide(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.pow(2);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.negate();
System.out.println("decimalA = " + decimalA);
}
}
Our class result are:
decimalA = 98765432123456799
decimalA = 987654321234567990
decimalA = 987654321234567980
decimalA = 98765432123456798
decimalA = 9754610582533151990855052972412804
decimalA = -9754610582533151990855052972412804
public class BigDecimalOperation {
public static void main(String[] args) {
BigDecimal decimalA = new BigDecimal("98765432123456789");
BigDecimal decimalB = new BigDecimal("10");
decimalA = decimalA.add(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.multiply(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.subtract(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.divide(decimalB);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.pow(2);
System.out.println("decimalA = " + decimalA);
decimalA = decimalA.negate();
System.out.println("decimalA = " + decimalA);
}
}
Our class result are:
decimalA = 98765432123456799
decimalA = 987654321234567990
decimalA = 987654321234567980
decimalA = 98765432123456798
decimalA = 9754610582533151990855052972412804
decimalA = -9754610582533151990855052972412804
Monday, October 27, 2008
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class BeanToXML {
private Long id;
private String itemName;
private String itemColour;
private Integer itemQuantities;
public static void main(String[] args) {
BeanToXML bean = new BeanToXML();
bean.setId(new Long(1));
bean.setItemName("T-Shirt");
bean.setItemColour("Dark Red");
bean.setItemQuantities(new Integer(100));
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream("Bean.xml")));
//
// Write an XML representation of the specified object to the output.
//
encoder.writeObject(bean);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemColour() {
return itemColour;
}
public void setItemColour(String itemColour) {
this.itemColour = itemColour;
}
public Integer getItemQuantities() {
return itemQuantities;
}
public void setItemQuantities(Integer itemQuantities) {
this.itemQuantities = itemQuantities;
}
}
The XML persistence will be like:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class BeanToXML {
private Long id;
private String itemName;
private String itemColour;
private Integer itemQuantities;
public static void main(String[] args) {
BeanToXML bean = new BeanToXML();
bean.setId(new Long(1));
bean.setItemName("T-Shirt");
bean.setItemColour("Dark Red");
bean.setItemQuantities(new Integer(100));
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream("Bean.xml")));
//
// Write an XML representation of the specified object to the output.
//
encoder.writeObject(bean);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemColour() {
return itemColour;
}
public void setItemColour(String itemColour) {
this.itemColour = itemColour;
}
public Integer getItemQuantities() {
return itemQuantities;
}
public void setItemQuantities(Integer itemQuantities) {
this.itemQuantities = itemQuantities;
}
}
The XML persistence will be like:
Saturday, October 25, 2008
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class XmlToBean {
public static void main(String[] args) {
try {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream("Bean.xml")));
//
// Reads the next object from the underlying input stream.
//
BeanToXML bean = (BeanToXML) decoder.readObject();
decoder.close();
System.out.println("ID = " + bean.getId());
System.out.println("Item Name = " + bean.getItemName());
System.out.println("Item Colour = " + bean.getItemColour());
System.out.println("Item Quantities = " + bean.getItemQuantities());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
The result are:
ID = 1
Item Name = T-Shirt
Item Colour = Dark Red
Item Quantities = 100
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class XmlToBean {
public static void main(String[] args) {
try {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream("Bean.xml")));
//
// Reads the next object from the underlying input stream.
//
BeanToXML bean = (BeanToXML) decoder.readObject();
decoder.close();
System.out.println("ID = " + bean.getId());
System.out.println("Item Name = " + bean.getItemName());
System.out.println("Item Colour = " + bean.getItemColour());
System.out.println("Item Quantities = " + bean.getItemQuantities());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
The result are:
ID = 1
Item Name = T-Shirt
Item Colour = Dark Red
Item Quantities = 100
Thursday, October 23, 2008
import java.beans.*;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class BeanToXmlTransient {
private Long id;
private String itemName;
private String itemColour;
private Integer itemQuantities;
static {
try {
//
// In this block will change the attribute type of the itemQuantities
// to transient so it will be not serialized to xml when we use
// XMLEncode to convert the bean to xml persistence.
//
BeanInfo bi = Introspector.getBeanInfo(BeanToXmlTransient.class);
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
for (int i = 0; i < pds.length; i++) {
PropertyDescriptor propertyDescriptor = pds[i];
if (propertyDescriptor.getName().equals("itemQuantities")) {
propertyDescriptor.setValue("transient", Boolean.TRUE);
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
BeanToXmlTransient bean = new BeanToXmlTransient();
bean.setId(new Long(1));
bean.setItemName("T-Shirt");
bean.setItemColour("Dark Red");
bean.setItemQuantities(new Integer(100));
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream("BeanTransient.xml")));
//
// Write an XML representation of the specified object to the output.
//
encoder.writeObject(bean);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemColour() {
return itemColour;
}
public void setItemColour(String itemColour) {
this.itemColour = itemColour;
}
public Integer getItemQuantities() {
return itemQuantities;
}
public void setItemQuantities(Integer itemQuantities) {
this.itemQuantities = itemQuantities;
}
}
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class BeanToXmlTransient {
private Long id;
private String itemName;
private String itemColour;
private Integer itemQuantities;
static {
try {
//
// In this block will change the attribute type of the itemQuantities
// to transient so it will be not serialized to xml when we use
// XMLEncode to convert the bean to xml persistence.
//
BeanInfo bi = Introspector.getBeanInfo(BeanToXmlTransient.class);
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
for (int i = 0; i < pds.length; i++) {
PropertyDescriptor propertyDescriptor = pds[i];
if (propertyDescriptor.getName().equals("itemQuantities")) {
propertyDescriptor.setValue("transient", Boolean.TRUE);
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
BeanToXmlTransient bean = new BeanToXmlTransient();
bean.setId(new Long(1));
bean.setItemName("T-Shirt");
bean.setItemColour("Dark Red");
bean.setItemQuantities(new Integer(100));
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream("BeanTransient.xml")));
//
// Write an XML representation of the specified object to the output.
//
encoder.writeObject(bean);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemColour() {
return itemColour;
}
public void setItemColour(String itemColour) {
this.itemColour = itemColour;
}
public Integer getItemQuantities() {
return itemQuantities;
}
public void setItemQuantities(Integer itemQuantities) {
this.itemQuantities = itemQuantities;
}
}
Tuesday, October 21, 2008
In this example we'll listen to bean's property change event. We create a small bean named MyBean, adds attributes and getter/setter. We want to know or to get notified when the bean property name is changed.
First we need the add a PropertyChangeSupport field to the bean, with this object we will fire the property change event. When we need to listen for the change we have to create an implementation of a PropertyChangeListener. In this example we'll just use the MyBean class as the listener.
The PropertyChangeListener has a method called propertyChange and inside this method we'll implementing the code to get the event fired by the PropertyChangeSupport.
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.io.Serializable;
public class MyBean implements PropertyChangeListener, Serializable {
private Long id;
private String name;
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public MyBean() {
pcs.addPropertyChangeListener(this);
}
/**
* This method gets called when a bound property is changed.
*
* @param evt A PropertyChangeEvent object describing the event source
* and the property that has changed.
*/
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Name = " + evt.getPropertyName());
System.out.println("Old Value = " + evt.getOldValue());
System.out.println("New Value = " + evt.getNewValue());
}
public static void main(String[] args) {
MyBean bean = new MyBean();
bean.setName("My Initial Value");
bean.setName("My New Value");
bean.setName("My Yet Another Value");
}
//~ --------------------------------------------- Bean's Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
String oldValue = this.name;
this.name = name;
//
// Fires a property change event
//
pcs.firePropertyChange("name", oldValue, name);
}
}
First we need the add a PropertyChangeSupport field to the bean, with this object we will fire the property change event. When we need to listen for the change we have to create an implementation of a PropertyChangeListener. In this example we'll just use the MyBean class as the listener.
The PropertyChangeListener has a method called propertyChange and inside this method we'll implementing the code to get the event fired by the PropertyChangeSupport.
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.io.Serializable;
public class MyBean implements PropertyChangeListener, Serializable {
private Long id;
private String name;
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public MyBean() {
pcs.addPropertyChangeListener(this);
}
/**
* This method gets called when a bound property is changed.
*
* @param evt A PropertyChangeEvent object describing the event source
* and the property that has changed.
*/
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Name = " + evt.getPropertyName());
System.out.println("Old Value = " + evt.getOldValue());
System.out.println("New Value = " + evt.getNewValue());
}
public static void main(String[] args) {
MyBean bean = new MyBean();
bean.setName("My Initial Value");
bean.setName("My New Value");
bean.setName("My Yet Another Value");
}
//~ --------------------------------------------- Bean's Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
String oldValue = this.name;
this.name = name;
//
// Fires a property change event
//
pcs.firePropertyChange("name", oldValue, name);
}
}
Sunday, October 19, 2008
The constrained property change is fired when a bean's value is about to change. When a VetoableChangeListener veto the value change the bean's value will be rolled-back to the previous value. In this example we have a constrained property called interest.
import java.beans.VetoableChangeSupport;
import java.beans.PropertyVetoException;
public class VetoBean {
private double interest;
private VetoableChangeSupport vcs = new VetoableChangeSupport(this);
public VetoBean() {
vcs.addVetoableChangeListener(new VetoChangeListener());
}
public double getInterest() {
return interest;
}
public void setInterest(double interest) {
try {
vcs.fireVetoableChange("interest", new Double(this.interest), new Double(interest));
this.interest = interest;
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
VetoBean bean = new VetoBean();
bean.setInterest(10.99);
bean.setInterest(15.99);
//
// PropertyVetoException will be thrown beacuse the interest value
// should not exceed 20.00.
//
bean.setInterest(20.99);
}
}
package org.kodejava.example.bean;
import java.beans.VetoableChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
public class VetoChangeListener implements VetoableChangeListener {
/**
* This method gets called when a constrained property is changed.
*
* @param evt a
* event source and the property that has changed.
* @throws java.beans.PropertyVetoException
* if the recipient wishes the property
* change to be rolled back.
*/
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
String eventName = evt.getPropertyName();
if (eventName.equalsIgnoreCase("interest")) {
double interest = ((Double) evt.getNewValue()).doubleValue();
if (interest > 20.00) {
throw new PropertyVetoException("Interest must be below 20.00", evt);
}
System.out.println("Interest applied = " + interest);
}
}
}
import java.beans.VetoableChangeSupport;
import java.beans.PropertyVetoException;
public class VetoBean {
private double interest;
private VetoableChangeSupport vcs = new VetoableChangeSupport(this);
public VetoBean() {
vcs.addVetoableChangeListener(new VetoChangeListener());
}
public double getInterest() {
return interest;
}
public void setInterest(double interest) {
try {
vcs.fireVetoableChange("interest", new Double(this.interest), new Double(interest));
this.interest = interest;
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
VetoBean bean = new VetoBean();
bean.setInterest(10.99);
bean.setInterest(15.99);
//
// PropertyVetoException will be thrown beacuse the interest value
// should not exceed 20.00.
//
bean.setInterest(20.99);
}
}
package org.kodejava.example.bean;
import java.beans.VetoableChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
public class VetoChangeListener implements VetoableChangeListener {
/**
* This method gets called when a constrained property is changed.
*
* @param evt a
PropertyChangeEvent object describing the* event source and the property that has changed.
* @throws java.beans.PropertyVetoException
* if the recipient wishes the property
* change to be rolled back.
*/
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
String eventName = evt.getPropertyName();
if (eventName.equalsIgnoreCase("interest")) {
double interest = ((Double) evt.getNewValue()).doubleValue();
if (interest > 20.00) {
throw new PropertyVetoException("Interest must be below 20.00", evt);
}
System.out.println("Interest applied = " + interest);
}
}
}
Saturday, October 18, 2008
import java.io.Serializable;
import java.io.IOException;
import java.beans.Beans;
public class TheBean implements Serializable {
private Long id;
private String name;
public TheBean() {
}
public static void main(String[] args) {
try {
TheBean bean = (TheBean) Beans.instantiate(
ClassLoader.getSystemClassLoader(), "org.kodejava.example.bean.TheBean");
System.out.println("The Bean = " + bean);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "[id=" + id + "; name=" + name + "]";
}
}
import java.io.IOException;
import java.beans.Beans;
public class TheBean implements Serializable {
private Long id;
private String name;
public TheBean() {
}
public static void main(String[] args) {
try {
TheBean bean = (TheBean) Beans.instantiate(
ClassLoader.getSystemClassLoader(), "org.kodejava.example.bean.TheBean");
System.out.println("The Bean = " + bean);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "[id=" + id + "; name=" + name + "]";
}
}
Friday, October 17, 2008
import java.io.Serializable;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.IntrospectionException;
public class Fruit implements Serializable {
private Long id;
private String name;
private String latinName;
private double price;
public Fruit() {
}
public static void main(String[] args) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(Fruit.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String propertyName = pd.getName();
System.out.println("propertyName = " + propertyName);
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLatinName() {
return latinName;
}
public void setLatinName(String latinName) {
this.latinName = latinName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.IntrospectionException;
public class Fruit implements Serializable {
private Long id;
private String name;
private String latinName;
private double price;
public Fruit() {
}
public static void main(String[] args) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(Fruit.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String propertyName = pd.getName();
System.out.println("propertyName = " + propertyName);
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLatinName() {
return latinName;
}
public void setLatinName(String latinName) {
this.latinName = latinName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Wednesday, October 15, 2008
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class FontNameList {
public static void main(String[] args) {
//
// Get all available fonts from GraphicsEnvironment
//
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();
//
// Iterates all available fonts and get their name and family name
//
for (Font font : fonts) {
String fontName = font.getName();
String familyName = font.getFamily();
System.out.println("Font: " + fontName + "; family: " + familyName);
}
}
}
import java.awt.GraphicsEnvironment;
public class FontNameList {
public static void main(String[] args) {
//
// Get all available fonts from GraphicsEnvironment
//
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();
//
// Iterates all available fonts and get their name and family name
//
for (Font font : fonts) {
String fontName = font.getName();
String familyName = font.getFamily();
System.out.println("Font: " + fontName + "; family: " + familyName);
}
}
}
Monday, October 13, 2008
import java.awt.Dimension;
import java.awt.Toolkit;
public class ScreenSizeExample {
public static void main(String[] args)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("Screen Widht: " + screenSize.getWidth());
System.out.println("Screen Height: " + screenSize.getHeight());
}
}
import java.awt.Toolkit;
public class ScreenSizeExample {
public static void main(String[] args)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("Screen Widht: " + screenSize.getWidth());
System.out.println("Screen Height: " + screenSize.getHeight());
}
}
Thursday, October 9, 2008
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class FontFamilyNameList {
public static void main(String[] args) {
//
// Get all available font family names from GraphicsEnvironment
//
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String familyNames[] = ge.getAvailableFontFamilyNames();
//
// Iterates familyNames array to display the available font's family names
//
for (String familyName : familyNames) {
System.out.println("Family names: " + familyName);
}
}
}
import java.awt.GraphicsEnvironment;
public class FontFamilyNameList {
public static void main(String[] args) {
//
// Get all available font family names from GraphicsEnvironment
//
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String familyNames[] = ge.getAvailableFontFamilyNames();
//
// Iterates familyNames array to display the available font's family names
//
for (String familyName : familyNames) {
System.out.println("Family names: " + familyName);
}
}
}
Tuesday, October 7, 2008
In this example you'll see how to capture a screenshot and save it into an image file such an a .png image. Some classes use in this program including the java.awt.Robot, java.awt.image.BufferedImage and javax.imageio.ImageIO.
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.io.File;
import java.io.IOException;
public class ScreenCapture {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// Capture screen from the top left in 200 by 200 pixel size.
//
BufferedImage bufferedImage = robot.createScreenCapture(
new Rectangle(new Dimension(200, 200)));
//
// The captured image will the writen into a file called
// screenshot.png
//
File imageFile = new File("screenshot.png");
ImageIO.write(bufferedImage, "png", imageFile);
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.io.File;
import java.io.IOException;
public class ScreenCapture {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// Capture screen from the top left in 200 by 200 pixel size.
//
BufferedImage bufferedImage = robot.createScreenCapture(
new Rectangle(new Dimension(200, 200)));
//
// The captured image will the writen into a file called
// screenshot.png
//
File imageFile = new File("screenshot.png");
ImageIO.write(bufferedImage, "png", imageFile);
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sunday, October 5, 2008
In this example we use the java.awt.Robot class to generate a key press event. We can call the keyPress(int keyCode) method to produce this event.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class CreatingKeyboardEvent {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// Create a three seconds delay.
//
robot.delay(3000);
//
// Genering key press event for writing the QWERTY letters
//
robot.keyPress(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_Y);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class CreatingKeyboardEvent {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// Create a three seconds delay.
//
robot.delay(3000);
//
// Genering key press event for writing the QWERTY letters
//
robot.keyPress(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_Y);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
Friday, October 3, 2008
The example here show us how to get the color of a pixel in the screen. We use the Robot.getPixelColor(int x, int y) method to obtain the Color of the pixel.
import java.awt.Color;
import java.awt.Robot;
import java.awt.AWTException;
public class ColorPickerDemo {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// The the pixel color information at 20, 20
//
Color color = robot.getPixelColor(20, 20);
//
// Print the RGB information of the pixel color
//
System.out.println("Red = " + color.getRed());
System.out.println("Green = " + color.getGreen());
System.out.println("Blue = " + color.getBlue());
} catch (AWTException e) {
e.printStackTrace();
}
}
}
import java.awt.Color;
import java.awt.Robot;
import java.awt.AWTException;
public class ColorPickerDemo {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// The the pixel color information at 20, 20
//
Color color = robot.getPixelColor(20, 20);
//
// Print the RGB information of the pixel color
//
System.out.println("Red = " + color.getRed());
System.out.println("Green = " + color.getGreen());
System.out.println("Blue = " + color.getBlue());
} catch (AWTException e) {
e.printStackTrace();
}
}
}
Thursday, October 2, 2008
In this example we are automating the process of creating mouse event using the java.awt.Robot class.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
public class MovingMouseDemo {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// Move mouse cursor to 200, 200
//
robot.mouseMove(200, 200);
//
// Press the mouse button #1.
//
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
//
// Scroll the screen up for a mouse with a wheel support.
//
robot.mouseWheel(-100);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
public class MovingMouseDemo {
public static void main(String[] args) {
try {
Robot robot = new Robot();
//
// Move mouse cursor to 200, 200
//
robot.mouseMove(200, 200);
//
// Press the mouse button #1.
//
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
//
// Scroll the screen up for a mouse with a wheel support.
//
robot.mouseWheel(-100);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
Subscribe to:
Posts (Atom)
SUBSCRIBE VIA eMAIL
Recent Posts
Archives
- December 2008 (5)
- November 2008 (15)
- October 2008 (17)
- September 2008 (9)
- August 2008 (12)
- July 2008 (19)
- June 2008 (22)
- May 2008 (17)
- April 2008 (2)
Categories
- java.sql (19)
- Examples (18)
- INTRODUCTION (9)
- JAVA APPLET (9)
- java.awt (8)
- java.net (8)
- java.beans (7)
- JAVA String Utility (6)
- Arrays (4)
- java.math (3)
- java.util.regex (3)
- Sort (2)
- Swing (2)
- java.security (2)
- java.util.zip (2)
- Catching Exceptions (1)
- Classes and Objects (1)
- Core Java Programs-part 1 (1)
- Core Java Programs-part 2 (1)
- Core Java Programs-part3 (1)
- Criticism of Java programming language (1)
- Falling Letters (1)
- File I/O and Streams (1)
- Fun With Letters and Words (1)
- Get current working directory (1)
- How do I convert String to Date object? (1)
- How do I convert string into InputStream? (1)
- How do i calculate directory size? (1)
- How to make executable jar files in JDK1.3.1? (1)
- Interfaces (1)
- JAVA Date Utility (1)
- Java Arithmetic Operators (1)
- Java Assignment Operators (1)
- Java Boolean Operators (1)
- Java Command Line Arguments (1)
- Java Comments (1)
- Java Conditional Operators (1)
- Java Data and Variables (1)
- Java Hello World Program (1)
- Java If-Else Statement (1)
- Java Increment and Decrement Operators (1)
- Java Loops (while (1)
- Java Relational Operators (1)
- Java Variables and Arithmetic Expressions (1)
- Java Virtual Machine (1)
- Know the current position of cursor (1)
- Letters (1)
- Methods (Includes Recursive Methods) (1)
- Rotating Lines (1)
- Send an email with attachment (1)
- Use for..each in Java (1)
- What is Autoboxing? (1)
- Write text file (1)
- connection to database (1)
- do-while and for loops) (1)

