Crenox

2 Squares in a Code

May 3rd, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Moving extends JApplet implements KeyListener
  6. {
  7. Point AppletD = new Point(800, 800),
  8. SquareP = new Point(400, 400),
  9. SquareD = new Point(100, 100),
  10. SquarePosition = new Point(100, 100),
  11. SquareDimensions = new Point(100, 100);
  12.  
  13. int SquareSpeed = 25,
  14. ScaleSpeed = 25,
  15. SquareS = 25,
  16. ScaleS = 25;
  17.  
  18. public void init()
  19. {
  20. setSize((int)AppletD.getX(), (int)AppletD.getY());
  21.  
  22. setFocusable(true);
  23. addKeyListener(this);
  24. }
  25.  
  26. public void paint(Graphics g)
  27. {
  28. g.clearRect(0, 0, getWidth(), getHeight());
  29. g.setColor(Color.pink);
  30. g.fillRect((int)SquareP.getX(), (int)SquareP.getY(), (int)SquareD.getX(), (int)SquareD.getY());
  31. // 2nd square.
  32. g.setColor(Color.red);
  33. g.fillRect((int)SquarePosition.getX(), (int)SquarePosition.getY(), (int)SquareDimensions.getX(), (int)SquareDimensions.getY());
  34. }
  35.  
  36. public void keyTyped(KeyEvent e){}
  37.  
  38. public void keyPressed(KeyEvent e)
  39. {
  40. if(e.getKeyCode() == KeyEvent.VK_UP)
  41. {
  42. SquareP = new Point ((int)SquareP.getX(), (int)SquareP.getY() - SquareSpeed);
  43. }
  44.  
  45. if(e.getKeyCode() == KeyEvent.VK_DOWN)
  46. {
  47. SquareP = new Point ((int)SquareP.getX(), (int)SquareP.getY() + SquareSpeed);
  48. }
  49.  
  50. if(e.getKeyCode() == KeyEvent.VK_RIGHT)
  51. {
  52. SquareP = new Point ((int)SquareP.getX() + SquareSpeed, (int)SquareP.getY());
  53. }
  54.  
  55. if(e.getKeyCode() == KeyEvent.VK_LEFT)
  56. {
  57. SquareP = new Point ((int)SquareP.getX() - SquareSpeed, (int)SquareP.getY());
  58. }
  59.  
  60. if(e.getKeyCode() == KeyEvent.VK_X)
  61. {
  62. SquareD = new Point ((int)SquareD.getX() - ScaleSpeed, (int)SquareD.getY() - ScaleSpeed);
  63. }
  64.  
  65. if(e.getKeyCode() == KeyEvent.VK_Z)
  66. {
  67. SquareD = new Point ((int)SquareD.getX() + ScaleSpeed, (int)SquareD.getY() + ScaleSpeed);
  68. }
  69.  
  70. if(e.getKeyCode() == KeyEvent.VK_W)
  71. {
  72. SquarePosition = new Point ((int)SquarePosition.getX(), (int)SquarePosition.getY() - SquareS);
  73. }
  74.  
  75. if(e.getKeyCode() == KeyEvent.VK_S)
  76. {
  77. SquarePosition = new Point ((int)SquarePosition.getX(), (int)SquarePosition.getY() + SquareS);
  78. }
  79.  
  80. if(e.getKeyCode() == KeyEvent.VK_D)
  81. {
  82. SquarePosition = new Point ((int)SquarePosition.getX() + SquareS, (int)SquarePosition.getY());
  83. }
  84.  
  85. if(e.getKeyCode() == KeyEvent.VK_A)
  86. {
  87. SquarePosition = new Point ((int)SquarePosition.getX() - SquareS, (int)SquarePosition.getY());
  88. }
  89.  
  90. if(e.getKeyCode() == KeyEvent.VK_O)
  91. {
  92. SquareDimensions = new Point ((int)SquareDimensions.getX() - ScaleS, (int)SquareDimensions.getY() - ScaleS);
  93. }
  94.  
  95. if(e.getKeyCode() == KeyEvent.VK_P)
  96. {
  97. SquareDimensions = new Point ((int)SquareDimensions.getX() + ScaleS, (int)SquareDimensions.getY() + ScaleS);
  98. }
  99.  
  100. repaint();
  101. }
  102.  
  103. public void keyReleased(KeyEvent e){}
  104. }
Advertisement
Add Comment
Please, Sign In to add comment