Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseMotionListener;
  7. import java.awt.image.BufferedImage;
  8.  
  9. import javax.swing.JOptionPane;
  10.  
  11. public class Ball extends Applet implements MouseMotionListener{
  12.  
  13. int speedx = 5;
  14. int speedy = 5;
  15. int ballx = 180;
  16. int bally = 490;
  17. int batx;
  18.  
  19. BufferedImage offScreenImage;
  20. Graphics offScreenGraphics;
  21.  
  22. public void init(){
  23. setSize(800,600);
  24. offScreenImage = new BufferedImage(getWidth(),
  25. getHeight(),BufferedImage.TYPE_INT_RGB);
  26. offScreenGraphics = offScreenImage.getGraphics();
  27. setBackground(Color.BLACK);
  28. addMouseMotionListener(this);
  29. }
  30. public void paint(Graphics g){
  31. render(offScreenGraphics);
  32. g.drawImage(offScreenImage, 0, 0, null);
  33.  
  34. updateState();
  35. repaint();
  36.  
  37. try{
  38. Thread.sleep(30);
  39. }catch(InterruptedException e){
  40. }
  41. }
  42. private void render (Graphics g){
  43. g.clearRect(0, 0, getWidth(), getHeight());
  44. g.setColor(Color.WHITE);
  45. g.fillOval(ballx, bally, 20, 20);
  46.  
  47. g.setColor(Color.GREEN);
  48. g.fillRect(batx, 580, 70, 25);
  49.  
  50. g.setColor(Color.RED);
  51. g.drawString("x= " + ballx, 10, 10);
  52.  
  53. g.setColor(Color.RED);
  54. g.drawString("x= " + bally, 10, 30);
  55.  
  56. }
  57. private void updateState(){
  58.  
  59. ballx= ballx+speedx;
  60. bally= bally+speedy;
  61.  
  62. if(ballx >= batx && ballx <=batx+140 && bally ==580){
  63. speedy = -speedy;
  64. }
  65. if(ballx >=batx && ballx<=batx+70 && bally>580 && bally <=605){
  66. bally -=bally-580+10;
  67. }
  68. if(ballx <= 0 && bally <600){
  69. speedx = -speedx;
  70. }
  71. if (ballx >=800 && bally < 600){
  72. speedx =-speedx;
  73. }
  74. if (ballx >0 && bally <800 && bally ==0){
  75. speedy = -speedy;
  76. }
  77. if (ballx > 0 && ballx >= 0 && bally == 610){
  78. JOptionPane.showMessageDialog(null,"Sorry GameOver");
  79. ballx = 180;
  80. bally = 400;
  81. }
  82. }
  83. public void mouseMoved(MouseEvent e){
  84. batx =e.getX();
  85. }
  86. public void mouseDragged(MouseEvent e){}
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement