fubarable

example

Oct 30th, 2020
1,971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6.  
  7. import javax.imageio.ImageIO;
  8. import javax.swing.*;
  9.  
  10. public class Foo001 {
  11.     public static void main(String[] args) {
  12.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  13.             public void run() {
  14.  
  15.                
  16.                 UI frame = null;
  17.                 try {
  18.                     frame = new UI();
  19.                 } catch (IOException e) {
  20.                     e.printStackTrace();
  21.                     System.exit(-1);
  22.                 }
  23.                 frame.setTitle("PacMan");
  24.                 frame.setResizable(false);
  25.                 frame.setSize(1200, 700);
  26.                 frame.setMinimumSize(new Dimension(1200, 700));
  27.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.                 frame.getContentPane().add(frame.PacMan);
  29.                 frame.pack();
  30.                 frame.setVisible(true);
  31.             }
  32.         });
  33.     }
  34. }
  35.  
  36. class UI extends JFrame implements KeyListener {
  37.     public PacMan PacMan;
  38.  
  39.     @Override
  40.     public void keyTyped(KeyEvent e) {
  41.  
  42.     }
  43.  
  44.     @Override
  45.     public void keyPressed(KeyEvent e) {
  46.  
  47.     }
  48.  
  49.     @Override
  50.     public void keyReleased(KeyEvent e) {
  51.         int key = e.getKeyCode();
  52.         if (key == KeyEvent.VK_RIGHT) {
  53.             PacMan.moveRight();
  54.         }
  55.         if (key == KeyEvent.VK_LEFT) {
  56.             PacMan.moveLeft();
  57.         }
  58.         if (key == KeyEvent.VK_UP) {
  59.             PacMan.moveUp();
  60.         }
  61.         if (key == KeyEvent.VK_DOWN) {
  62.             PacMan.moveDown();
  63.         }
  64.     }
  65.  
  66.     public UI() throws IOException {
  67.         this.PacMan = new PacMan();
  68.         addKeyListener(this);
  69.         setFocusable(true);
  70.         setFocusTraversalKeysEnabled(false);
  71.  
  72.     }
  73. }
  74.  
  75. class PacMan extends JLabel {
  76.     private static final String TIGER_LEFT = "textures/tigerLeft.png";
  77.     private static final String TIGER_RIGHT = "textures/tigerRight.png";
  78.     public int xCoords = 570;
  79.     public int yCoords = 320;
  80.     JLabel pacManImage = new JLabel();
  81.     Icon tigerLeft;
  82.     Icon tigerRight;
  83. //  ImageIcon tigerLeft = new ImageIcon(
  84. //          new ImageIcon("textures/tigerLeft.png").getImage().getScaledInstance(60, 40, Image.SCALE_DEFAULT));
  85. //  ImageIcon tigerRight = new ImageIcon(
  86. //          new ImageIcon("textures/tigerRight.png").getImage().getScaledInstance(60, 40, Image.SCALE_DEFAULT));
  87.    
  88.     public PacMan() throws IOException {
  89.         File tigerLeftImageFile = new File(TIGER_LEFT);
  90.         File tigerRightImageFile = new File(TIGER_RIGHT);
  91.        
  92.         BufferedImage tigerLeftImg = ImageIO.read(tigerLeftImageFile);
  93.         BufferedImage tigerRightImg = ImageIO.read(tigerRightImageFile);
  94.        
  95.         tigerLeft = new ImageIcon(tigerLeftImg);
  96.         tigerRight = new ImageIcon(tigerRightImg);
  97.     }
  98.  
  99.     public void initialDraw() {
  100.         pacManImage.setIcon(tigerRight);
  101.         pacManImage.setBounds(xCoords, yCoords, 60, 40);
  102.     }
  103.  
  104.     public void moveRight() {
  105.         pacManImage.setIcon(tigerRight);
  106.        
  107.         System.out.println("here: " + tigerRight);
  108.         xCoords = xCoords + 2;
  109.         pacManImage.setBounds(xCoords, yCoords, 60, 40);
  110.     }
  111.  
  112.     public void moveLeft() {
  113.         pacManImage.setIcon(tigerLeft);
  114.         xCoords = xCoords - 2;
  115.         pacManImage.setBounds(xCoords, yCoords, 60, 40);
  116.     }
  117.  
  118.     public void moveUp() {
  119.         yCoords = yCoords - 2;
  120.         pacManImage.setBounds(xCoords, yCoords, 60, 40);
  121.     }
  122.  
  123.     public void moveDown() {
  124.         yCoords = yCoords + 2;
  125.         pacManImage.setBounds(xCoords, yCoords, 60, 40);
  126.     }
  127. }
Add Comment
Please, Sign In to add comment