Advertisement
Guest User

Untitled

a guest
Oct 4th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import java.awt.Color;
  2. import javax.swing.JFrame;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.awt.geom.Rectangle2D;
  8. import javax.swing.JPanel;
  9. import java.awt.geom.Ellipse2D;
  10.  
  11. public class Main extends JFrame {
  12. private static final long serialVersionUID = 1L;
  13.  
  14. PadClass p = new PadClass();
  15. BallClass b = new BallClass();
  16.  
  17. public Main() {
  18. setSize(300, 200);
  19. setTitle("First bouncy game");
  20. setResizable(false);
  21. getContentPane().setBackground(Color.orange);
  22. //add(b);
  23. add(new PadClass());
  24. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25. setVisible(true);
  26. }
  27.  
  28. public static void main(String args[]) {
  29. new Main();
  30. }
  31. }
  32.  
  33. public class PadClass extends JPanel implements KeyListener {
  34. private static final long serialVersionUID = 1L;
  35.  
  36. int x = 130, y = 165, velx = 3;
  37.  
  38. public PadClass() {
  39. addKeyListener(this);
  40. setFocusable(true);
  41.  
  42. }
  43.  
  44. public void paintComponent(Graphics g) {
  45. super.paintComponent(g);
  46. Graphics2D g2 = (Graphics2D) g;
  47. g2.fill(new Rectangle2D.Double(x, y, 40, 10));
  48. repaint();
  49. }
  50.  
  51. public void right() {
  52. x += velx;
  53. }
  54.  
  55. public void left() {
  56. x -= velx;
  57. }
  58.  
  59. public void keyPressed(KeyEvent e) {
  60. int key = e.getKeyCode();
  61. if (key == KeyEvent.VK_RIGHT) {
  62. right();
  63. }
  64. if (key == KeyEvent.VK_LEFT) {
  65. left();
  66. }
  67. }
  68.  
  69. public void keyReleased(KeyEvent e) {}
  70.  
  71. public void keyTyped(KeyEvent e) {}
  72.  
  73. }
  74.  
  75. public class BallClass extends JPanel{
  76. private static final long serialVersionUID = 1L;
  77. int x = 130;
  78. int y = 0;
  79.  
  80. public BallClass (){
  81. }
  82.  
  83. public void paintComponent(Graphics g) {
  84. super.paintComponent(g);
  85. Graphics2D g2 = (Graphics2D) g;
  86. g2.fill(new Ellipse2D.Double(x, y, 10, 10));
  87. repaint();
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement