Shapes not moving on drawing panel java - java

i am trying to make a simple java game with a bat(paddle) and ball. So far i have painted the 2 objects onto the panel, however i cant get them to move. i have added key events for the bat and a move() method for the ball. Below are all my classes.
Game class:
public class Game extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Game();
}});
}
MyDrawingPanel myDrawingPanel = new MyDrawingPanel(this);
MyUIPanel myUIPanel = new MyUIPanel(this);
public Game()
{
setSize(1160,660); // you may change frame and panel sizes
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(myDrawingPanel);
cp.add(myUIPanel);
setVisible(true);
}
}
MyDrawingPanel class:
class MyDrawingPanel extends JPanel {
Game game;
Ball ball = new Ball(this);
Bat bat = new Bat(this);
public MyDrawingPanel(Game game)
{
this.game=game;
setPreferredSize(new Dimension(800,600));
setBackground(Color.RED);
requestFocus();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
Graphics2D gBat = (Graphics2D) g;
bat.paint(gBat);
}
}
Ball class:
public class Ball {
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private MyDrawingPanel myDrawingPanel;
public Ball(MyDrawingPanel myDrawingPanel) {
this.myDrawingPanel = myDrawingPanel;
}
public void move() {
if (x + xa < 0)
xa = 1;
if (x + xa > myDrawingPanel.getWidth() - 30)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > myDrawingPanel.getHeight() - 30)
ya = -1;
x = x + xa;
y = y + ya;
}
public void paint(Graphics2D g) {
g.fillOval(x, y, 30, 30);
}
}
Bat class:
public class Bat{
int x = 0;
int xa = 0;
private MyDrawingPanel myDrawingPanel;
public Bat(MyDrawingPanel myDrawingPanel)
{
this.myDrawingPanel = myDrawingPanel;
}
public void move(){
if(x + xa > 0 && x + xa <myDrawingPanel.getWidth()-60 )
x = x + xa;
}
public void paint(Graphics2D g)
{
g.setColor(Color.BLUE);
g.fillRoundRect(x, 500, 100, 20, 10, 10);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -1;
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 1;
}
public void keyReleased(KeyEvent e)
{
xa = 0;
}
}

Methods such as keyPressed and keyReleased don't do anything without a KeyListener implementation. So currently, your methods are useless. What you should do have the DrawingPanel class implement the KeyListener, like this
public class DrawingPanel extends JPanel implements KeyListener {
...
}
The methods, keyPressed and keyReleased should be in that class. You'll also want setter methods for what ever variables are updated in the Bat class, like x and xa, is those are the variables that determine the movement of the Bat.
So in the keyPressed, which should be in the DrawingPanel class, it could look something like this
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
bat.setXa(bat.getXa() - 1);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
bat.setXa(1);
repaint();
}
}
Notice how I call repaint(). That's what you need to do after you move something.
This should get you started in the right direction
EDIT Forget the majority of the above answer.
Instead of using a KeyListener though I would recommeng using key binding. You may face focus problems using KeyListener. I implemented the keybinding for the DrawingPanel and it works fine. It'll give you some ideas to work with.
class MyDrawingPanel extends JPanel {
Game game;
Ball ball = new Ball(this);
Bat bat = new Bat(this);
public MyDrawingPanel(Game game) {
this.game = game;
setPreferredSize(new Dimension(800, 600));
setBackground(Color.RED);
requestFocus();
Action rightAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
bat.x += 10;
repaint();
}
};
Action leftAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
bat.x -= 10;
repaint();
}
};
InputMap inputMap = getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
actionMap.put("rightAction", rightAction);
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
actionMap.put("leftAction", leftAction);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
Graphics2D gBat = (Graphics2D) g;
bat.paint(gBat);
}
}
See How to use key bindings

Related

JPanels not being added to the JFrame

So I was building this very simple game and I've add all the elements (for the movement) but when I hit run it only shows me the JFrame and not the JPanel. I should mention that I am very new to Java and the code you see bellow might not be the cleanest (it's also not complete).
Main
public class Main {
public static void main(String[] args) {
GameFrame frame = new GameFrame();
}
}
GameFrame
public class GameFrame extends JFrame {
GamePanel panel;
GameFrame() {
panel = new GamePanel();
this.add(panel);
this.setTitle("Survival");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
Panel
public class GamePanel extends JPanel implements Runnable {
static final int GAME_WIDTH = 1000;
static final int GAME_HEIGHT = GAME_WIDTH * 9 / 16;
static final Dimension SCREEN_SIZE = new Dimension(GAME_WIDTH, GAME_HEIGHT);
static final int PLAYER_WIDTH = 40;
static final int PLAYER_HEIGHT = 40;
Thread gameThread;
Graphics graphics;
Player player;
int fireBallY = (int) (Math.random() * 563);
Fireballs fireball;
Image image;
int dimSky = 150;
GamePanel() {
newFireballs();
newPlayer();
this.setFocusable(true);
this.addKeyListener(new actionListener());
this.setPreferredSize(SCREEN_SIZE);
this.setBackground(Color.CYAN);
gameThread = new Thread(this);
gameThread.start();
}
private void newPlayer() {
player = new Player(200, 150, PLAYER_WIDTH, PLAYER_HEIGHT);
}
private void newFireballs() {
for (int i = 0; i < 5; i++) new Fireballs(800, fireBallY, PLAYER_WIDTH, PLAYER_HEIGHT);
}
public void paint(Graphics g) {
image = createImage(getWidth(), getHeight());
graphics = image.getGraphics();
draw(graphics);
g.drawImage(image, 0,0, this);
}
public void draw(Graphics g) {
player.draw(g);
}
public void move() {
player.moveX();
player.moveY();
}
public void collision() {}
public void run() {
//game loop
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
while (true) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
move();
collision();
repaint();
delta--;
System.out.println("It Works!");
}
}
}
private class actionListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
player.KeyPressed(e);
}
public void keyReleased(KeyEvent e) {
player.KeyReleased(e);
}
}
}
Player
public class Player extends Rectangle {
int xVelocity;
int yVelocity;
int speed = 10;
public Player(int x, int y, int PLAYER_WIDTH, int PLAYER_HEIGHT) {
super(x,y, PLAYER_WIDTH, PLAYER_HEIGHT);
}
public void KeyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
setYDirection(-speed);
moveX();
moveY();
}
if (e.getKeyCode() == KeyEvent.VK_S) {
setYDirection(+speed);
moveX();
moveY();
}
if (e.getKeyCode() == KeyEvent.VK_D) {
setXDirection(+speed);
moveX();
moveY();
}
if (e.getKeyCode() == KeyEvent.VK_A) {
setXDirection(-speed);
moveX();
moveY();
}
}
public void KeyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
setYDirection(-10);
moveX();
moveY();
}
if (e.getKeyCode() == KeyEvent.VK_S) {
setYDirection(+1);
moveX();
moveY();
}
if (e.getKeyCode() == KeyEvent.VK_D) {
setXDirection(+1);
moveX();
moveY();
}
if (e.getKeyCode() == KeyEvent.VK_A) {
setXDirection(-1);
moveX();
moveY();
}
}
public void setXDirection(int xVelocity) {
x = x + xVelocity;
}
public void setYDirection(int yVelocity) {
y = y + yVelocity;
}
public void moveX() {
x = x + xVelocity;
}
public void moveY() {
y = y + yVelocity;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
}
}
There are other classes but those are completely empty and I don't think they are the cause of my problem.
Several issues:
If you are new to Swing, do not start off with too much complexity.
when you cannot see a JPanel on screen, my approach would be first to amend that. Only then add some complexity to the JPanel itself.
I have done that below by simply displaying a big red JPanel.
Is the reason you do not see anything because you did not set a JFrame.size(Dimension d)? I have added that in the construtor of the JFrame
Indeed, as #Frakcool and #camickr have pointed out in their comments, the canonic approach in Swing is overriding JPanel.paintComponent(Graphics g)
You should obey the thread landscape that Swing has prepared for you. That is indeed use SwingWorker threads for background tasks as #Frakcool commented.
And also use the EventDispatchingThread to handle events and startup. In the main method below, you see the call to SwingUtilities.invokeLater(new Runnable()).
The more complex your application gets, the more likely you will run into trouble if not obeying the EventDispatchingThread.
here is my - minimal - sample code containing examples for the points above. Within your game logic may be further work to be done, which I think is out of scope.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GameFrame extends JFrame {
GamePanel panel;
GameFrame() {
panel = new GamePanel();
this.add(panel);
this.setTitle("Survival");
this.pack();
/*
* might help to set a window size:
*/
setSize(800, 600);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private class GamePanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
// check if needed, eg for subcomponents
// super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GameFrame();
}
});
}
}

