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.
In this applet we use the GridLayout. The numbers in parentheses determine the setup of the components and refer to the number of rows (5), number of columns (2), vertical pixel spacing between components (5), and horizontal pixel spacing between components (5), respectively.
let's see what the interface looks like so far:
Send comments and suggestions to jdecarolis@clarku.edu
Last updated 10 October 1997