Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.samkough.spaceshipbattle.main;
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class Main extends JFrame
- {
- public Main()
- {
- add(new Board());
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(400, 400);
- setLocationRelativeTo(null);
- setTitle("Spaceship Battle");
- setResizable(false);
- setVisible(true);
- }
- public static void main(String[] args)
- {
- new Main();
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- package com.samkough.spaceshipbattle.main;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- import com.samkough.spaceshipbattle.entity.Craft;
- @SuppressWarnings("serial")
- public class Board extends JPanel implements ActionListener
- {
- private Timer timer;
- private Craft craft;
- public Board()
- {
- addKeyListener(new TAdapter());
- setFocusable(true);
- setBackground(Color.BLACK);
- setDoubleBuffered(true);
- craft = new Craft();
- timer = new Timer(5, this);
- timer.start();
- }
- public void paint(Graphics g)
- {
- super.paint(g);
- Graphics2D g2d = (Graphics2D)g;
- // In the paint() method, we draw the spacecraft. We get the image and the coordinates from the sprite class.
- g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
- Toolkit.getDefaultToolkit().sync();
- g.dispose();
- }
- // The actionPerformed() method is called every 5ms. We move the sprite and repaint the board.
- public void actionPerformed(ActionEvent e)
- {
- craft.move();
- repaint();
- }
- private class TAdapter extends KeyAdapter
- {
- public void keyReleased(KeyEvent e)
- {
- craft.keyReleased(e);
- }
- public void keyPressed(KeyEvent e)
- {
- craft.keyPressed(e);
- }
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- /* This class represents a spacecraft. In this class we keep the image of the sprite and the coordinates of the sprite.
- * The keyPressed() and keyReleased() methods control whether the sprite is moving or is in standstill.
- */
- package com.samkough.spaceshipbattle.entity;
- import java.awt.Image;
- import java.awt.event.KeyEvent;
- import javax.swing.ImageIcon;
- public class Craft
- {
- private String craft = "craft.png";
- private int dx;
- private int dy;
- private int x;
- private int y;
- private Image image;
- public Craft()
- {
- ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
- image = ii.getImage();
- x = 40;
- y = 60;
- }
- // The move() method changes the coordinates of the sprite. These x, y values are used in the paint() method to draw the image of the sprite.
- public void move()
- {
- x += dx;
- y += dy;
- }
- public int getX()
- {
- return x;
- }
- public int getY()
- {
- return y;
- }
- public Image getImage()
- {
- return image;
- }
- public void keyPressed(KeyEvent e)
- {
- int key = e.getKeyCode();
- if (key == KeyEvent.VK_LEFT)
- {
- dx = -1;
- }
- if (key == KeyEvent.VK_RIGHT)
- {
- dx = 1;
- }
- if (key == KeyEvent.VK_UP)
- {
- dy = -1;
- }
- if (key == KeyEvent.VK_DOWN)
- {
- dy = 1;
- }
- }
- public void keyReleased(KeyEvent e)
- {
- int key = e.getKeyCode();
- // When we release the left cursor key, we set the dx variable to zero. The spacecraft will stop moving.
- if (key == KeyEvent.VK_LEFT)
- {
- dx = 0;
- }
- if (key == KeyEvent.VK_RIGHT)
- {
- dx = 0;
- }
- if (key == KeyEvent.VK_UP)
- {
- dy = 0;
- }
- if (key == KeyEvent.VK_DOWN)
- {
- dy = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment