• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why a negative offset of the y coordinate is required in the program as in line 62

 
Naya Hegde
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
2. import java.awt.image.*;
3. import java.awt.event.*;
4. import javax.swing.*;
5. import java.net.*;
6.
7. /**
8. An applet that shows a rotating globe.
9. */
10. public class Animation extends JApplet
11. {
12. public void init()
13. {
14. addMouseListener(new MouseAdapter()
15. {
16. public void mousePressed(MouseEvent evt)
17. {
18. if (runner == null)
19. start();
20. else
21. stop();
22. }
23. });
24.
25. try
26. {
27. String imageName = getParameter("imagename");
28. imageCount = 1;
29. String param = getParameter("imagecount");
30. if (param != null)
31. imageCount = Integer.parseInt(param);
32. current = 0;
33. image = null;
34. loadImage(new URL(getDocumentBase(), imageName));
35. }
36. catch (Exception e)
37. {
38. showStatus("Error: " + e);
39. }
40. }
41.
42. /**
43. Loads an image.
44. @param url the URL of the image file
45. */
46. public void loadImage(URL url)
47. throws InterruptedException
48. // thrown by MediaTracker.waitFor
49. {
50. image = getImage(url);
51. MediaTracker tracker = new MediaTracker(this);
52. tracker.addImage(image, 0);
53. tracker.waitForID(0);
54. imageWidth = image.getWidth(null);
55. imageHeight = image.getHeight(null);
56. resize(imageWidth, imageHeight / imageCount);
57. }
58.
59. public void paint(Graphics g)
60. {
61. if (image == null) return;
62. g.drawImage(image, 0, - (imageHeight / imageCount)
63. * current, null);
64. }
65.
66. public void start()
67. {
68. runner = new
69. Thread()
70. {
71. public void run()
72. {
73. try
74. {
75. while (!Thread.interrupted())
76. {
77. repaint();
78. current = (current + 1) % imageCount;
79. Thread.sleep(200);
80. }
81. }
82. catch(InterruptedException e) {}
83. }
84. };
85. runner.start();
86. showStatus("Click to stop");
87. }
88.
89. public void stop()
90. {
91. runner.interrupt();
92. runner = null;
93. showStatus("Click to restart");
94. }
95.
96. private Image image;
97. private int current;
98. private int imageCount;
99. private int imageWidth;
100. private int imageHeight;
101. private Thread runner;
102. }
 
Naya Hegde
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Welcome to the Ranch.

Here are a few tips to help you get the help you need:

Please take the time to choose an appropriate forum for your posts. This forum is for questions on Threads. For more information, please click this link ⇒ CarefullyChooseOneForum. This post has been moved to a more appropriate forum.

Also, Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

Code tags will automatically number your code lines so you do not have to.


Hello Sir,

I would like to know where I can ask logical questions related to java programming
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!