Mental Jetsam

By Peter Finch

Archive for July 11th, 2012

Java Hello World

Posted by pcfinch on July 11, 2012

This is a standard “hello world” program that I use when testing a java installation to make sure everything works.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class MyFrame extends JFrame {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyFrame mf = new MyFrame() ;
	}
	
	protected MyFrame() {
		this.setTitle("MyFrame") ;
		this.setSize(800, 600) ;
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
		this.getContentPane().add(new JPanel() {
			public void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D)g ;
				g2.setColor(Color.blue) ;				
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.drawLine(0, 0, this.getWidth(),  this.getHeight()) ;
				g2.drawOval(0, 0, this.getWidth(),  this.getHeight()) ;
				g2.drawString("Hello World", this.getWidth() /2 , this.getHeight() /2) ;
			}			
		}) ;
		this.setVisible(true) ;
	}

}

Posted in Uncategorized | Leave a Comment »