How to implement a KeyListener to an object in a JFrame?

I'm attempting to make Frogger in java for a school project but I'm having a lot of difficulties setting up KeyListener for the actual frog character.
I've tried setting up key bindings, requesting focus for the JPanel and JFrame, moving around where the character is initiated but nothing has seemed to work. This is the remnants of my attempts.
This is the program that runs my game.
import javax.swing.*;
public class Frogger
{
JFrame frame = new JFrame("Frogger");
CPT c = new CPT();
public Frogger()
{
frame.setBounds(0,0,700,500);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(c);
}
public static void main(String[] args){
new Frogger();
}
}
The main game
public CPT() {
setLayout(new BorderLayout());
label = new JLabel("Frogger");
frame1 = new JFrame("Main");
label.setFont(new Font("Serif", Font.BOLD,50));
label.setBounds(275,10,250,250);
button1 = new JButton("PLAY");
button1.setBounds(300,350,100,50);
button1.setOpaque(false);
button1.setVisible(true);
this.setOpaque(false);
this.setLayout(null);
this.add(label);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
frame1.setSize(700,500);
frame1.setResizable(false);
frame1.setFocusable(false);
button1.setVisible(false);
frame1.add(new TrainCanvas());
frame1.add(p1);
p1.requestFocus();
}
});
this.add(button1); }
This is the TrainCanvas class that Draws the cars in the games as well as the frog
class TrainCanvas extends JComponent
{
private int lastX = 0;
private int lastX_1 = 0;
private int lastX_2 = 0;
public TrainCanvas() {
Thread animationThread = new Thread(new Runnable() {
public void run() {
while (true) {
repaint();
try {Thread.sleep(10);} catch (Exception ex) {}
}
}
});
animationThread.start();
}
public void paintComponent(Graphics g) {
Graphics2D gg = (Graphics2D) g;
//Draws Train 1
int w = getWidth();
int h = getHeight();
int trainW_1 = 100;
int trainH_1 = 5;
int trainSpeed_1 = 3;
int x = lastX + trainSpeed_1;
if (x > w + trainW_1) {
x = -trainW_1;
}
gg.setColor(Color.BLACK);
gg.fillRect(x, h/2 + trainH_1, trainW_1, trainH_1);
lastX = x;
Graphics2D g3 = (Graphics2D) g;
frog = new Rectangle(f_x,f_y,25,25);
g3.fill(frog);
g3.setColor(Color.GREEN);
}
}
Finally, the Key Listener
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()== KeyEvent.VK_UP)
{
CPT.f_y -= 100;
repaint();
}
else if(e.getKeyCode()== KeyEvent.VK_RIGHT)
{
CPT.f_x += 100;
repaint();
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
CPT.f_x -= 100;
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
CPT.f_y += 100;
repaint();
}
else {}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
The program runs perfectly fine without giving me any errors, which is making this troublesome. Whenever it gets to the main game window, none of the keys seem to work.

Blank screen during the execution of a java application on the swing frame

I have the following three classes:
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class Racquet {
int x = 0;
int xa = 0;
private Game game;
public Racquet(Game game) {
this.game= game;
}
public void move() {
if (x + xa > 0 && x + xa < game.getWidth()-60)
x = x + xa;
}
public void paint(Graphics2D g) {
g.fillRect(x, 330, 60, 10);
}
public void keyReleased(KeyEvent e) {
xa = 0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -1;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 1;
}
}
import java.awt.Graphics2D;
public class Ball {
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private Game game;
public Ball(Game game) {
this.game= game;
}
void move() {
if (x + xa < 0)
xa = 1;
if (x + xa > game.getWidth() - 30)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > game.getHeight() - 30)
ya = -1;
x = x + xa;
y = y + ya;
}
public void paint(Graphics2D g) {
g.fillOval(x, y, 30, 30);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Game extends JPanel {
Ball ball = new Ball(this);
Racquet racquet = new Racquet(this);
public Game() {
addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
racquet.keyReleased(e);
}
#Override
public void keyPressed(KeyEvent e) {
racquet.keyPressed(e);
}
});
setFocusable(true);
}
private void move() {
ball.move();
racquet.move();
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
racquet.paint(g2d);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Mini Tennis");
Game game = new Game();
frame.add(game);
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
for(long i=0;i<=100000*100;i++)
{
}
}
}
}
import javax.swing.*;
import java.awt.event.*;
class T extends JFrame implements ActionListener
{
public T()
{
setVisible(true);
setSize(300,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
JButton b1=new JButton("Click me");
b1.setBounds(150,150,50,50);
b1.addActionListener(this);
add(b1);
}
public void actionPerformed(ActionEvent a)
{
Game ob=new Game();
ob.main(null);
}
public static void main(String args[])
{
T obj=new T();
}
}
When I execute the class T, I am supposed to get the Game but I don't know why I get the blank screen and not the game. I am new to Java so could someone help me out?
The reason that the T class doesn't work is that your code uses bad code to run the game loop, a while (true) loop that if called on the Swing event thread (as T does), will completely block the this thread, preventing it from performing its necessary functions including drawing the GUI.
The solution is to use a Swing Timer to drive your game loop, not a while (true) loop. To gain a better understanding of Swing threading issues, please read: Lesson: Concurrency in Swing

creating many objects with collision properties. JAVA

I have a simple program with three rectangles: one that can move with the push of the arrow keys, and two that are already moving back and forth on their own.
When the 'player' rectangle and top red collide, the player driven rectangle gets put back to (0,0). When I try to collide the player rectangle with the bottom red rectangle, it does not have those collision properties and I have no idea why.
What am I missing?
import java.awt.*;//needed for graphics
import javax.swing.*;//needed for JFrame window
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class javaapplication23 extends JFrame implements KeyListener, ActionListener {
public static int x = 0;
public static int y = 0;
public static int x2 = 100;
public static int y2 = 100;
public javaapplication23() {//constructor for JPanel
add(new JP());
}//close Jpanel Contructor
public static void main(String[] args) {
javaapplication23 w = new javaapplication23();
w.setTitle("MIKE IS AWESOME");
w.setSize(Toolkit.getDefaultToolkit().getScreenSize());
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setVisible(true);
w.addKeyListener(w);
}
public class JP extends JPanel {//start JPanel CLass
public JP() {
Container c = getContentPane();
c.setBackground(Color.white);//backgraund color can be changed
}
public void paint(Graphics g) {//opens paint method
super.paint(g);
player(g, x, y);
g.setColor(Color.RED);
enemylevel1(g, x2, y2);
Rectangle enemyblocks = new Rectangle(x2, y2, 25, 25);
Rectangle player = new Rectangle(x, y, 25, 25);
enemyblocks.contains(x2, y2);
player.contains(x, y);
if (player.getBounds().intersects(enemyblocks.getBounds())) {
x = 0;
y = 0;
}
pause(1);
repaint();
}//close paint method
}//close JPanel Class
public static void pause(int time) {
try //opens an exception handling statement
{
Thread.sleep(time);
} catch (InterruptedException e) {
} //captures the exception
}
public void actionPerformed(ActionEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_RIGHT) {
x += 20;//global variable controlling right movement
repaint();
}
if (e.getKeyCode() == e.VK_LEFT) {
x -= 20;//global variable controlling left movement
repaint();
}
if (e.getKeyCode() == e.VK_UP) {
y -= 20;//global variable controlling up movement
repaint();
}
if (e.getKeyCode() == e.VK_DOWN) {
y += 20;//global variable controlling down movement
repaint();
}
}
public void player(Graphics g, int x, int y) {
g.fillRect(x, y, 30, 30);
}
public void enemylevel1(Graphics g, int x, int y) {
g.fillRect(x2, y2, 25, 25);
g.fillRect(x2, y2 + 100, 25, 25);
if (x2 < 200 && y2 == 100) {
x2 += 1;
}
if (x2 == 200 && y2 >= 100) {
y2 += 1;
}
if (x2 <= 200 && y2 >= 101) {
x2 -= 1;
}
if (x2 == 100 && y2 <= 101) {
y2 -= 1;
}
pause(10);
repaint();
}
}
Start by having a look at Working with Geometry, this will allow you to reduce much of the code complexity.
Basically, a enemy is just a Rectangle, Graphics2D can paint these without to much of an issue. What you need to do is create an instance which can also update it's position based on your needs
public class Enemy extends Rectangle {
private int xDelta;
public Enemy(int x, int y) {
super(x, y, 20, 20);
if (x == 0) {
xDelta = 1;
} else {
xDelta = -1;
}
}
public void update(Rectangle bounds) {
x += xDelta;
if (x < bounds.x) {
x = bounds.x;
xDelta *= -1;
} else if (x > bounds.x + bounds.width - width) {
x = bounds.x + bounds.width - width;
xDelta *= -1;
}
}
}
So, this creates a single unit of work, which is isolated from everything else and carries it's own logic with it. This makes updating it, painting and generally working with much simpler.
Next, you need to create a List of these
public class Bounce extends JPanel implements KeyListener, ActionListener {
private List<Enemy> enemies;
//...
public Bounce() {
enemies = new ArrayList<>(5);
int y = 100;
for (int index = 0; index < 5; index++) {
int x = (index % 2 == 0) ? 0 : 200;
Enemy enemy = new Enemy(x, y);
enemies.add(enemy);
y += 60;
}
This creates a List of Enemys which are distributed evenly within the container.
Now, we need to paint them....
#Override
protected void paintComponent(Graphics g) {//opens paint method
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
g2d.setColor(Color.RED);
for (Enemy enemy : enemies) {
g2d.fill(enemy);
}
}//close paint method
nb: General convention suggests that you should override paintComponent when you want to perform custom painting
But they don't move, that kind of sucks. So we need a way to, on a regular bases, update the position of the enemies...
First, we create a simple method which we can call to update the enemies, remember, they are capable of updating themselves, we just need to tell them when
public void updateState() {
Rectangle bounds = new Rectangle(20, 20, 200, 200);
for (Enemy enemy : enemies) {
enemy.update(bounds);
}
}
Remember, the Enemy is self contained, it knows how to update itself based on the constraints you have provided.
And now, we need to call this method on a regular bases...
javax.swing.Timer timer = new javax.swing.Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updateState();
repaint();
}
});
timer.start();
Okay, this will schedule a callback every 40 milliseconds which will allow us to call the updateState method and repaint the component. This is neat because it won't block the Event Dispatching Thread (making our program look like it's hung) but which notifies us within the context of the EDT, making it safe to update the UI from within - WIN/WIN :)
Take a look at Concurrency in Swing and How to use Swing Timers for more details.
Okay, but that doesn't solve the collision...
The player is also a Rectangle, so why not use the same concept we have with the enemies...
public class Bounce extends JPanel implements KeyListener, ActionListener {
private List<Enemy> enemies;
private Rectangle player;
//...
public Bounce() {
player = new Rectangle(0, 0, 30, 30);
enemies = new ArrayList<>(5);
//...
}
#Override
protected void paintComponent(Graphics g) {//opens paint method
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
drawPlayer(g2d);
g2d.setColor(Color.RED);
for (Enemy enemy : enemies) {
g2d.fill(enemy);
if (player.intersects(enemy)) {
player.x = 0;
player.y = 0;
}
}
}//close paint method
public void drawPlayer(Graphics2D g) {
g.fill(player);
}
Which ends up with something like...
This allows you to add/remove enemies as you want and also change the way in which the enemies move, simply and easily
An my "awesome" test code...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Bounce extends JPanel implements KeyListener, ActionListener {
private List<Enemy> enemies;
private Rectangle player;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Bounce());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public Bounce() {
player = new Rectangle(0, 0, 30, 30);
enemies = new ArrayList<>(5);
int y = 100;
for (int index = 0; index < 5; index++) {
int x = (index % 2 == 0) ? 0 : 200;
Enemy enemy = new Enemy(x, y);
enemies.add(enemy);
y += 60;
}
setBackground(Color.white);//backgraund color can be changed
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updateState();
repaint();
}
});
timer.start();
setFocusable(true);
requestFocusInWindow();
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
requestFocusInWindow();
}
});
addKeyListener(this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(240, 400);
}
#Override
protected void paintComponent(Graphics g) {//opens paint method
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
drawPlayer(g2d);
g2d.setColor(Color.RED);
for (Enemy enemy : enemies) {
g2d.fill(enemy);
if (player.intersects(enemy)) {
player.x = 0;
player.y = 0;
}
}
}//close paint method
public void actionPerformed(ActionEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_RIGHT) {
player.x += 20;//global variable controlling right movement
}
if (e.getKeyCode() == e.VK_LEFT) {
player.x -= 20;//global variable controlling left movement
}
if (e.getKeyCode() == e.VK_UP) {
player.y -= 20;//global variable controlling up movement
}
if (e.getKeyCode() == e.VK_DOWN) {
player.y += 20;//global variable controlling down movement
}
}
public void drawPlayer(Graphics2D g) {
g.fill(player);
}
public void updateState() {
Rectangle bounds = new Rectangle(20, 20, 200, 200);
for (Enemy enemy : enemies) {
enemy.update(bounds);
}
}
public class Enemy extends Rectangle {
private int xDelta;
public Enemy(int x, int y) {
super(x, y, 20, 20);
if (x == 0) {
xDelta = 1;
} else {
xDelta = -1;
}
}
public void update(Rectangle bounds) {
x += xDelta;
if (x < bounds.x) {
x = bounds.x;
xDelta *= -1;
} else if (x > bounds.x + bounds.width - width) {
x = bounds.x + bounds.width - width;
xDelta *= -1;
}
}
}
}

Background image for simple game?

I have been following the Java Game Programming for Beginners tutorial series, and wished to experiment by applying a background image. Unfortunately, when I render it through the paintComponent method, it moves with my sprite (albeit at one unit continuously as opposed to five); and when I render it through the paint method, I get a strange, flickering box that matches the color designated in the setBackground (color) property of the JFrame and it moves with the sprite identically to that of the prior instance (within paintComponent).
How might I code the image so as to remain static, as a background should be?
Code:
public class JavaGame extends JFrame{
int x, y;
private Image dbImage;
private Graphics dbg;
Image ghost;
Image bg;
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_LEFT){
if(x <= 8)
x = 8;
else
x += -5;
}
if(keyCode == e.VK_RIGHT){
if(x >= 235)
x = 235;
else
x += +5;
}
if(keyCode == e.VK_UP){
if(y <= 18)
y = 18;
else
y += -5;
}
if(keyCode == e.VK_DOWN){
if(y >= 235)
y = 235;
else
y += +5;
}
}
public void keyReleased(KeyEvent e){
}
}
public JavaGame(){
//Load images
ImageIcon i = new ImageIcon("C:/Users/Taylor/workspace/Java game/src/ghost.png");
ghost = i.getImage();
ImageIcon j = new ImageIcon("C:/Users/Taylor/workspace/Java game/src/bg.png");
bg = j.getImage();
//Game properties
addKeyListener(new AL());
setTitle("Java Game");
setSize(500, 500);
setResizable(false);
setVisible(true);
setBackground(Color.GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 150;
y = 150;
}
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, x, y, this);
}
public void paintComponent(Graphics g){
g.setColor(Color.WHITE);
g.drawImage(ghost, x, y, this);
repaint();
}
public static void main(String[] args) {
new JavaGame();
}
Pictures:
Were you copy/pasting code at random? That is what it looked like. There were so many odd aspects to that code that I did not document them all (a good one for code review, maybe). The example uses an asynchronous method to load the images (in order to get the animated image, animating). Use ImageIO.read(URL) for a synchronous way to load static images.
Here are some brief tips:
By the time this becomes deployed, the images will likely become an embedded resource and will not be accessible by File object. Add them to the run-time class-path and access them by URL.
Swing GUIs should be started and altered on the EDT (see the change to the main()).
Always call super.paint(g); (or paintComponent(g)) at the start of the method.
Don't extend frame, don't paint to a top level component. Instead extend panel and override paintComponent(). Add the panel to the frame.
Code
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
public class JavaGame extends JPanel {
int x, y;
private Image dbImage;
private Graphics dbg;
Image ghost;
Image bg;
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
if (x <= 8)
x = 8;
else
x += -5;
}
if (keyCode == e.VK_RIGHT) {
if (x >= 235)
x = 235;
else
x += +5;
}
if (keyCode == e.VK_UP) {
if (y <= 18)
y = 18;
else
y += -5;
}
if (keyCode == e.VK_DOWN) {
if (y >= 235)
y = 235;
else
y += +5;
}
}
public void keyReleased(KeyEvent e) {
}
}
public JavaGame() throws Exception {
// Load images
//ImageIcon i = new ImageIcon(
// "C:/Users/Taylor/workspace/Java game/src/ghost.png");
URL urlGhost = new URL("http://1point1c.org/gif/thum/plnttm.gif");
ghost = Toolkit.getDefaultToolkit().createImage(urlGhost);
//ImageIcon j = new ImageIcon(
// "C:/Users/Taylor/workspace/Java game/src/bg.png");
URL urlBG = new URL("http://pscode.org/media/stromlo2.jpg");
bg = Toolkit.getDefaultToolkit().createImage(urlBG);
setFocusable(true);
// Game properties
addKeyListener(new AL());
x = 150;
y = 150;
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
repaint();
}
};
Timer timer = new Timer(50,al);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg, 0, 0, null);
//dbImage = createImage(getWidth(), getHeight());
//dbg = dbImage.getGraphics();
//paintComponent(dbg);
g.drawImage(dbImage, x, y, this);
g.setColor(Color.WHITE);
g.drawImage(ghost, x, y, this);
//repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
JFrame f = new JFrame("Java Game");
f.setSize(500, 500);
f.setResizable(false);
f.setVisible(true);
f.setBackground(Color.GRAY);
f.setContentPane(new JavaGame());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Categories

Resources