Setting Up the Interface for the Planet Applet

Now that we have a static HTML page to work with, we have to write the code for the applet itself. The first step is to design a simple interface for our applet. Let's think about how our planetary motion applet should look. There should be some textfields so that users can specify the values of certain parameters, "Start" and "Stop" buttons, and enough room to graphically represent our planet orbiting a star. Let's set up the interface first, and then we can worry about its functionality later.

Below is the Java code which creates the simple interface discussed above. A line by line explanation is given afterwards. (The numbers that appear in purple to the far left are line references to make the explanation easier, they are not part of the code.

 

1.  import java.awt.*;
2.	import java.applet.Applet;

3.	public class Planet extends Applet {	
4.		Panel dataPanel;	
5.		TextField inputxcoord, inputycoord, inputxvel, inputyvel;
		
6.		public void init() {
7.			dataPanel = new Panel(); 
8.			dataPanel.setLayout(new GridLayout (5,2,5,5));
9.			dataPanel.add(new Label("x-coordinate"));
10.			inputxcoord = new TextField("50");
11.			dataPanel.add(inputxcoord);
12.			dataPanel.add(new Label("y-coordinate"));
13.			inputycoord = new TextField("50");
14.			dataPanel.add(new Label("x-velocity"));
15.			inputxvel = new TextField("50");
16.			dataPanel.add(inputxvel);
17.			dataPanel.add(new Label("y-velocity"));
18.			inputyvel = new TextField("50");
19.			dataPanel.add(inputyvel);
20.			dataPanel.add(new Button("Start"));
21.			dataPanel.add(new Button("Pause"));

22.			setLayout(new BorderLayout(10,10));
23.			add("East",dataPanel);
24.		}

25.		public void paint (Graphics g) {
26.			g.setColor(Color.yellow);		//sun is yellow
27.			g.fillOval(50,50,8,8);			//paint sun
28.			g.setColor(Color.blue);		//planet is blue
29.			g.fillOval(50,100,4,4);			//paint planet
30.		}
31.	}

Now for the line by line explanation.

let's see what the interface looks like so far:


Send comments and suggestions to jdecarolis@clarku.edu
Last updated 10 October 1997