Year of the Desktop Linux

July 12th, 2007
    
import java.awt.*;
import java.util.Calendar;

import javax.swing.*;

public class YearOfLinuxDesktop {
  private static class YearOfLinuxDesktopPanel 
        extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D)g.create();
      g2d.setColor(Color.white);
      g2d.fillRect(0, 0, getWidth(),
           getHeight());

      g2d.setColor(Color.black);
      Calendar calendar = Calendar.getInstance();
      String str = (calendar.get(Calendar.YEAR)
        + 1)+ 
        " will be the year of Linux desktop";
      g2d.setFont(g2d.getFont().deriveFont(20.0f));
      FontMetrics fm = g2d.getFontMetrics();

      g2d.setRenderingHint(
          RenderingHints.KEY_TEXT_ANTIALIASING,
          RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      // The following is safe to call on Linux
      // as well since the rendering is done in
      // Java and not with native APIs.
      g2d.drawString(str, 
          (getWidth() - fm.stringWidth(str)) / 2,
          (getHeight() - fm.getHeight()) / 2);
      g2d.dispose();
    }
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new JFrame(
          "Linux on Desktop is here!!!");
        frame.add(new YearOfLinuxDesktopPanel());
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(
             JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}