Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- public class Foo001 {
- public static void main(String[] args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- UI frame = null;
- try {
- frame = new UI();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(-1);
- }
- frame.setTitle("PacMan");
- frame.setResizable(false);
- frame.setSize(1200, 700);
- frame.setMinimumSize(new Dimension(1200, 700));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().add(frame.PacMan);
- frame.pack();
- frame.setVisible(true);
- }
- });
- }
- }
- class UI extends JFrame implements KeyListener {
- public PacMan PacMan;
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- int key = e.getKeyCode();
- if (key == KeyEvent.VK_RIGHT) {
- PacMan.moveRight();
- }
- if (key == KeyEvent.VK_LEFT) {
- PacMan.moveLeft();
- }
- if (key == KeyEvent.VK_UP) {
- PacMan.moveUp();
- }
- if (key == KeyEvent.VK_DOWN) {
- PacMan.moveDown();
- }
- }
- public UI() throws IOException {
- this.PacMan = new PacMan();
- addKeyListener(this);
- setFocusable(true);
- setFocusTraversalKeysEnabled(false);
- }
- }
- class PacMan extends JLabel {
- private static final String TIGER_LEFT = "textures/tigerLeft.png";
- private static final String TIGER_RIGHT = "textures/tigerRight.png";
- public int xCoords = 570;
- public int yCoords = 320;
- JLabel pacManImage = new JLabel();
- Icon tigerLeft;
- Icon tigerRight;
- // ImageIcon tigerLeft = new ImageIcon(
- // new ImageIcon("textures/tigerLeft.png").getImage().getScaledInstance(60, 40, Image.SCALE_DEFAULT));
- // ImageIcon tigerRight = new ImageIcon(
- // new ImageIcon("textures/tigerRight.png").getImage().getScaledInstance(60, 40, Image.SCALE_DEFAULT));
- public PacMan() throws IOException {
- File tigerLeftImageFile = new File(TIGER_LEFT);
- File tigerRightImageFile = new File(TIGER_RIGHT);
- BufferedImage tigerLeftImg = ImageIO.read(tigerLeftImageFile);
- BufferedImage tigerRightImg = ImageIO.read(tigerRightImageFile);
- tigerLeft = new ImageIcon(tigerLeftImg);
- tigerRight = new ImageIcon(tigerRightImg);
- }
- public void initialDraw() {
- pacManImage.setIcon(tigerRight);
- pacManImage.setBounds(xCoords, yCoords, 60, 40);
- }
- public void moveRight() {
- pacManImage.setIcon(tigerRight);
- System.out.println("here: " + tigerRight);
- xCoords = xCoords + 2;
- pacManImage.setBounds(xCoords, yCoords, 60, 40);
- }
- public void moveLeft() {
- pacManImage.setIcon(tigerLeft);
- xCoords = xCoords - 2;
- pacManImage.setBounds(xCoords, yCoords, 60, 40);
- }
- public void moveUp() {
- yCoords = yCoords - 2;
- pacManImage.setBounds(xCoords, yCoords, 60, 40);
- }
- public void moveDown() {
- yCoords = yCoords + 2;
- pacManImage.setBounds(xCoords, yCoords, 60, 40);
- }
- }
Add Comment
Please, Sign In to add comment