Advertisement
Guest User

Program Code

a guest
Aug 28th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. --
  2. TestGame.java
  3. --
  4. import javax.swing.JFrame;
  5.  
  6. public class TestGame extends JFrame {
  7.  
  8.     public TestGame() {
  9.  
  10.         add(new Board());
  11.  
  12.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.         setSize(400, 300);
  14.         setLocationRelativeTo(null);
  15.         setTitle("Project Obcasus");
  16.         setResizable(false);
  17.         setVisible(true);
  18.     }
  19.  
  20.     public static void main(String[] args) {
  21.         new TestGame();
  22.     }
  23. }
  24.  
  25. --
  26. Board.java
  27. --
  28. import java.awt.Color;
  29. import java.awt.Graphics;
  30. import java.awt.Graphics2D;
  31. import java.awt.Toolkit;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.event.KeyAdapter;
  35. import java.awt.event.KeyEvent;
  36.  
  37. import javax.swing.JPanel;
  38. import javax.swing.Timer;
  39.  
  40.  
  41. public class Board extends JPanel implements ActionListener {
  42.  
  43.     private Timer timer;
  44.     private Emilia emilia;
  45.  
  46.     public Board() {
  47.  
  48.         addKeyListener(new TAdapter());
  49.         setFocusable(true);
  50.         setBackground(Color.BLACK);
  51.         setDoubleBuffered(true);
  52.  
  53.         emilia = new Emilia();
  54.  
  55.         timer = new Timer(5, this);
  56.         timer.start();
  57.     }
  58.  
  59.  
  60.     public void paint(Graphics g) {
  61.         super.paint(g);
  62.  
  63.         Graphics2D g2d = (Graphics2D)g;
  64.         g2d.drawImage(emilia.getImage(), emilia.getX(), emilia.getY(), this);
  65.  
  66.         Toolkit.getDefaultToolkit().sync();
  67.         g.dispose();
  68.     }
  69.  
  70.  
  71.     public void actionPerformed(ActionEvent e) {
  72.         emilia.move();
  73.         repaint();  
  74.     }
  75.  
  76.  
  77.     private class TAdapter extends KeyAdapter {
  78.  
  79.         public void keyReleased(KeyEvent e) {
  80.             emilia.keyReleased(e);
  81.         }
  82.  
  83.         public void keyPressed(KeyEvent e) {
  84.             emilia.keyPressed(e);
  85.         }
  86.     }
  87.  
  88. }
  89.  
  90. --
  91. Emilia.java
  92. --
  93. import java.awt.Image;
  94. import java.awt.event.KeyEvent;
  95.  
  96. import javax.swing.ImageIcon;
  97.  
  98. public class Emilia {
  99.  
  100.     private String emilia = "emiliasprite.png";
  101.  
  102.     private int dx;
  103.     private int dy;
  104.     private int x;
  105.     private int y;
  106.     private Image image;
  107.  
  108.     public Emilia() {
  109.         ImageIcon ii = new ImageIcon(this.getClass().getResource(emilia));
  110.         image = ii.getImage();
  111.         x = 40;
  112.         y = 60;
  113.     }
  114.  
  115.  
  116.     public void move() {
  117.         x += dx;
  118.         y += dy;
  119.     }
  120.  
  121.     public int getX() {
  122.         return x;
  123.     }
  124.  
  125.     public int getY() {
  126.         return y;
  127.     }
  128.  
  129.     public Image getImage() {
  130.         return image;
  131.     }
  132.  
  133.     public void keyPressed(KeyEvent e) {
  134.  
  135.         int key = e.getKeyCode();
  136.  
  137.         if (key == KeyEvent.VK_A) {
  138.             dx = -1;
  139.         }
  140.  
  141.         if (key == KeyEvent.VK_D) {
  142.             dx = 1;
  143.         }
  144.  
  145.         if (key == KeyEvent.VK_W) {
  146.             dy = -1;
  147.         }
  148.  
  149.  
  150.     }
  151.  
  152.     public void keyReleased(KeyEvent e) {
  153.         int key = e.getKeyCode();
  154.  
  155.         if (key == KeyEvent.VK_A) {
  156.             dx = 0;
  157.         }
  158.  
  159.         if (key == KeyEvent.VK_D) {
  160.             dx = 0;
  161.         }
  162.  
  163.         if (key == KeyEvent.VK_W) {
  164.             dy = 0;
  165.         }
  166.     }
  167. }
  168.  
  169. --
  170. ImageIcon.java Line 205
  171. --
  172.         this(location, location.toExternalForm());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement