Exercise 1 : Some revisions on Java and rediscovery of Swing 'Hello World'
You will improve the following very simple example by making some modifications.
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.*;
Transform that program into a two Class program (instead
of three - HelloWordSimple, HelloWordFrame and HelloPanel)
Set the background of the frame to be cyan.
Set the size of the window by using 'command line' parameters. Try it and
then come back to class fields.
Add some comments in the code
Make the program find the size of your computer screen, in order to make
the frame half that size, and centre it in the middle of the screen.
Tip : Explore the Toolkit Class
Make the window not resizable.
Add some comments in the code
Get a pop up window that asks for your name, before showing up the "hello
world" frame.
showInputDialog from JOptionPane...
String name = JOptionPane.showInputDialog("What is
your Name");
Place text in the HelloWord Panel such that the end of the text is one
third of the distance from the right and the bottom of the window. That text
will be "Bonjour xxxx", where xxxx is the name entered in the inputDialog.
use a Font object, get a FontMetrics from it, and then
get the stringWidth or...
use a Font object, a Graphics2D object (from the Graphics one), and a FontRenderContext
and 'getStringBounds' from the font
Check that there are enough comments in your code and run javadoc to see
the result.
Think about using some of javadoc options in order
to get more information (-private, -author, etc)
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 character entities.