Listing the available fonts on your system

You could use the Toolkit method getFontList:

      String fontnames[] = Toolkit.getDefaultToolkit().getFontList();

However, this method is deprecated. Here’s a newer version:

import java.awt.*;
 
public class Main
{
   public static void main(String[] args) {
 
     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
     String[] fontnames = env.getAvailableFontFamilyNames();
 
     System.out.println("Available fonts:n");
     for(int i=0; i<fontnames.length; i++)
        System.out.println(fontnames[i]);
     }
}

outputs:

Available fonts:

Andale Mono IPA
Arial
Arial Black
Arial Narrow
Book Antiqua
Bookman Old Style
Century Gothic
Comic Sans MS
Courier New
Curlz MT
Default
Dialog
dialog.bold
dialog.bolditalic
dialog.italic
DialogInput
dialoginput.bold
dialoginput.bolditalic
dialoginput.italic
Edwardian Script ITC
eelfont001
eelfont002
Engravers MT
Franklin Gothic Book
Franklin Gothic Demi Cond
Franklin Gothic Heavy
Franklin Gothic Medium Cond
Garamond
Georgia Ref
Haettenschweiler
Impact
Jokerman
Juice ITC
Lucida Bright
Lucida Console
Lucida Sans
Lucida Sans Typewriter
Lucida Sans Unicode
Marlett
Mediascape OSD Icon
Mistral
Monospaced
monospaced.bold
monospaced.bolditalic
monospaced.italic
MS Reference 1
MS Reference 2
MS Reference Sans Serif
MS Reference Serif
MS Reference Specialty
OCR A Extended
RefSpecialty
Rockwell
SansSerif
sansserif.bold
sansserif.bolditalic
sansserif.italic
Serif
serif.bold
serif.bolditalic
serif.italic
Symbol
Tahoma
Tera Special
Times New Roman
Trebuchet MS
Verdana
Verdana Ref
Webdings
Wingdings
Wingdings 2

Shearing an image

Shearing is performing an operation on the image in which the parallel lines will remain parallel while opposite corners are pulled apart (one or two dimensions).

Main.java:

import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   private Image image;
 
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      image = Toolkit.getDefaultToolkit().getImage("c:\djkrush.jpg");
      waitForImage(image);
      image = shearImage(image, 1, 0);
      setSize(image.getWidth(null), image.getHeight(null));
   }
 
   public void paint(Graphics g) {
      g.drawImage(image, 0, 0, this);
   }
 
   public void waitForImage(Image image) {
      MediaTracker mt = new MediaTracker(this);
 
      int id = 0;
      mt.addImage(image, id);
 
      try {
         mt.waitForID(id);
      }
      catch(InterruptedException e) {
         System.out.println("Image loading interrupted : " + e);
      }
   }
 
   public Image shearImage(Image image, double shearx, double sheary) {
      BufferedImage bi = toBufferedImage(image);
 
      AffineTransformOp op = new AffineTransformOp(
                   AffineTransform.getShearInstance(shearx, sheary), null); 
 
      return op.filter(bi, null); 
   }
 
   public BufferedImage toBufferedImage(Image image) {
      BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), 
                                           BufferedImage.TYPE_INT_RGB); 
 
      // copy the original image
      Graphics g = bi.createGraphics();
    
      g.drawImage(image, 0, 0, null);
      g.dispose();
 
      return bi;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setVisible(true);
   }
}

Image used:

Get the SUID of a class at runtime

Invoke the method getSerialVersionUID on an instance of the class ObjectStreamClass.

Main.java:

import java.io.*;
 
public class Main implements Serializable {
   public static void main(String args[]) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(Test1.class);
      System.out.println("serialVersionUID for class Test1: " + osc.getSerialVersionUID());
 
      osc = ObjectStreamClass.lookup(Test2.class);
      System.out.println("serialVersionUID for class Test2: " + osc.getSerialVersionUID());
   }
}
 
class Test1 implements Serializable {
}
 
class Test2 implements Serializable {
   static final long serialVersionUID = 1;
}

outputs:

serialVersionUID for class Test1: 3264391750135642662
serialVersionUID for class Test1: 1

Detecting CAPS LOCK in Java

From JDK1.3, you should normally be able to get the state using the Toolkit class as shown in following example.

Main.java

import java.awt.*;
 
public class Main
{
   public static void main(String []args) {
      Toolkit tk = Toolkit.getDefaultToolkit();
      System.out.println(tk.getLockingKeyState(java.awt.event.KeyEvent.VK_CAPS_LOCK));
 
      System.exit(1);
   }
}

However, check out this bug.

Difference between getAbsolutePath and getCanonicalPath

getCanonicalPath() resolves relative paths. It also restores original capitalization. You can use canonical paths to determine if two paths are the same, eg. c:windows..autoexec.bat and c:autoexec.bat will be equal if you get the path using getCanonicalPath but different with getAbsolutePath.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      printPath("autoexec.bat");
      printPath("c:\windows\system32\.\cloud.gif");
      printPath("c:\windows\..\autoexec.bat");
      printPath("c:\windows");
   }
 
   public static void printPath(String s) {
      try {
         System.out.println(s);
         System.out.println("tgetAbsolutePath()t" + new File(s).getAbsolutePath());
         System.out.println("tgetCanonicalPath()t" + new File(s).getCanonicalPath());
      }
      catch(IOException e) {
         System.out.println(e); 
      }
   }
}

outputs:

autoexec.bat
	getAbsolutePath()		C:\autoexec.bat
	getCanonicalPath()	C:autoexec.bat
c:windowssystem32.cloud.gif
	getAbsolutePath()		c:windowssystem32.cloud.gif
	getCanonicalPath()	C:WINDOWSSYSTEM32cloud.gif
c:windows..autoexec.bat
	getAbsolutePath()		c:windows..autoexec.bat
	getCanonicalPath()	C:autoexec.bat
c:windows
	getAbsolutePath()		c:windows
	getCanonicalPath()	C:WINDOWS

Catching events while an image is being read using ImageIO

Add an IIOReadProgressListener to your reader.

   IIOReadProgressListener listener = new MyReadProgressListener();
   reader.addIIOReadProgressListener(listener);

It’ll throw events while the reading process is going on. The following example shows a JProgressBar while reading the image fruit.png, to be used as background.

Main.java:

import javax.imageio.metadata.*; 
import javax.imageio.stream.*;
import javax.imageio.event.*;
import javax.imageio.*;
 
import javax.swing.event.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*; 
import java.io.*;
  
public class Main extends JFrame
{
   JProgressBar progressBar;
   BufferedImage background = null;
  
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      progressBar = new JProgressBar(0, 100);
 
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(BorderLayout.NORTH, progressBar);
    }
 
   public void loadBackgroundImage(String filename) throws Exception {
      Iterator readers = ImageIO.getImageReadersByFormatName("png");
      ImageReader reader = (ImageReader) readers.next();
 
      IIOReadProgressListener listener = new MyReadProgressListener();
      reader.addIIOReadProgressListener(listener);
 
      ImageInputStream iis = ImageIO.createImageInputStream(new File(filename));
      reader.setInput(iis, true);
      background = reader.read(0);
   }
 
   public static void main(String []args) throws Exception {
      Main main = new Main();
      main.setSize(400, 150);
      main.setVisible(true);
  
      main.loadBackgroundImage("fruit.png");
   }
 
   public void paint(Graphics g) {
      System.out.println(background);
      if (background != null) {
         g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
      }
   }
 
   class MyReadProgressListener implements IIOReadProgressListener {
 
	public MyReadProgressListener() {}
 
        public void imageProgress(ImageReader source, final float percentageDone) {
           System.out.println("imageProgress: " + percentageDone);
 
           SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                 progressBar.setValue((int) percentageDone);
              }
           });   
        }
 
        public void imageComplete(ImageReader source) {
           SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                 try {
                    // wait for a bit until bufferedimage is assigned by reader
                    Thread.sleep(50);
                 }
                 catch(InterruptedException e) { }
                 repaint();
              }
           });              
        }
 
        public void imageStarted(ImageReader source, int imageIndex)  { }
        public void readAborted(ImageReader source)                   { }
        public void sequenceComplete(ImageReader source)              { }
        public void sequenceStarted(ImageReader source, int minIndex) { }
        public void thumbnailComplete(ImageReader source)             { }
        public void thumbnailProgress(ImageReader source, float percentageDone) { }
        public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) { }
   }
}

Loading a class with the classloading mechanism in JDK1.2

When the JRE needs to load a new class, it consults following locations:

- bootstrap classes: the core API, rt.jar and i18n.jar
- installed extentions: all jars in lib/ext of JRE_HOME
- system classes: classes specified in the classpath
- download extensions: classes specified in the classpath header of installed extension jars, classpath jars, or previously downloaded download extensions.

1.2 has a delegation model for loading a class. Before loading a class with the current class loader, it is first delegated to the parent to check whether it can be loaded from there. For example, you could define your own package java.util and create a class Vector. Your class would be in the classpath and part of the system class loader, but this one delegates the request first to its parent, the bootstrap class loader which finds the class and loads it from there. This prevents a user from overriding security restrictions by replacing a class in the core API by an own version of the class.

You can create your own class loader by extending from java.lang.ClassLoader and you can specify a parent class loader. If you don’t specify the parent (null), then the system class loader will be its parent.

Filling up an array

Since JDK1.2, the utility class Arrays in the java.util package provides useful
array manipulation methods such as fill, binary search and sort.

You can easily fill up an array with a for loop:

      int values[] = { 5, 74, 23, 99, 6, 0, -2, -60 };
 
      for (int i=0; i<values.length; i++)
         values[i] = 69;

You could also use the Arrays.fill method defined for arrays of type byte[], char[],
double[], float[], int[], long[], object[].

Eg.

      Arrays.fill(values, 69);

It doesn’t work any faster than writing the for loop yourself, it does exactly
the same (contains a for loop), except that it does some range checking.
So, theoretically using Arrays.fill is slower, but your code looks a lot cleaner.
Range checking is necessary if you want to fill up only part of the array using
fill(array, fromvalue, tovalue, value).

      Arrays.fill(values, 1, 4, 69);  yields in the array { 5, 69, 69, 69, 6, 0, -2, -60 }

Creating an instance of an inner class from a static method of the outer class

A static inner class can be instantiated without the need of an instance of the outer class.

Main.java:

public class Main {
   public static void main(String args[]) {
 
      Main main = new Main();
      Main.InnerClass ic = new InnerClass();
      ic.test();
   }
 
   static class InnerClass {
      public void test() {
         System.out.println("Method test called!");
      }
   } 
}

You need an instance of the outer class to instantiate a non-static inner class. Just specify it before the new operator.

Main.java:

public class Main {
   public static void main(String args[]) {
 
      Main main = new Main();
      Main.InnerClass ic = main.new InnerClass();
      ic.test();
   }
 
   class InnerClass {
      public void test() {
         System.out.println("Method test called!");
      }
   } 
}

Getting a list of all declared fields and values in an Object

Use the reflection API. It allows you to inspect class members at runtime. The
process is simple: get the classname from the class you want to inspect and invoke
getDeclaredFields. This will give you an array of member variables that have
been declared in that object. To get the actual value, call the get method on
a field instance with the object you want to inspect.

You are not allowed to inspect private values, unless you specifically ask for it
(see second example).

Person.java (the class we are inspecting)

public class Person
{
   public String name;
   private int age;
 
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
}

Main.java

import java.lang.reflect.Field;
  
public class Main
{
   public static void main(String[] args)
   {
      Person p = new Person("John Doe", 36);
 
      try {
         listObjectMembers(p);
      }
      catch(Exception e) { 
         System.out.println(e);
      }
   }
 
   private static void listObjectMembers(Object o) {       
      if (o == null) return;
        
      Class cl = o.getClass();
      Field fields[] = cl.getDeclaredFields();
      for (int i=0; i<fields.length; i++) {
         Field field = fields[i];
         Class fieldType = field.getType();            
 
         System.out.println(fieldType.getName() + " " + field.getName());
 
         try {
            Object member = field.get(o);
            System.out.println("thas value: " + member);
         }
         catch(IllegalAccessException e) {
            System.out.println("t" + e);     
         }        
      }
   }
}

Running this program will yield:

java.lang.String name
	has value: John Doe
int age
	java.lang.IllegalAccessException: Person

As the integer “age” is declared as being private to the class, the Java Runtime
Environment will throw an IllegalAccessException. However, since Java 2, a new
function has been added to the Field class that solves this problem.
Invoke setAccessible(true) on a field that is private, and you’ll get the value.

import java.lang.reflect.Field;
  
public class Main
{
   public static void main(String[] args)
   {
      Person p = new Person("John Doe", 36);
 
      try {
         listObjectMembers(p);
      }
      catch(Exception e) { 
         System.out.println(e);
      }
   }
 
   private static void listObjectMembers(Object o) {       
      if (o == null) return;
        
      Class cl = o.getClass();
      Field fields[] = cl.getDeclaredFields();
      for (int i=0; i<fields.length; i++) {
         Field field = fields[i];
         Class fieldType = field.getType();            
 
         System.out.println(fieldType.getName() + " " + field.getName());
 
         try {
            field.setAccessible(true);
            Object member = field.get(o);
            System.out.println("thas value: " + member);
         }
         catch(IllegalAccessException e) {
            System.out.println("t" + e);     
         }        
      }
   }
}

outputs:

java.lang.String name
	has value: John Doe
int age
	has value: 36

See the Reflection API for more functionality.