akosiraff

Paddle Ball Game Java

Mar 5th, 2013
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. Download: http://solutionzip.com/downloads/paddle-ball-game-java/
  2.  
  3. Week 7 Programming Assignment
  4. Documentation
  5. The objective of this part of the lab is to create documentation using Javadoc for the Paddle Ball Game project from week 5. The following documentation requirements must be met.
  6. 1. All commentary must use standard JavaDoc comments and tags.
  7. 2. For every class add a class level comment including the author (Melissa Kapolka) and the purpose of the class.
  8. 3. Where one class heavily depends on the use of an object from another class, provide a link to the other class using the @see tag.
  9. 4. For every method, add a method level comment which includes information on the purpose of the method, the method parameters, and the result returned by the method.
  10. 5. Use the javadoc command to generate HTML files for your project.
  11. 6. Use the browser to view your generated documentation to verify that all the required information for all the classes is included.
  12. Consult the document provided describing how to export JavaDoc comments from Eclipse.
  13. When your documentation is complete, turn in one of the following depending on what your instructor requires:
  14. 1. Print outs of all the documentation pages from the browser.
  15. 2. A Zip file containing all the documentation pages.
  16. Deployment
  17. Create a JAR file for the Paddle Ball Game project from week 5. The JAR file must contain all the class files needed to make the Paddle Ball Game work. Consult the document provided describing how to export a JAR file from Eclipse. To test if you have made your JAR file correctly, double click on your JAR file using Windows Explorer. The Paddle Ball Game should start running. If you are on Citrix, you will need to copy your game to a local computer before you can successfully execute it.
  18. When you have successfully executed your JAR file, turn in a copy of your JAR file to your instructor.
  19. BALL CLASS:
  20. import java.awt.*;
  21. import javax.swing.*;
  22. import java.awt.event.*;
  23. /**
  24. * Paddle Ball code using javadoc comments.
  25. * @author Melissa Kapolka
  26. * @verion 1.0
  27. */
  28. public class Ball extends JPanel {
  29. private int delay = 10;
  30. // Create a timer with delay 1000 ms
  31. private Timer timer = new Timer(delay, new TimerListener());
  32. private int x = 0; private int y = 0; // Current ball position
  33. private int radius = 5; // Ball radius
  34. private int dx = 2; // Increment on ball’s x-coordinate
  35. private int dy = 2; // Increment on ball’s y-coordinate
  36. public Ball() {
  37. timer.start();
  38. }
  39. private class TimerListener implements ActionListener {
  40. /** Handle the action event */
  41. public void actionPerformed(ActionEvent e) {
  42. repaint();
  43. }
  44. }
  45. protected void paintComponent(Graphics g) {
  46. super.paintComponent(g);
  47. g.setColor(Color.red);
  48. // Check boundaries
  49. if (x < radius) dx = Math.abs(dx);
  50. if (x > getWidth() – radius) dx = -Math.abs(dx);
  51. if (y < radius) dy = Math.abs(dy);
  52. if (y > getHeight() – radius) dy = -Math.abs(dy);
  53. // Adjust ball position
  54. x += dx;
  55. y += dy;
  56. g.fillOval(x – radius, y – radius, radius * 2, radius * 2);
  57. }
  58. }
  59. CONTROLLER CLASS:
  60. import javax.swing.*;
  61. import java.awt.*;
  62. public class Controller {
  63. private Ball ball = new Ball();
  64. public Controller() {
  65. //The controller has a ball, a paddle, and a display object
  66. Display display = new Display(500,500);
  67. display.setLayout(new BorderLayout());
  68. // Add ball to the display
  69. display.add(ball, BorderLayout.CENTER);
  70. int width = 500;
  71. int paddleWidth = 40;
  72. int paddleHeight = 12;
  73. Paddle paddle = new Paddle(width / 2 – paddleWidth, 0, paddleWidth, paddleHeight);
  74. display.add(paddle, BorderLayout.SOUTH);
  75. }
  76. }
  77. DISPLAY CLASS:
  78. import javax.swing.JFrame;
  79. public class Display extends JFrame{
  80. public Display(int h, int w) {
  81. this.setSize(h,w);
  82. this.setVisible(true);
  83. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  84. }
  85. }
  86. MAIN CLASS:
  87. public class Main {
  88. public static void main(String[] args) {
  89. new Controller();
  90. }
  91. }
  92. PADDLE CLASS:
  93. import java.awt.*;
  94. import java.awt.event.MouseEvent;
  95. import java.awt.event.MouseMotionListener;
  96. import javax.swing.JPanel;
  97. class Paddle extends JPanel implements MouseMotionListener {
  98. //The Paddle class has a length and a position
  99. private int xpos;
  100. private int ypos;
  101. int widtH;
  102. int heighT;
  103. public Paddle(int xpos, int ypos, int widtH, int heighT) {
  104. this.xpos = xpos;
  105. this.ypos = ypos;
  106. this.widtH = widtH;
  107. this.heighT = heighT;
  108. addMouseMotionListener(this);
  109. }
  110. //The draw method of the Paddle class takes a Graphics object parameter and draws a filled
  111. public void paint(Graphics g) {
  112. super.paintComponent(g);
  113. g.fillRect(xpos, ypos, widtH, heighT);
  114. }
  115. boolean collision(int ballxpos, int ballypos, int ballwidth, int ballheight) {
  116. if ((ballxpos + ballwidth < xpos || ballxpos > xpos + widtH)
  117. || (ballypos + ballheight < ypos || ballypos > ypos + heighT)) {
  118. return false;
  119. } else {
  120. return true;
  121. }
  122. }
  123. /*
  124. * To support the controller, the Paddle class
  125. must have methods which return the position of the top, bottom, left, and right edges of the this.
  126. */
  127. void setXpos(int x) {
  128. xpos = x;
  129. }
  130. void setWidth(int widtH) {
  131. this.widtH = widtH;
  132. }
  133. int getXpos() {
  134. return xpos;
  135. }
  136. int getWidtH() {
  137. return widtH;
  138. }
  139. public void mouseDragged(MouseEvent e) {
  140. // Get the new location and repaint the screen
  141. xpos = e.getX();
  142. repaint();
  143. }
  144. public void mouseMoved(MouseEvent e) {
  145. }
  146. }
  147.  
  148. Download: http://solutionzip.com/downloads/paddle-ball-game-java/
Advertisement
Add Comment
Please, Sign In to add comment