Click here to skip to the explanation.
The numbers to the far left are only for reference, the lines labeled with purple line numbers has already been explained as part of the interface, and the lines labeled with green line numbers are the extra code needed to make the interface functional.
1. import java.awt.*;
2. import java.applet.Applet;
3. public class Planet extends Applet implements Runnable{
4. Thread displayer; //declare the thread
5. Panel dataPanel;
6. Button controlButton;
7. boolean doSuspend;
8. TextField inputxcoord, inputycoord, inputxvel, inputyvel; //declare variables
9. int centerx, centery;
10. double[] pos = new double[2]; //declare arrays for position, velocity, and acceleration
11. double[] vel = new double[2];
12. double[] accel = new double[2];
13. double dt = 0.01; //declare and initialize some variables
14. double GM = 4pi2;
15. public void init() {
16. setBackground(Color.white);
17. dataPanel = new Panel();
18. dataPanel.setLayout(new GridLayout (5,2,5,5));
19. dataPanel.add(new Label("x-coordinate"));
20. inputxcoord = new TextField("50");
21. dataPanel.add(inputxcoord);
22. dataPanel.add(new Label("y-coordinate"));
23. inputycoord = new TextField("50");
24. dataPanel.add(new Label("x-velocity"));
25. inputxvel = new TextField("50");
26. dataPanel.add(inputxvel);
27. dataPanel.add(new Label("y-velocity"));
28. inputyvel = new TextField("50");
29. dataPanel.add(inputyvel);
30. dataPanel.add(new Button("Start"));
31. setLayout(new BorderLayout(10,10));
32. add("East",dataPanel);
33. setBackground(Color.black);
34. }
35. public void paint (Graphics g) {
36. g.setColor(Color.yellow); //sun is yellow
37. g.fillOval(50,50,8,8); //paint sun
38. g.setColor(Color.green); //planet is green
39. g.fillOval(50,100,4,4); //paint planet
40. }
41. public void update (Graphics g) { //update the picture
42. g.setColor(Color.blue); //planet is blue
43. g.fillOval(centerx+(int)pos[0],centery+(int)pos[1],4,4); //paint planet
44. inputxcoord.setText(Double.toString(pos[0]));
45. inputycoord.setText(Double.toString(pos[1]));
46. inputxvel.setText(Double.toString(vel[0]));
47. inputyvel.setText(Double.toString(vel[1]));
48. }
49. public void start() { //start the thread running
50. if (displayer == null) {
51. displayer = new Thread(this);
52. }
52. }
53. public void stop() { //stop the thread
54. if (displayer != null) {
55. displayer.stop();
56. }
57. }
58. public void run() { //run the Euler() method
59. while (true) {
60. Euler();
61. if (doSuspend) {
62. displayer.suspend();
63. doSuspend = false;
64. }
65. try {displayer.sleep(10);}
66. catch (InterruptedException e){};
67. }
68. }
69. void resume(){ //resume the applet after being paused
70. try {
71. pos[0] = Integer.parseInt(inputxcoord.getText());}
72. catch (NumberFormatException e) {
73. inputxcoord.setText(Double.toString(pos[0])); }
74. try {
75. pos[1] = Integer.parseInt(inputycoord.getText());}
76. catch (NumberFormatException e) {
77. inputycoord.setText(Double.toString(pos[1])); }
78. try {
79. vel[0] = Integer.parseInt(inputxvel.getText());}
80. catch (NumberFormatException e) {
81. inputxvel.setText(Double.toString(vel[0])); }
82. try {
83. vel[1] = Integer.parseInt(inputyvel.getText());}
84. catch (NumberFormatException e) {
85. inputyvel.setText(Double.toString(vel[1])); }
86. displayer.resume();
87. }
88. public boolean action (Event evt, Object arg) { //event handling
89. if (evt.target instanceof Button) {
90. System.out.println(" Button "+arg+" pressed");
91. if ("Start".equals(arg)) {
92. controlButton.setLabel("Pause");
93. displayer.start();
94. } else if ("Pause".equals(arg)) {
95. controlButton.setLabel("Resume");
96. doSuspend = true;
97. } else if ("Resume".equals(arg)) {
98. controlButton.setLabel("Pause");
99. resume();
100. }
101. return true;
102. }
103. if (evt.target instanceof TextField) {
104. System.out.println(" TextField "+evt.target+" arg="+arg);
105. return true;
106. }
107. return false;
108. }
109. public void Euler() { //simple physics of planetary motion
110. double r,r2,r3;
111. r2 = pos[0]*pos[0] + pos[1]*pos[1]; //r^2 = x^2 + y^2
112. r3 = r2*Math.sqrt(r2); //r = sqrt(x^2 + y^2)
113. for (int i=0;i<=1;i++) {
114. accel[i] = -G*M*pos[i]/r3;
115. vel[i] = vel[i] + accel[i]*dt;
116. pos[i] = pos[i] + vel[i]*dt;
117. }
118. }
119. }
Here's the explanation:
If the user pushes the Pause button, the label is changed to "Resume" and the boolean variable (doSuspend) is given the value true. This in turn, causes the thread's execution to be suspended in the suspend() method (a predefined thread method), which is called in the run() method (Line 62).
If the Resume button is pressed, the button label is changed back to "Pause" and the thread method resume() is called.
Send comments and suggestions to jdecarolis@clarku.edu
Last updated 10 October 1997