How do I launch user-default web browser?

The code below show you how to browse a website using the user’s default web browser. To get the default web browser you can use the Desktop class browse(URI uri) method call.

package org.kodejava.awt;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;

public class RunningDefaultBrowser {
    public static void main(String[] args) {
        URI uri = URI.create("https://kodejava.org");
        try {
            // Get Desktop instance of the current browser context. An 
            // UnsupportedOperationException will be thrown if the 
            // platform doesn't support Desktop API. 
            Desktop desktop = Desktop.getDesktop();

            // Browse the uri using user's default web browser.
            desktop.browse(uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I get and display an image in Applet?

To display an image in an applet we first need to obtain the image object itself. A call to applet’s getImage(URL url, String name) method help us to create an image object from the specified URL of the image.

For displaying the image on the applet’s screen we draw it in the paint() method using Graphics.drawImage() method.

package org.kodejava.applet;

import java.applet.Applet;
import java.awt.*;

public class AppletGetImage extends Applet {
    private Image logo;

    @Override
    public void init() {
        // Get an Image object that can be painted on the Applet
        // screen. We need to supply the URL of the document as
        // the base location of the image and the location of the
        // image relative the base URL. 
        logo = getImage(getDocumentBase(), "/images/logo.png");
    }

    @Override
    public void paint(Graphics g) {
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        g.drawImage(logo, 10, 10, this);
    }
}
** Deprecated: The Applet API is deprecated since JDK 9, no replacement.

How do I get Applet’s document URL?

The code snippet below show you how to get the URL of the document (HTML, JSP, etc.) where the Applet is embedded. To obtain this document URL we use the getDocumentBase() method call provided by the Applet class.

In the paint() method below we use the getDocumentBase() to create a URL as a link to an image to be displayed by our applet.

package org.kodejava.applet;

import java.applet.Applet;
import java.awt.*;

public class AppletDocumentBase extends Applet {
    private Image logo;

    @Override
    public void init() {
        // Locates logo image base on the URL of the document
        // where the Applet is embedded which is returned by
        // the getDocumentBase() method call.
        //
        // eg. http://localhost:8080/images/logo.jpg
        logo = getImage(getDocumentBase(), "/images/logo.png");
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);

        // Draw the logo image on the Applet surface.
        g.drawImage(logo, 10, 10, this);
    }
}
** Deprecated: The Applet API is deprecated since JDK 9, no replacement.

How do I get printer or print service name?

This example show you how to get the printer or print service installed on your machine. To get the installed services we can use PrinterJob.lookupPrintServices() method call. This method return an array of PrintService objects. After that call PrintService.getName() method to get the print service name.

package org.kodejava.print;

import javax.print.PrintService;
import java.awt.print.PrinterJob;

public class PrinterName {
    public static void main(String[] args) {
        // Lookup for the available print services.
        PrintService[] printServices = PrinterJob.lookupPrintServices();

        // Iterates the print services and print out its name.
        for (PrintService printService : printServices) {
            String name = printService.getName();
            System.out.println("Name = " + name);
        }
    }
}

The program will print the installed print service on your machine.

Name = OneNote for Windows 10
Name = OneNote (Desktop)
Name = Microsoft XPS Document Writer
Name = Microsoft Print to PDF
Name = HP LaserJet P1005
Name = Fax

How do I clear a buffer using compact() method?

If you want to clear a buffer, but you want to keep the unread data in the buffer then you need to use the compact() method of the buffer. The compact() method will copy the unread data to the beginning of the buffer and set the position right after the unread data. The limit itself still have the value equals to the buffer capacity. The buffer will be ready to be written again without overwriting the unread data.

package org.kodejava.io;

import java.nio.CharBuffer;

public class BufferCompact {
    public static void main(String[] args) throws Exception {
        CharBuffer buffer = CharBuffer.allocate(64);
        buffer.put("Let's write some Java code! ");

        System.out.println("Position: " + buffer.position());
        System.out.println("Limit   : " + buffer.limit());

        // Read 10 chars from the buffer.
        buffer.flip();
        for (int i = 0; i < 10; i++) {
            System.out.print(buffer.get());
        }
        System.out.println();

        System.out.println("Position: " + buffer.position());
        System.out.println("Limit   : " + buffer.limit());

        // clear the buffer using compact() method.
        buffer.compact();
        System.out.println("Position: " + buffer.position());
        System.out.println("Limit   : " + buffer.limit());

        // Write and read some more data.
        buffer.put("Add some more data.");

        buffer.flip();
        while (buffer.hasRemaining()) {
            System.out.print(buffer.get());
        }
    }
}

The output of the code snippet above is:

Position: 28
Limit   : 64
Let's writ
Position: 10
Limit   : 28
Position: 18
Limit   : 64
e some Java code! Add some more data.