Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, March 26, 2008

Application Virtualization for Enterprise Java

Learn how to take the next step in Java virtualization: achieving flexibility through policy-based, next-generation application virtualization technology.

In this free white paper, learn how to make your computing resources more flexible, responsive, and highly available—all by deploying a management framework to proactively address key Java virtualization challenges. Find out how you can achieve centralized, policy-based control across both virtualized and non-virtualized Java applications, to automate key data center operations and ensure application SLAs are met. Get virtualization under control.

Download your free copy now

Enterprise Java Virtualization

Cut ownership costs by letting enterprise Java applications run directly inside a virtual machine—without overhead from a traditional OS.

In this free executive brief, discover a better, less costly way to run Java on hypervisor-based virtualization platforms. Understand important TCO factors for a virtualized enterprise Java application, as well as the benefits of eliminating the OS layer. Then, discover a useful model for comparing the potential costs of running Java apps under competing architectures. This free brief is a must-read for those charged with keeping TCO low.

Download your free copy now

The Java Virtual Appliance - No OS Required

Learn how a new approach to Java virtualization can make your data center's hardware go farther—and run faster.

This free white paper reveals how a new Java virtualization approach can result in a streamlined "Java Virtual Appliance" that lets you leverage your current hardware for years—without the usual OS overhead. Learn why most servers run at only 10 percent of capacity; how virtualization saves an average of 23 percent on server space, power, and cooling; and how virtual middleware enhances speed, flexibility, and ROI.

Download your free copy now.

Saturday, March 22, 2008

Introduction to Glass Panes in Swing

In the Java Swing classes JFrame and JApplet, the glass pane provides a unique and powerful feature. The glass pane is a layer above all other controls within the JFrame and JApplet. It allows you to paint above all other controls.

In addition to drawing graphics over the contents of a JFrame or JApplet, the glass pane can also be used to handle mouse events. Be aware that adding a mouse listener to the glass pane will prevent any mouse events from being sent to the controls below it.

The first example will demonstrate drawing graphics over all controls in the JFrame.

//Create a panel to use as the glass pane
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g)
{
g.setColor(Color.red);

//Draw an oval in the panel
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
};

//Turn off the opaque attribute of the panel
//This allows the controls to show through
panel.setOpaque(false);

//Set the glass pane in the JFrame
setGlassPane(panel);

//Display the panel
panel.setVisible(true);
The results of the glass pane:


By setting the opaque attribute on the JPanel to false, it turns of the background of the JPanel. When the panel is painted, only the graphics in the paintComponent will be drawn without the background.

By simply modifying the paintComponent you can create a completely different effect. In the next example, we set the alpha level for the color. This allows a color to be painted while allowing the controls below it to be shown.
//Create a panel to use as the glass pane
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g)
{
//Set the color to with red with a 50% alpha
g.setColor(new Color(1, 0, 0, 0.5f));

//Fill a rectangle with the 50% red color
g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);
}
};

The results of glass pane:


This tutorial only covers the basics of using the glass pane. There are a number of interesting possible uses of the glass pane. Below are some ideas of things to try with the glass pane:
  • Create a transition effects between screens.
  • Display a progress screen within the glass pane.
  • Display debug information within the glass pane.
Related Links
Swing Hacks -Chapter 6 has some cool examples for creating a progress indicator in the glass pane.

Friday, March 7, 2008

Java for iPhone

Infoworld is reporting that Sun will be making Java Micro Edition (JME) available for iPhone. Sun says they try to provide as much native functionality as possible. Sun plans to release it sometime after June.

Although Sun has made this announcement, there maybe legal issues with this. In the iPhone SDK Agreement, it states:

No interpreted code may be downloaded and used in an Application except for code that is interpreted and run by Apple's Published APIs and builtin interpreter(s).
I hope that Apple and Sun can getting the legal issues resolved, so the release of JME isn't prevented.

For the full article, visit InfoWorld.

Thursday, March 6, 2008

Convert a String to Lower Case in Java

This tutorial will show you how to convert a string to lower case in Java. To convert a string to lower case, call the toLowerCase() method on the string.

Below is an example:

String original = "HELLO";

String lowerCase = original.toLowerCase();

Convert a String to Upper Case in Java

This tutorial will show you how to convert a string to upper case in Java. To convert a string to upper case, call the toUpperCase() method on the string.

Below is an example:

String original = "hello";

String upperCase = original.toUpperCase();

Get the List of Files in a Directory using Java

This tutorial shows how to get the list of files that are in a specific directory using Java. This tutorial will also show you how to filter the files to get only the files you are interested in.

To get the list of files, you need to create a File object for the directory. Next, you can call the listFiles() method on this File object to get the list of files contained in this directory.

File directory = new File("c:\\");
File[] files = directory.listFiles();

for (int index = 0; index < files.length; index++)
{
//Print out the name of files in the directory
System.out.println(files[index].toString());
}

If you don't want to get all the files in a directory, you can create a class that implements the FileFilter (java.io package) interface. The accept() method that you implement will specify the criteria to be used to determine if the file is returned. In our example below, the filter will only return files with "txt" for the extension.

class Filter implements FileFilter
{
public boolean accept(File file)
{
return file.getName().endsWith("txt");
}
}

To use apply this filter, pass an instance of it to the listFiles() method. Below is an example:

files.listFiles(new Filter());

Saturday, March 1, 2008

Convert a String to Number in Java

This tutorial will show you how to convert a string to different number types. Each number type in Java has a parse method that allows you convert a string into the primitive type.

When converting a string to a number, the parse method may throw a NumberFormatException if the string is null or an invalid representation for that type.

Convert a String to an int
To convert a String to an int, call the static method parseInt() on the Integer class. Below is an example:

String string = "123";
int value = Integer.parseInt(string);
Convert a String to a long
To convert a String to a long, call the static method parseLong() on the Long class. Below is an example:
String string = "123";
long value = Long.parseLong(string);

Convert a String to a float
To convert a String to a float, call the static method parseFloat() on the Float class. Below is an example:
String string = "123.4";
float value = Float.parseFloat(string);

Convert a String to a double
To convert a String to a double, call the static method parseDouble() on the Double class. Below is an example:
String string = "123.4";
double value = Double.parseDouble(string);

Split a String into Words in Java

This tutorial shows how to split a string into multiple words.

In Java 1.4, Sun added a split() method to the String class. The split method allows you to specify a regular expression that is used to split the string into a string array. To split a string where there words are separated by a space, simply specify a space (" ") as the parameter to the split() method.

The following code example shows how to split a sentence into words:

String sentence = "This is a sentence.";
String[] words = sentence.split(" ");

for (String word : words)
{
System.out.println(word);
}
Below is output from the example code:
This
is
a
sentence.

Convert a BMP to a PNG in Java

This tutorial shows how to use the ImageIO API to convert a BMP to a PNG image in Java. The ImageIO API provides methods to read the source image and to write the image in the new file format.

To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.

//Create file for the source
File input = new File("c:/temp/image.bmp");

//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);

Once you have the BufferedImage, you can write the image as a PNG. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "png".

//Create a file for the output
File output = new File("c:/temp/image.png");

//Write the image to the destination as a PNG
ImageIO.write(image, "png", output);

Convert a BMP to a JPG in Java

This tutorial shows how to use the ImageIO API to convert a BMP to a JPG image in Java. The ImageIO API provides methods to read the source image and to write the image in the new file format.

To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.

//Create file for the source
File input = new File("c:/temp/image.bmp");

//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);

Once you have the BufferedImage, you can write the image as a JPG. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "jpg".

//Create a file for the output
File output = new File("c:/temp/image.jpg");

//Write the image to the destination as a JPG
ImageIO.write(image, "jpg", output);

Convert a PNG to a BMP in Java

This tutorial shows how to use the ImageIO API to convert a PNG to a BMP image in Java. The ImageIO API provides methods to read the source image and to write the image in the new file format.

To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.

//Create file for the source
File input = new File("c:/temp/image.png");

//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);

Once you have the BufferedImage, you can write the image as a BMP. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "bmp".

//Create a file for the output
File output = new File("c:/temp/image.bmp");

//Write the image to the destination as a BMP
ImageIO.write(image, "bmp", output);

Convert a JPG to a BMP in Java

This tutorial shows how to use the ImageIO API to convert a JPG to a BMP image in Java. The ImageIO API provides methods to read the source image and to write the image in the new file format.

To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.

//Create file for the source
File input = new File("c:/temp/image.jpg");

//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);

Once you have the BufferedImage, you can write the image as a BMP. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "bmp".

//Create a file for the output
File output = new File("c:/temp/image.bmp");

//Write the image to the destination as a BMP
ImageIO.write(image, "bmp", output);

Convert a GIF to a BMP in Java

This tutorial shows how to use the ImageIO API to convert a GIF to a BMP image in Java. The ImageIO API provides methods to read the source image and to write the image in the new file format.

To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.

//Create file for the source
File input = new File("c:/temp/image.gif");

//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);

Once you have the BufferedImage, you can write the image as a BMP. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "bmp".

//Create a file for the output
File output = new File("c:/temp/image.bmp");

//Write the image to the destination as a BMP
ImageIO.write(image, "bmp", output);

Convert a Color Image to a Gray Scale Image in Java

This tutorial will show you how to convert a color image to a gray scale image in Java. In Java, there are a number of ways to convert a color image to gray scale. This tutorial will show you three ways convert the image including output quality and performance.

For reference, below is the color image that was converted:

Changing the Color Space
One way to convert a color image to gray scale, is to change the color space of the image. The sample code below shows how to change the color space:

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
BufferedImage image = op.filter(bufferedImage, null);

Performance: Poor (approx. 60ms)
Image Results: Poor


Drawing to a Grayscale BufferedImage
The easiest way to convert a color image to a gray scale image is to simply draw the color image to a gray scale BufferedImage. The sample code below shows how to draw the colored image to a gray scale BufferedImage:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();

Performance: Good (approx. 9 ms)
Image Results: Best


Using the GrayFilter
The final way to convert a color image to a gray scale image is to use the GrayFilter. In order to use the GrayFilter, the color image must either be an ImageProducer or a BufferedImage (use the getSource() method to get the ImageProducer). The sample code below shows how to use the GrayFilter:

ImageFilter filter = new GrayFilter(true, 50);
ImageProducer producer = new FilteredImageSource(colorImage.getSource(), filter);
Image mage = this.createImage(producer);

Performance: Best (approx. 1 ms)
Image Results: Good. When constructing the GrayFilter, you can control the brightness of the grays with the second parameter.

Which Method Should You Use
The low quality and slow performance, should rule out using the ColorSpace method. If you need the best performance and can live with the image quality, the GrayFilter would be the option for you. Drawing to a gray scale BufferedImage is the overall best option, it is only a few milliseconds slower than the best, the image quality is the best, and the code is the simplest.

What is JSP?

JSP or Java Server Pages is a Java technology that allows developers to dynamically generate HTML, XML or some other type of web page. The technology allows Java code and certain pre-defined actions to be embedded into static content.

The JSP syntax adds additional XML tags, called JSP actions, to be used to invoke built-in functionality. Additionally, the technology allows for the creation of JSP tag libraries that act as extensions to the standard HTML or XML tags. Tag libraries provide a platform independent way of extending the capabilities of a web server.

JSPs are compiled into Servlets by a JSP compiler. A JSP compiler may generate a servlet in Java code that is then compiled by the Java compiler, or it may generate byte code for the servlet directly. In either case, it is helpful to understand how the JSP compiler transforms the page into a Java servlet.

JSP and Servlets
Architecturally speaking, JSP can be viewed as a high-level abstraction of servlets that is implemented as an extension of the Servlet 2.1 API.

About this Terminology
This terminology is from The Wikipedia which is published under the GNU Free Documentation License.

Wednesday, February 27, 2008

What is a JDBC Type 4 Driver?

The JDBC type 4 driver, also known as the native-protocol driver is a database driver implementation that converts JDBC calls directly into the vendor-specific database protocol.

The type 4 driver is written completely in Java and is hence platform independent. It provides better performance over the type 1 and 2 drivers as it does not have the overhead of conversion of calls into ODBC or database API calls. Unlike the type 1 and 2 drivers, it does not need associated software to work.

As the database protocol is vendor-specific, separate drivers, usually vendor-supplied, need to be used to connect to the database.

About this Terminology
This terminology is from The Wikipedia which is published under the GNU Free Documentation License.

What is a JDBC Type 3 Driver?

The JDBC type 3 driver, also known as the network-protocol driver is a database driver implementation which makes use of a middle-tier between the calling program and the database. The middle-tier (application server) converts JDBC calls directly or indirectly into the vendor-specific database protocol.

This differs from the type 4 driver in that the protocol conversion logic resides not at the client, but in the middle-tier. However, like type 4 drivers, the type 3 driver is written entirely in Java.

The same driver can be used for multiple databases. It depends on the number of databases the middleware has been configured to support. The type 3 driver is platform-independent as the platform-related differences are taken care by the middleware. Also, making use of the middleware provides additional advantages of security and firewall access.

About this Terminology
This terminology is from The Wikipedia which is published under the GNU Free Documentation License.

What is a JDBC Type 2 Driver?

The JDBC type 2 driver, also known as the Native-API driver is a database driver implementation that uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API.

The type 2 driver is not written entirely in Java as it interfaces with non-Java code that makes the final database calls. The driver is compiled for use with the particular operating system. For platform interoperability, the Type 4 driver, being a full-Java implementation, is preferred over this driver.

However the type 2 driver provides more functionality and performance that the type 1 driver as it does not have the overhead of the additional ODBC function calls.

About this Terminology
This terminology is from The Wikipedia which is published under the GNU Free Documentation License.