Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class MovingPractice extends JApplet implements KeyListener
- {
- Point AppletDimensions = new Point(800, 600),
- SquarePosition = new Point(60, 60),
- SquareDimensions = new Point(100, 100);
- int SquareSpeed = 20,
- ScaleSpeed = 3;
- public void init()
- {
- setSize((int)AppletDimensions.getX(), (int)AppletDimensions.getY());
- setFocusable(true);
- addKeyListener(this);
- }
- public void paint(Graphics g)
- {
- g.clearRect(0, 0, getWidth(), getHeight());
- g.setColor(Color.red);
- /* If you want to customize your color, do this:
- g.setColor(new Color(200, 200, 200));
- That gives you the color light grey */
- g.fillRect((int)SquarePosition.getX(), (int)SquarePosition.getY(), (int)SquareDimensions.getX(), (int)SquareDimensions.getY());
- }
- public void keyTyped(KeyEvent e){}
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode() == KeyEvent.VK_UP)
- {
- SquarePosition = new Point((int)SquarePosition.getX(), (int)SquarePosition.getY() - SquareSpeed);
- }
- if(e.getKeyCode() == KeyEvent.VK_DOWN)
- {
- SquarePosition = new Point((int)SquarePosition.getX(), (int)SquarePosition.getY() + SquareSpeed);
- }
- if(e.getKeyCode() == KeyEvent.VK_RIGHT)
- {
- SquarePosition = new Point((int)SquarePosition.getX() + SquareSpeed, (int)SquarePosition.getY());
- }
- if(e.getKeyCode() == KeyEvent.VK_LEFT)
- {
- SquarePosition = new Point((int)SquarePosition.getX() - SquareSpeed, (int)SquarePosition.getY());
- }
- if(e.getKeyCode() == KeyEvent.VK_A)
- {
- SquareDimensions = new Point((int)SquareDimensions.getX() + ScaleSpeed, (int)SquareDimensions.getY() + ScaleSpeed);
- }
- if(e.getKeyCode() == KeyEvent.VK_S)
- {
- SquareDimensions = new Point((int)SquareDimensions.getX() - ScaleSpeed, (int)SquareDimensions.getY() - ScaleSpeed);
- }
- repaint();
- }
- public void keyReleased(KeyEvent e){}
- }
Advertisement
Add Comment
Please, Sign In to add comment