Pages

Search

 

31 July 2009

[JAVA] Displaying Images with the DisplayJAI class

INTRODUCTION

DisplayJAI is a class distributed with the Java Advanced Imaging API that can display an instance of the RenderedImage class, including instances of PlanarImage and TiledImage (tiling is supported). Since it inherits from JComponent it can be used in graphical interfaces as any other component.

This chapter shows some basic usage examples. Other chapters in this section shows how to extend the class for more specific tasks.

Important: A message on this class' API documentation says: This class is not a committed part of the JavaTM Advanced Imaging API per se. It might therefore not be supported by JAI implementations other than that of Sun Microsystems, Inc.

Displaying Images with the DisplayJAI class

A simple application will demonstrate the usage of the DisplayJAI class. It will load an image from disk, create a simple graphical interface with the DisplayJAI on the center and a text label on the bottom. The instance of DisplayJAI will be contained inside a JScrollPane so if the image is larger than the application's window the user will be able to the scrollbars to display different viewports of the image. The text label will show information about the image.

CODE :

1 /*
2 * Part of the Java Image Processing Cookbook, please see
3 * http://www.lac.inpe.br/~rafael.santos/JIPCookbook/index.jsp
4 * for information on usage and distribution.
5 * Rafael Santos (rafael.santos@lac.inpe.br)
6 */
7 package display.basic;
8
9 import java.awt.BorderLayout;
10 import java.awt.Container;
11
12 import javax.media.jai.JAI;
13 import javax.media.jai.PlanarImage;
14 import javax.swing.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JScrollPane;
17
18 import com.sun.media.jai.widget.DisplayJAI;
19
20 /**
21 * This application shows how to use the DisplayJAI class with a JScrollPane to display images.
22 */
23 public class DisplayJAIExample
24 {
25 /**
26 * Entry point for this application.
27 * @param args an image file name.
28 */
29 public static void main(String[] args)
30 {
31 // Load the image which file name was passed as the first argument to the
32 // application.
33 PlanarImage image = JAI.create("fileload", args[0]);
34 // Get some information about the image
35 String imageInfo = "Dimensions: "+image.getWidth()+"x"+image.getHeight()+
36 " Bands:"+image.getNumBands();
37 // Create a frame for display.
38 JFrame frame = new JFrame();
39 frame.setTitle("DisplayJAI: "+args[0]);
40 // Get the JFrame's ContentPane.
41 Container contentPane = frame.getContentPane();
42 contentPane.setLayout(new BorderLayout());
43 // Create an instance of DisplayJAI.
44 DisplayJAI dj = new DisplayJAI(image);
45 // Add to the JFrame's ContentPane an instance of JScrollPane containing the
46 // DisplayJAI instance.
47 contentPane.add(new JScrollPane(dj),BorderLayout.CENTER);
48 // Add a text label with the image information.
49 contentPane.add(new JLabel(imageInfo),BorderLayout.SOUTH);
50 // Set the closing operation so the application is finished.
51 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52 frame.setSize(400,400); // adjust the frame size.
53 frame.setVisible(true); // show the frame.
54 }
55 }
Screenshot :

[JAVA] Java Programming Language "Hello World"


/*
* Outputs "Hello, world!" and then exits
*/

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
By convention, source files are named after the public class they contain, appending the suffix .java, for example, HelloWorld.java. It must first be compiled into bytecode, using a Java compiler, producing a file named HelloWorld.class. Only then can it be executed, or 'launched'. The java source file may only contain one public class but can contain multiple classes with less than public access and any number of public inner classes.

A class that is declared private may be stored in any .java file. The compiler will generate a class file for each class defined in the source file. The name of the class file is the name of the class, with .class appended. For class file generation, anonymous classes are treated as if their name was the concatenation of the name of their enclosing class, a $, and an integer.

The keyword public denotes that a method can be called from code in other classes, or that a class may be used by classes outside the class hierarchy. The class hierarchy is related to the name of the directory in which the.java file is.

The keyword static in front of a method indicates a static method, which is associated only with the class and not with any specific instance of that class. Only static methods can be invoked without a reference to an object. Static methods cannot access any method variables that are not static.

The keyword void indicates that the main method does not return any value to the caller. If a Java program is to exit with an error code, it must call System.exit() explicitly.

The method name "main" is not a keyword in the Java language. It is simply the name of the method the Java launcher calls to pass control to the program. Java classes that run in managed environments such as applets and Enterprise Java Beans do not use or need a main() method. A java program may contain multiple classes that have main methods, which means that the VM needs to be explicitly told which class to launch from.

The main method must accept an array of String objects. By convention, it is referenced as args although any other legal identifier name can be used. Since Java 5, the main method can also use variable arguments, in the form of public static void main(String... args), allowing the main method to be invoked with an arbitrary number of String arguments. The effect of this alternate declaration is semantically identical (the args parameter is still an array of String objects), but allows an alternate syntax for creating and passing the array.

The Java launcher launches Java by loading a given class (specified on the command line or as an attribute in a JAR) and starting its public static void main(String[]) method. Stand-alone programs must declare this method explicitly. The String[] args parameter is an array of String objects containing any arguments passed to the class. The parameters to main are often passed by means of a command line.

Printing is part of a Java standard library: The System class defines a public static field called out. The out object is an instance of the PrintStream class and provides many methods for printing data to standard out, including println(String) which also appends a new line to the passed string.

The string "Hello world!" is automatically converted to a String object by the compiler.

[JAVA] Java Programming Language Syntax

The syntax of Java is largely derived from C++. Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object oriented language. All code is written inside a class and everything is an object, with the exception of the intrinsic data types (ordinal and real numbers, boolean values, and characters), which are not classes for performance reasons.

Java suppresses several features (such as operator overloading and multiple inheritance) for classes in order to simplify the language and to prevent possible errors and anti-pattern design.

Java uses the same commenting methods as C++. There are two different methods of commenting, the first is generally used for single line comments // two forward slashes and the second is generally used for multiple line commenting, this requires an open and close. In order to use the second method of commenting you must use the forward slash asterisk (/*) and it must end with an asterisk forward slash (*/)

//This is an example of a single line comment using two forward slashes
/* This is an example of a multiple line comment using the forward slash
and asterisk. This type of comment can be used to hold a lot of information
but it is very important to remember to close the comment. */

[JAVA] Java Programming Language Automatic Memory Management


Java uses an automatic garbage collector to manage memory in the object lifecycle. The programmer determines when objects are created, and the Java runtime is responsible for recovering the memory once objects are no longer in use. Once no references to an object remain, the unreachable object becomes eligible to be freed automatically by the garbage collector. Something similar to a memory leak may still occur if a programmer's code holds a reference to an object that is no longer needed, typically when objects that are no longer needed are stored in containers that are still in use. If methods for a nonexistent object are called, a "null pointer exception" is thrown. [16][17]

One of the ideas behind Java's automatic memory management model is that programmers be spared the burden of having to perform manual memory management. In some languages memory for the creation of objects is implicitly allocated on the stack, or explicitly allocated and deallocated from the heap. Either way the responsibility of managing memory resides with the programmer. If the program does not deallocate an object, a memory leak occurs. If the program attempts to access or deallocate memory that has already been deallocated, the result is undefined and difficult to predict, and the program is likely to become unstable and/or crash. This can be partially remedied by the use of smart pointers, but these add overhead and complexity.

Garbage collection may happen at any time. Ideally, it will occur when a program is idle. It is guaranteed to be triggered if there is insufficient free memory on the heap to allocate a new object; this can cause a program to stall momentarily. Where performance or response time is important, explicit memory management and object pools are often used.

Java does not support C/C++ style pointer arithmetic, where object addresses and unsigned integers (usually long integers) can be used interchangeably. This allows the garbage collector to relocate referenced objects, and ensures type safety and security.

As in C++ and some other object-oriented languages, variables of Java's primitive types are not objects. Values of primitive types are either stored directly in fields (for objects) or on the stack (for methods) rather than on the heap, as commonly true for objects (but see Escape analysis). This was a conscious decision by Java's designers for performance reasons. Because of this, Java was not considered to be a pure object-oriented programming language. However, as of Java 5.0, autoboxing enables programmers to proceed as if primitive types are instances of their wrapper classes.

[JAVA] Java Implementation

Sun Microsystems officially licenses the Java Standard Edition platform for Microsoft Windows, Linux, Mac OS X, and Solaris. Through a network of third-party vendors and licensees[14], alternative Java environments are available for these and other platforms.

Sun's trademark license for usage of the Java brand insists that all implementations be "compatible". This resulted in a legal dispute with Microsoft after Sun claimed that the Microsoft implementation did not support RMI or JNI and had added platform-specific features of their own. Sun sued in 1997, and in 2001 won a settlement of $20 million as well as a court order enforcing the terms of the license from Sun.[15] As a result, Microsoft no longer ships Java with Windows, and in recent versions of Windows, Internet Explorer cannot support Java applets without a third-party plugin. Sun, and others, have made available free Java run-time systems for those and other versions of Windows.

Platform-independent Java is essential to the Java EE strategy, and an even more rigorous validation is required to certify an implementation. This environment enables portable server-side applications, such as Web services, servlets, and Enterprise JavaBeans, as well as with embedded systems based on OSGi, using Embedded Java environments. Through the new GlassFish project, Sun is working to create a fully functional, unified open-source implementation of the Java EE technologies.

Sun also distributes a superset of the JRE called the Java 2 SDK (more commonly known as the JDK), which includes development tools such as the Java compiler, Javadoc, Jar and debugger.

[JAVA] What is Java Programming Language?



Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.

There were five primary goals in the creation of the Java programming language
1. It should be "simple, object oriented, and familiar".
2. It should be "robust and secure".
3. It should be "architecture neutral and portable".
4. It should execute with "high performance".
5. It should be "interpreted, threaded, and dynamic".