Crenox

Java Moving Source Code: Rectangle

Apr 23rd, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class MovingPractice extends JApplet implements KeyListener
  6. {
  7. Point AppletDimensions = new Point(800, 600),
  8. SquarePosition = new Point(60, 60),
  9. SquareDimensions = new Point(100, 100);
  10.  
  11. int SquareSpeed = 20,
  12. ScaleSpeed = 3;
  13.  
  14. public void init()
  15. {
  16. setSize((int)AppletDimensions.getX(), (int)AppletDimensions.getY());
  17.  
  18. setFocusable(true);
  19. addKeyListener(this);
  20. }
  21.  
  22. public void paint(Graphics g)
  23. {
  24. g.clearRect(0, 0, getWidth(), getHeight());
  25. g.setColor(Color.red);
  26. /* If you want to customize your color, do this:
  27. g.setColor(new Color(200, 200, 200));
  28. That gives you the color light grey */
  29. g.fillRect((int)SquarePosition.getX(), (int)SquarePosition.getY(), (int)SquareDimensions.getX(), (int)SquareDimensions.getY());
  30. }
  31.  
  32. public void keyTyped(KeyEvent e){}
  33.  
  34. public void keyPressed(KeyEvent e)
  35. {
  36. if(e.getKeyCode() == KeyEvent.VK_UP)
  37. {
  38. SquarePosition = new Point((int)SquarePosition.getX(), (int)SquarePosition.getY() - SquareSpeed);
  39. }
  40.  
  41. if(e.getKeyCode() == KeyEvent.VK_DOWN)
  42. {
  43. SquarePosition = new Point((int)SquarePosition.getX(), (int)SquarePosition.getY() + SquareSpeed);
  44. }
  45.  
  46. if(e.getKeyCode() == KeyEvent.VK_RIGHT)
  47. {
  48. SquarePosition = new Point((int)SquarePosition.getX() + SquareSpeed, (int)SquarePosition.getY());
  49. }
  50.  
  51. if(e.getKeyCode() == KeyEvent.VK_LEFT)
  52. {
  53. SquarePosition = new Point((int)SquarePosition.getX() - SquareSpeed, (int)SquarePosition.getY());
  54. }
  55.  
  56. if(e.getKeyCode() == KeyEvent.VK_A)
  57. {
  58. SquareDimensions = new Point((int)SquareDimensions.getX() + ScaleSpeed, (int)SquareDimensions.getY() + ScaleSpeed);
  59. }
  60.  
  61. if(e.getKeyCode() == KeyEvent.VK_S)
  62. {
  63. SquareDimensions = new Point((int)SquareDimensions.getX() - ScaleSpeed, (int)SquareDimensions.getY() - ScaleSpeed);
  64. }
  65.  
  66. repaint();
  67. }
  68.  
  69. public void keyReleased(KeyEvent e){}
  70. }
Advertisement
Add Comment
Please, Sign In to add comment