A First Example with comments

Source : HelloWordSimple.java

All Java source code files should end with a .java extension. When a Java program is compiled one or more .class files are generated. The number of .class files generated by a compile depends on how many objects are defined in the source file.

Let's test it first


/** Simple Java 2D Example
 *
 *	@version 0.9  12/07/2001
 *	@author Pascal Vuylsteker
 */
 

// to tell to the compiler where to look for class definition
// class that are not in the default package nor in java.lang
import javax.swing.*;

// that was missing...
import java.awt.*;


//Since all Java code must be contained within an object, 
// an object called HelloWordSimple is defined in this file.

// "There should be only one"... public class per file

public class HelloWordSimple
{	
//	in order to run a program, the public class should contain 
//  a main method which is static
	public static void main(String[] args)
	{	HelloWordFrame frame = new HelloWordFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}

// You don't even need to know any of the details of how a class works, 
//  beyond the interfaces to methods, in order to modify the original code. This 
//  is a simple example of the power of object-oriented programming. 
class HelloWordFrame extends JFrame 
{	public HelloWordFrame()
	{	setTitle("Hello World !! ");
		setSize(300,200);
		
		HelloPanel panel = new HelloPanel();
		Container contentPane = getContentPane();
		contentPane.add(panel);
	}
}

// JPanel has to be extented : the main method contain all
// the work.
class HelloPanel extends JPanel
{	public void paintComponent(Graphics g)
	{	super.paintComponent(g);
		g.drawString("Hello again...", 20, 20);
	}
}
 

NB : what is wrong with HelloWordSimple2.java ?

It is not object oriented programming...

See the last semester summary about graphics