Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.SwingUtilities;
- public class ImageTutorial {
- //Frame Vars:
- private JFrame frame = new JFrame();
- private JLabel player = new JLabel();
- //Movement Vars:
- private int moveSpeed = 15;
- public ImageTutorial() {
- //Frame Attributes:
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(null);
- frame.setVisible(true);
- frame.setResizable(false);
- frame.setSize(600, 600);
- frame.setTitle("Image Tutorial");
- //Player Attributes:
- player.setBounds(150, 150, 250, 150);
- player.setIcon(new ImageIcon("D:\\Documents\\Visual Studio 2015\\Projects\\CarCrashSim\\CarCrashSim\\redCar.png"));
- frame.add(player);
- SwingUtilities.updateComponentTreeUI(frame);
- //Key Events:
- frame.addKeyListener(new KeyListener(){
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- player.setLocation(player.getLocation().x + moveSpeed, player.getLocation().y);
- }
- else if(e.getKeyCode() == KeyEvent.VK_LEFT){
- player.setLocation(player.getLocation().x - moveSpeed, player.getLocation().y);
- }
- else if(e.getKeyCode() == KeyEvent.VK_UP){
- player.setLocation(player.getLocation().x, player.getLocation().y - moveSpeed);
- }
- else if(e.getKeyCode() == KeyEvent.VK_DOWN){
- player.setLocation(player.getLocation().x, player.getLocation().y + moveSpeed);
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- public void keyTyped(KeyEvent e) {
- }
- });
- }
- public static void main(String[] args){
- new ImageTutorial();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement