[ Contact ] [ Links ]

Exercice 1 : Some revisions about Java and rediscovery of Swing Hello World

You will improve the following very simple exemple by adding some asked modififations to it.

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

import javax.swing.*;
 

public class HelloWordSimple
{	public static void main(String[] args)
	{	HelloWordFrame frame = new HelloWordFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}


class HelloWordFrame extends JFrame 
{	public HelloWordFrame()
	{	setTitle("Hello World !! ");
		setSize(300,200);
		
		HelloPanel panel = new HelloPanel();
		Container contentPane = getContentPane();
		contentPane.add(panel);
	}
}

class HelloPanel extends JPanel
{	public void paintComponent(Graphics g)
	{	super.paintComponent(g);
		g.drawString("Hello again...", 20, 20);
	}
}

 

Understand how that program works.

Add some comments in the code

Debug it... because it is the more time consuming activity in computing.

import java.awt.*;

Is it possible to transform that program into an only two Class program (instead of the three HelloWordSimple, HelloWordFrame and HelloPanel)

Set the background of the frame to be cyan.

 

Transform the size of the window into in line parameters try it and then comme back to class fields.

Add some comments in the code

Make the programm guess the size of your computer screen, in order to have a window half that size, and place it in the middle of the screen.

Toolkit Class

Toolkit Class

Make the window not resizable.

Add some comments in the code

Get a pop up window that ask for you name, before showing up the "hello word" frame.

showInputDialog from JOptionPane...

String name = JOptionPane.showInputDialog("What is your Name");

Place a text in the HelloWord Panel such as the end of the text is at one third from the right and the bottom of the window. That text will be "Bonjour xxxx", where xxx is the name got from the inputDialog.

use a Font object, get a FontMetrics from it, and then get the stringWidth

Check that there are enough comments in your code an run javadoc to see the result
what is your comment ?

 

Put your class in a package and use it in another short program from another directory.

package, import, javac -d, CLASSPATH, package name : reversed internet domain name

Extra :

List of the fonts available in your java package.

Table of characteres entities.


See the "Links" link above to find out the sources of the proposed informations
Pascal Vuylsteker / eScience / Computer Science / ANU
Last modified: 2/4/2002
TOC - Print
Send your comments at :
<Hugh.Fisher@anu.edu.au>