Mental Jetsam

By Peter Finch

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) ;
	}

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: