Advertisement
icorrelate

GM GamePanel.java

Dec 12th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.RenderingHints;
  5.  
  6. import javax.swing.JPanel;
  7.  
  8. public class GamePanel extends JPanel{
  9.  
  10. //First Ball Horizontal Movement with Collision Detection
  11. int fx = 0;
  12. int fy = 30;
  13. int firstXBallSpeed = 1;
  14. void moveFBall() {
  15. fx = fx + firstXBallSpeed;
  16. if(fx > 80 || fx < 1){
  17. firstXBallSpeed *= -1;
  18. }
  19. }
  20.  
  21. //second ball Diagonal
  22. int sx = 100;
  23. int sy = 0;
  24. int secYBallSpeed = 1;
  25. int secXBallSpeed = 1;
  26. void moveSBall(){
  27. sy = sy + secYBallSpeed;
  28. sx = sx + secYBallSpeed;
  29.  
  30. if(sy > 80 || sy < 0){
  31. secYBallSpeed *= -1;
  32. }
  33. }
  34.  
  35. //second third Diagonal
  36. int tx = 230;
  37. int ty = 0;
  38. int thYBallSpeed = 1;
  39. void movetBall(){
  40. ty = ty + thYBallSpeed;
  41. if(ty > 80 || ty < 1){
  42. thYBallSpeed *= -1;
  43. }
  44. }
  45.  
  46. double MAX_X = 30;
  47. double MAX_Y = 30;
  48. double angle = 0;
  49. int frballx = 0;
  50. int frbally = 0;
  51.  
  52. void movefrBall(){
  53.  
  54. //making angle lower will reduce its speed
  55. angle += 0.1;
  56.  
  57. frballx = (int) (Math.cos(angle) * MAX_X);
  58. frbally = (int) (Math.sin(angle) * MAX_Y);
  59.  
  60. //move the ball to desired location
  61. frballx += 330;
  62. frbally += 40;
  63.  
  64.  
  65. }
  66.  
  67.  
  68. //rectangular movement
  69.  
  70. int fyy = 0;
  71. int fxx = 400;
  72. int fyyspeed = 1;
  73. int fxxspeed = 1;
  74.  
  75. void moveftBall(){
  76. if(fyy == 0){
  77. fxx += fxxspeed;
  78. }else if (fyy == 80){
  79. fyyspeed = 0;
  80. fxxspeed = 1;
  81. fxx -= fxxspeed;
  82. }
  83.  
  84. if(fxx == 480){
  85. fxxspeed = 0;
  86. fyy += fyyspeed;
  87. }
  88.  
  89. if(fxx == 400){
  90. fyyspeed = 1;
  91. fyy -= fyyspeed;
  92. }
  93.  
  94.  
  95.  
  96. }
  97.  
  98. @Override
  99. public void paint(Graphics g) {
  100. super.paint(g);
  101. Graphics2D g2d = (Graphics2D) g;
  102. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  103. RenderingHints.VALUE_ANTIALIAS_ON);
  104.  
  105.  
  106. for(int i = 0; i < 5 ; i++){
  107. g2d.drawRect(i * 100, 0, 100, 100);
  108. }
  109.  
  110. g2d.drawString("Horizontal", 20, 120);
  111. g2d.drawString("Diagonal", 120, 120);
  112. g2d.drawString("Vertical", 220, 120);
  113. g2d.drawString("Circular", 320, 120);
  114. g2d.drawString("Rectangular", 410, 120);
  115.  
  116. g2d.setColor(Color.RED);
  117.  
  118. //Draw 1st ball;
  119. g2d.fillOval(fx, fy, 20, 20);
  120.  
  121.  
  122. //Draw 2nd ball;
  123. g2d.fillOval(sx, sy, 20, 20);
  124.  
  125. //Draw 3rd ball;
  126. g2d.fillOval(tx, ty, 20, 20);
  127.  
  128. //Draw 4th ball;
  129. g2d.fillOval(frballx, frbally, 20, 20);
  130.  
  131. //Draw 5th Ball
  132. g2d.fillOval(fxx, fyy, 20, 20);
  133.  
  134.  
  135.  
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement