How do I set an image in a rectangle? - java

I have a little problem. I have searched the internet everywhere, and I can't find how to set an image in a rectangle. It seems fairly simple, but I am just getting into game programming, and I can't seem to find an explanation on this. I was referred to this site by somebody in Y! Ansers, and I think you guys might be able to help. I have this code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Racing extends JFrame {
private static final long serialVersionUID = -198172151996959655L;
//makes the screen size
final int WIDTH = 900, HEIGHT = 650;
//keeps track of player speed
double plSpeed = .5, p2Speed = .5;
//numbers that represent direction
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, STOP = 5;
//keeps track of player direction
int p1Direction = STOP;
int p2Direction = STOP;
//create rectangles
Rectangle left = new Rectangle( 0, 0, WIDTH/9, HEIGHT );
Rectangle right = new Rectangle( ( WIDTH/9 )*8, 0, WIDTH/9, HEIGHT );
Rectangle top = new Rectangle ( 0, 0, WIDTH, HEIGHT/9 );
Rectangle bottom = new Rectangle( 0, ( HEIGHT/9 ) * 8, WIDTH, HEIGHT/9 );
Rectangle center = new Rectangle( (int) ((WIDTH/9) * 2.5), (int) ((HEIGHT/9) * 2.5), (int) ((WIDTH/9) * 5), (int) ((HEIGHT/9) * 4));
//makes the obstacles
Rectangle obstacle = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 7), WIDTH/10, HEIGHT/9 );
Rectangle obstacle2 = new Rectangle ( WIDTH/3, (int) ((HEIGHT/9 ) * 5), WIDTH/10, HEIGHT/4 );
Rectangle obstacle3 = new Rectangle ( 2 * (WIDTH/3), (int) ((HEIGHT/9) * 5),WIDTH/10, HEIGHT/4 );
Rectangle obstacle4 = new Rectangle ( WIDTH/3, HEIGHT/9, WIDTH/30, HEIGHT/9 );
Rectangle obstacle5 = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 1.5), WIDTH/30, HEIGHT/4 );
//makes the finish line
Rectangle finish = new Rectangle ( WIDTH/9, (HEIGHT/2)-HEIGHT/9, (int) ((WIDTH/9) * 1.5), HEIGHT/70 );
//makes player 1's car
Rectangle p1 = new Rectangle ( WIDTH/9, HEIGHT/2, WIDTH/30, WIDTH/30 );
//makes player 2's car
Rectangle p2 = new Rectangle ((( WIDTH/9 ) + ((int) ((WIDTH/9) * 1.5) /2)),(HEIGHT/2) + (HEIGHT/10), WIDTH/30, WIDTH/30);
//constructor
public Racing() {
//define defaults for the JFrame
super ("Racing");
setSize( WIDTH, HEIGHT );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setBackground(Color.BLACK);
//start the inner class
Move2 m2 = new Move2();
m2.start();
Move1 m1 = new Move1();
m1.start();
}
//draws the cars and race track
public void paint(Graphics g) {
//make borders green
g.setColor(Color.GREEN);
//rectangles for start lines (lineO = outer player, lineI = inner player)
Rectangle lineO = new Rectangle( WIDTH/9, HEIGHT/2, (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
Rectangle lineI = new Rectangle( ((WIDTH/9)+((int) ((WIDTH/9) * 1.5)/2)), (HEIGHT/2) + (HEIGHT/10), (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
//fill the rectangles
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
//make the starting line color white
g.setColor(Color.WHITE);
//draw starting line
g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
//make the finish line yellow
g.setColor(Color.YELLOW);
//draw the finish line
g.fillRect(finish.x,finish.y,finish.width,finish.height);
//make player one red
g.setColor(Color.RED);
//draw player 1
g.fill3DRect(p1.x,p1.y,p1.width,p2.height,true);
//make player two blue
g.setColor(Color.BLUE);
//now draw player two
g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
}
private class Move1 extends Thread implements KeyListener {
public void run() {
//makes the key listener "wake up"
addKeyListener(this);
//should be done in an infinite loop, so it repeats
while (true) {
//make try block, so it can exit if it errors
try {
//refresh screen
repaint();
//check to see if car hits wall
//if so, slow down
if (p1.intersects(left) || p1.intersects(right) ||
p1.intersects(top) || p1.intersects(bottom) ||
p1.intersects(obstacle) || p1.intersects(obstacle2) ||
p1.intersects(obstacle3) || p1.intersects(obstacle4) ||
p1.intersects(obstacle5) || p1.intersects (center) ||
p1.intersects(p2)) {
plSpeed = -5;
Thread.sleep(128);
p1Direction = STOP;
}
//lets the car stop
if (plSpeed==0) {
p1Direction = STOP;
}
//moves player based on direction
if (p1Direction==UP) {
p1.y -= (int) plSpeed;
}
if (p1Direction==DOWN) {
p1.y += (int) plSpeed;
}
if (p1Direction==LEFT) {
p1.x -= (int) plSpeed;
}
if (p1Direction==RIGHT) {
p1.x += (int) plSpeed;
}
if (p1Direction==STOP) {
plSpeed = 0;
}
//delays refresh rate
Thread.sleep(75);
}
catch(Exception e) {
//if an error, exit
break;
}
}
}
//have to input these (so it will compile)
public void keyPressed(KeyEvent event) {
try {
//makes car increase speed a bit
if (event.getKeyChar()=='w' ||
event.getKeyChar()=='a' ||
event.getKeyChar()=='s' ||
event.getKeyChar()=='d') {
plSpeed += .2;
repaint();
}
} catch (Exception I) {}
}
public void keyReleased(KeyEvent event) {}
//now, to be able to set the direction
public void keyTyped(KeyEvent event) {
if (event.getKeyChar()=='a') {
if (p1Direction==RIGHT) {
p1Brake();
} else {
if (p1Direction==LEFT) {
} else {
plSpeed = .5;
p1Direction = LEFT;
}
}
}
if (event.getKeyChar()=='s') {
if (p1Direction==UP) {
p1Brake();
} else {
if (p1Direction==DOWN) {
} else {
plSpeed = .5;
p1Direction = DOWN;
}
}
}
if (event.getKeyChar()=='d') {
if (p1Direction==LEFT) {
p1Brake();
} else {
if (p1Direction==RIGHT) {
} else {
plSpeed = .5;
p1Direction = RIGHT;
}
}
}
if (event.getKeyChar()=='w') {
if (p1Direction==DOWN) {
p1Brake();
} else {
if (p1Direction==UP) {
} else {
plSpeed = .5;
p1Direction = UP;
}
}
}
if (event.getKeyChar()=='z') {
p1Brake();
}
}
public void p1Brake () {
try {
while (plSpeed != 0) {
plSpeed -= .2;
Thread.sleep(75);
}
} catch (Exception e) {
plSpeed = 0;
}
}
}
private class Move2 extends Thread implements KeyListener {
public void run() {
//makes the key listener "wake up"
addKeyListener(this);
//should be done in an infinite loop, so it repeats
while (true) {
//make try block, so it can exit if it errors
try {
//refresh screen
repaint();
//check to see if car hits outside wall
//if so, slow down
if (p2.intersects(left) || p2.intersects(right) ||
p2.intersects(top) || p2.intersects(bottom) ||
p2.intersects(obstacle) || p2.intersects(obstacle2) ||
p2.intersects(obstacle3) || p2.intersects(obstacle4) ||
p2.intersects(obstacle5) || p2.intersects(p1) ||
p2.intersects(center)) {
plSpeed = -.25;
Thread.sleep(128);
p2Direction = STOP;
}
//makes car increase speed a bit
if (p2Speed <= 5) {
plSpeed += .2;
}
//lets the car stop
if (p2Speed == 0) {
p2Direction = STOP;
}
//moves player based on direction
if (p2Direction==UP) {
p2.y -= (int) p2Speed;
}
if (p2Direction==DOWN) {
p2.y += (int) p2Speed;
}
if (p2Direction==LEFT) {
p2.x -= (int) p2Speed;
}
if (p2Direction==RIGHT) {
p2.x += (int) p2Speed;
}
if (p2Direction==STOP) {
p2Speed = 0;
}
//delays refresh rate
Thread.sleep(75);
}
catch(Exception e) {
//if an error, exit
break;
}
}
}
public void keyPressed(KeyEvent event) {
try {
//makes car increase speed a bit
if (event.getKeyChar()=='j' ||
event.getKeyChar()=='k' ||
event.getKeyChar()=='l' ||
event.getKeyChar()=='i') {
plSpeed += .2;
}
} catch (Exception I) {}
}
public void keyReleased(KeyEvent event) {}
//now, to be able to set the direction
public void keyTyped(KeyEvent event) {
if (event.getKeyChar()=='j') {
if (p2Direction==RIGHT) {
p2Brake();
} else {
p2Direction = LEFT;
p2Speed = .4;
}
}
if (event.getKeyChar()=='k') {
if (p2Direction==UP) {
p2Brake();
} else {
p2Direction = DOWN;
p2Speed = .4;
}
}
if (event.getKeyChar()=='l') {
if (p2Direction==LEFT) {
p2Brake();
} else {
p2Direction = RIGHT;
p2Speed = .4;
}
}
if (event.getKeyChar()=='i') {
if (p2Direction==DOWN) {
p2Brake();
} else {
p2Direction = UP;
p2Speed = .4;
}
}
if (event.getKeyChar()=='m') {
p2Brake();
}
}
public void p2Brake () {
try {
while (p2Speed != 0) {
p2Speed -= .5;
Thread.sleep(75);
}
} catch (Exception i) {
p2Speed = 0;
}
}
}
//finally, to start the program
public static void main(String[] args) {
Racing frame = new Racing();
frame.setVisible( true );
frame.setLocationRelativeTo( null );
frame.setResizable( false );
}
}
Currently I am just filling the p1 rectangle with the color red. I have an image of a car, and want to replace the red with the car. Any notes on how to do this?
** EDIT **
I found my answer. Using isah's g.drawImage, combined with the toolkit for getting the image, I was able to do this. I am leaving this up for anybody who wants to know.

How about drawImage. Take a look at this tutorial.

Related

How to change color automatically in processing.exe?

I currently learned about processing.exe. I've made a snake game in processing and I want to modify it. What I want is every time the snake eats food, the food that is moving randomly also gets given random colors.
here's my code:
snake s;
int grid = 15;
PVector food;
int r;
int g;
int b;
int warna;
void setup() {
size(600, 600);
s = new snake();
food = new PVector();
r = (int)random(255);
g = (int)random(255);
b = (int)random(255);
frameRate(15);
newFood();
}
void draw() {
background(0);
s.showScore();
s.display();
if (s.gameOver()) {
background(0);
textAlign(LEFT);
textSize(25);
fill(255);
text("Game Over", 10, 10, width - 20, 50);
noLoop();
}
if (s.eat(food)) {
newFood();
}
s.move();
fill (r, g, b);
rect (food.x, food.y, grid, grid);
}
void newFood() {
food.x = floor(random(width));
food.y = floor(random(height));
food.x = floor(food.x/grid) * grid;
food.y = floor(food.y/grid) * grid;
if (food.x == floor(random(width)) && food.y == floor(random(height))){
fill (r = (int)random(255), g = (int)random(255), b = (int)random(255));
rect( food.x, food.y, grid, grid);
}
}
void keyPressed() {
if (keyCode == UP) {
s.arah(0, -1);
} else if (keyCode == DOWN) {
s.arah(0, 1);
} else if (keyCode == RIGHT) {
s.arah(1, 0);
} else if (keyCode == LEFT) {
s.arah(-1, 0);
}
}
And this are the snake :
class snake {
float x = 0;
float y = 0;
float xspd = 1;
float yspd = 0;
int panjang = 0;
ArrayList<PVector> body = new ArrayList<PVector>();
snake() {
}
boolean eat(PVector pos) {
float d = dist(x, y, pos.x, pos.y);
if (d < 1) {
panjang++;
return true;
} else {
return false;
}
}
void arah(float x, float y) {
xspd = x;
yspd = y;
}
boolean gameOver() {
for (int i = 0; i < body.size(); i++) {
PVector pos = body.get(i);
float d = dist(x, y, pos.x, pos.y);
if (d < 1) {
panjang = 0;
body.clear();
return true;
}
}
return false;
}
void move() {
if (panjang > 0) {
if (panjang == body.size() && !body.isEmpty()) {
body.remove(0);
}
body.add(new PVector(x, y));
}
x = x + xspd*grid;
y = y + yspd*grid;
x = (x + width) % width;
y = (y + height) % height;
}
void display() {
noStroke();
fill(255);
for (PVector bagi : body) {
rect(bagi.x, bagi.y, grid, grid);
}
rect(x, y, grid, grid);
}
void showScore() {
textAlign(LEFT);
textSize(25);
fill(255);
text("Score: " + body.size(), 10, 10, width - 20, 50);
}
}
I've tried to change the color with declare r, g, b and assign a random color to it. But the food color doesn't seem to change every time the snake eats the food. Any suggestions on what I should do?
You're doing good so far. The only part you missed is that you want to change the food's color when you create some new food. So... you just have to take this part of the setup() method:
r = (int)random(255);
g = (int)random(255);
b = (int)random(255);
and move it in the newFood() method.
Hope this helps. Have fun!

Move an image from one place to another

I have a class called enemy and another class called "goal". The goal is that the enemy has to move towards the "goal". So I got the X and Y position of the "goal" but and when I implement that in the enemy class the enemy should move but it doesn't. Why is that?
Here is what I have done so far.
Main Class:
public class GameManager extends JFrame implements KeyListener {
private int canvasWidth;
private int canvasHeight;
private int borderLeft;
private int borderTop;
private BufferedImage canvas;
private Stage stage;
private Enemy[] enemies;
private Player player;
private Goal goal;
private Graphics gameGraphics;
private Graphics canvasGraphics;
private int numEnemies;
private boolean continueGame;
public static void main(String[] args) {
// During development, you can adjust the values provided in the brackets below
// as needed. However, your code must work with different/valid combinations
// of values.
GameManager managerObj = new GameManager(1980, 1280, 30);
}
public GameManager(int preferredWidth, int preferredHeight, int maxEnemies) {
this.borderLeft = getInsets().left;
this.borderTop = getInsets().top;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (screenSize.width < preferredWidth)
this.canvasWidth = screenSize.width - getInsets().left - getInsets().right;
else
this.canvasWidth = preferredWidth - getInsets().left - getInsets().right;
if (screenSize.height < preferredHeight)
this.canvasHeight = screenSize.height - getInsets().top - getInsets().bottom;
else
this.canvasHeight = preferredHeight - getInsets().top - getInsets().bottom;
setSize(this.canvasWidth, this.canvasHeight);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
addKeyListener(this);
Random rng = new Random(2);
this.canvas = new BufferedImage(this.canvasWidth, this.canvasHeight, BufferedImage.TYPE_INT_RGB);
// Create a Stage object to hold the background images
this.stage = new Stage();
// Create a Goal object with its initial x and y coordinates
this.goal = new Goal(this.canvasWidth / 2, Math.abs(rng.nextInt()) % this.canvasHeight);
// Create a Player object with its initial x and y coordinates
this.player = new Player(this.canvasWidth - (Math.abs(rng.nextInt()) % (this.canvasWidth / 2)),
(Math.abs(rng.nextInt()) % this.canvasHeight));
// Create the Enemy objects, each with a reference to this (GameManager) object
// and their initial x and y coordinates.
this.numEnemies = maxEnemies;
this.enemies = new Enemy[this.numEnemies];
for (int i = 0; i < this.numEnemies; i++) {
this.enemies[i] = new Enemy(this, Math.abs(rng.nextInt()) % (this.canvasWidth / 4),
Math.abs(rng.nextInt()) % this.canvasHeight);
}
this.gameGraphics = getGraphics();
this.canvasGraphics = this.canvas.getGraphics();
this.continueGame = true;
while (this.continueGame) {
updateCanvas();
}
this.stage.setGameOverBackground();
updateCanvas();
}
public void updateCanvas() {
long start = System.nanoTime();
// If the player is alive, this should move the player in the direction of the
// key that has been pressed
// Note: See keyPressed and keyReleased methods in the GameManager class.
this.player.performAction();
// If the enemy is alive, the enemy must move towards the goal. The goal object
// is obtained via the GameManager object that is given at the time of creating
// an Enemy object.
// Note: The amount that the enemy moves must be much smaller than that of
// the player above or else the game becomes hard to play.
for (int i = 0; i < this.numEnemies; i++) {
this.enemies[i].performAction();
}
if ((Math.abs(this.goal.getX() - this.player.getX()) < (this.goal.getCurrentImage().getWidth() / 2))
&& (Math.abs(this.goal.getY() - this.player.getY()) < (this.goal.getCurrentImage().getWidth() / 2))) {
for (int i = 0; i < this.numEnemies; i++) {
// Sets the image of the enemy to the "dead" image and sets its status to
// indicate dead
this.enemies[i].die();
}
// Sets the image of the enemy to the "dead" image and sets its status to
// indicate dead
this.goal.die();
// Sets the background of the stage to the finished game background.
this.stage.setGameOverBackground();
this.continueGame = false;
}
// If an enemy is close to the goal, the player and goal die
int j = 0;
while (j < this.numEnemies) {
if ((Math.abs(this.goal.getX() - this.enemies[j].getX()) < (this.goal.getCurrentImage().getWidth() / 2))
&& (Math.abs(this.goal.getY() - this.enemies[j].getY()) < (this.goal.getCurrentImage().getWidth()
/ 2))) {
this.player.die();
this.goal.die();
this.stage.setGameOverBackground();
j = this.numEnemies;
this.continueGame = false;
}
j++;
}
try {
// Draw stage
this.canvasGraphics.drawImage(stage.getCurrentImage(), 0, 0, null);
// Draw player
this.canvasGraphics.drawImage(player.getCurrentImage(),
this.player.getX() - (this.player.getCurrentImage().getWidth() / 2),
this.player.getY() - (this.player.getCurrentImage().getHeight() / 2), null);
// Draw enemies
for (int i = 0; i < this.numEnemies; i++) {
this.canvasGraphics.drawImage(this.enemies[i].getCurrentImage(),
this.enemies[i].getX() - (this.enemies[i].getCurrentImage().getWidth() / 2),
this.enemies[i].getY() - (this.enemies[i].getCurrentImage().getHeight() / 2), null);
}
// Draw goal
this.canvasGraphics.drawImage(this.goal.getCurrentImage(),
this.goal.getX() - (this.goal.getCurrentImage().getWidth() / 2),
this.goal.getY() - (this.goal.getCurrentImage().getHeight() / 2), null);
} catch (Exception e) {
System.err.println(e.getMessage());
}
// Draw everything.
this.gameGraphics.drawImage(this.canvas, this.borderLeft, this.borderTop, this);
long end = System.nanoTime();
this.gameGraphics.drawString("FPS: " + String.format("%2d", (int) (1000000000.0 / (end - start))),
this.borderLeft + 50, this.borderTop + 50);
}
public Goal getGoal() {
return this.goal;
}
public void keyPressed(KeyEvent ke) {
// Below, the setKey method is used to tell the Player object which key is
// currently pressed.
// The Player object must keep track of the pressed key and use it for
// determining the direction
// to move.
if (ke.getKeyCode() == KeyEvent.VK_LEFT)
this.player.setKey('L', true);
if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
this.player.setKey('R', true);
if (ke.getKeyCode() == KeyEvent.VK_UP)
this.player.setKey('U', true);
if (ke.getKeyCode() == KeyEvent.VK_DOWN)
this.player.setKey('D', true);
if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
this.continueGame = false;
}
#Override
public void keyReleased(KeyEvent ke) {
// Below, the setKey method is used to tell the Player object which key is
// currently released.
// The Player object must keep track of the pressed key and use it for
// determining the direction
// to move.
if (ke.getKeyCode() == KeyEvent.VK_LEFT)
this.player.setKey('L', false);
if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
this.player.setKey('R', false);
if (ke.getKeyCode() == KeyEvent.VK_UP)
this.player.setKey('U', false);
if (ke.getKeyCode() == KeyEvent.VK_DOWN)
this.player.setKey('D', false);
}
#Override
public void keyTyped(KeyEvent ke) {
}
}
Player Class:
private int myX;
private int myY;
private char d;
public Player(int i, int j) {
// TODO Auto-generated constructor stub
try {
this.imageRunning = ImageIO.read(new File(
"/Users/Desktop/images/player-alive.png"));
this.imageOver = ImageIO.read(new File(
"/Users/Desktop/images/player-dead.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.imageCurrent = this.imageRunning;
myX = i;
myY = j;
}
public void performAction() {
}
public int getX() {
return myX;
}
public int getY() {
return myY;
}
public BufferedImage getCurrentImage() {
return this.imageCurrent;
}
public void die() {
this.imageCurrent = this.imageOver;
}
public void setKey(char c, boolean b) {
}
That simply changes the variables gmX and gmY. Nothing more. You need to update the frame after setting the new gmX and gmY values. Assuming frameis your JFrame object, you need something like:
public void performAction() {
gmX += 40;
gmY += 40;
frame.repaint();
}
You need to redraw graphics on the component:
gmY += 40;
repaint(); /* add this where you want to redraw */

Using keybinding to make sprite continue to move and Snake body

It's the same game that I'm working on and I've come to a problem that I can't understand again. I'm using Key Bindings to move my sprite and it's working great! but since it's a snake game I need the sprite to keep moving without stopping and change direction and keep moving after the player types a key. I know it's possible with KeyListener, but I really don't want to have to change my program entirely. I just need to know what code needs to change, if possible.
On top of that I'm also working on two arrays for the x and y co-ordinates for the snake body so that squares will follow behind the head, but I can't get it to paint. and how to display an integer for the score.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.ArrayList;
public class Snake2 extends JFrame {
/* Sprite: snake head co-ordinates */
int x = 400;
int y = 450;
int width = 10;
int height = 10;
/* Sprite: snake body */
int length = 0;
ArrayList <Integer> bodyX = new ArrayList <Integer>();
ArrayList <Integer> bodyY = new ArrayList <Integer>();
/* Score */
int point = 0;
/* Sprite: mouse co-ordinates */
Random rand = new Random();
int addx = (rand.nextInt(10))*10;
int addy = (rand.nextInt(10))*10;
int mx = ((rand.nextInt(5)+1)*100) + addx;
int my = ((rand.nextInt(6)+2)*100) + addy;
DrawPanel drawPanel = new DrawPanel();
public Snake2() {
addMouseListener(new MouseListener());
System.out.print(mx + " " + my);
/* move snake up */
Action upAction = new AbstractAction(){
public void actionPerformed(ActionEvent e) {
y -=10;
if (y >= my && y <= my+9 && x >= mx && x <= mx+9)
{
addx = (rand.nextInt(10))*10;
addy = (rand.nextInt(10))*10;
mx = ((rand.nextInt(5)+1)*100) + addx;
my = ((rand.nextInt(6)+1)*100) + addy;
point += 100;
length++;
bodyY.add(0, y);
}
if (y <99)
{
new GameOver();
dispose();
}
drawPanel.repaint();
}
};
/* move snake down */
Action downAction = new AbstractAction(){
public void actionPerformed(ActionEvent e) {
y +=10;
if (y >= my && y <= my+9 && x >= mx && x <= mx+9)
{
addx = (rand.nextInt(10))*10;
addy = (rand.nextInt(10))*10;
mx = ((rand.nextInt(5)+1)*100) + addx;
my = ((rand.nextInt(6)+1)*100) + addy;
point += 100;
length++;
bodyY.add(0, y);
}
if (y > 799)
{
new GameOver();
dispose();
}
drawPanel.repaint();
}
};
/* move snake left */
Action leftAction = new AbstractAction(){
public void actionPerformed(ActionEvent e) {
x -=10;
if (x >= mx && x <= mx+9 && y >= my && y <= my+9)
{
addx = (rand.nextInt(10))*10;
addy = (rand.nextInt(10))*10;
mx = ((rand.nextInt(5)+1)*100) + addx;
my = ((rand.nextInt(6)+1)*100) + addy;
point += 100;
length++;
bodyX.add(0, x);
}
if (x <99)
{
new GameOver();
dispose();
}
drawPanel.repaint();
}
};
/* move snake right */
Action rightAction = new AbstractAction(){
public void actionPerformed(ActionEvent e) {
x +=10;
if (x >= mx && x <= mx+9 && y >= my && y <= my+9)
{
addx = (rand.nextInt(10))*10;
addy = (rand.nextInt(10))*10;
mx = ((rand.nextInt(5)+1)*100) + addx;
my = ((rand.nextInt(6)+1)*100) + addy;
point += 100;
length++;
bodyX.add(0, x);
}
if (x > 699)
{
new GameOver();
dispose();
}
drawPanel.repaint();
}
};
InputMap inputMap = drawPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = drawPanel.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
actionMap.put("rightAction", rightAction);
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
actionMap.put("leftAction", leftAction);
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "downAction");
actionMap.put("downAction", downAction);
inputMap.put(KeyStroke.getKeyStroke("UP"), "upAction");
actionMap.put("upAction", upAction);
add(drawPanel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}//Snake2()
private class GameOver extends JFrame implements ActionListener{
JLabel answer = new JLabel("");
JPanel pane = new JPanel(); // create pane object
JButton pressme = new JButton("Quit");
JButton replay = new JButton("Replay?");
GameOver() // the constructor
{
super("Game Over"); setBounds(100,100,300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane(); // inherit main frame
con.add(pane); pressme.setMnemonic('Q'); // associate hotkey
pressme.addActionListener(this); // register button listener
replay.addActionListener(this);
pane.add(answer); pane.add(pressme); pane.add(replay); pressme.requestFocus();
answer.setText("You Lose");
setVisible(true); // make frame visible
}//GameOver()
// here is the basic event handler
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == pressme)
System.exit(0);
if (source == replay)
{
dispose();
EventQueue.invokeLater(new Runnable(){
public void run(){
new Snake2();
}
});
}
}//actionPreformed
}//GameOver
private class DrawPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Font ith = new Font("Ithornît", Font.BOLD, 78);
/* Background: Snake */
g.setColor(Color.darkGray);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.gray);
g.fillRect(100,100,600,700);
g.setColor(Color.white);
g.drawRect(99,99,601,701);
g.drawString("Quit",102,86);
g.drawRect(100,70,30,20);
g.drawString("Score: ", 602, 86);
g.setFont(ith);
g.drawString("SNAKE",350,60);
/* Sprite: Mouse */
g.setColor(Color.black);
g.fillRect(mx, my, width, height);
//System.out.print(mx + " " + my);
/* Sprite: Snake Body */
if (length != 0){
for(int i = 0; i >= length; i++)
{
g.setColor(Color.darkGray);
g.fillRect(bodyX.get(i), bodyY.get(i), width, height);
}
}
/* Sprite: Snake head */
g.setColor(Color.white);
g.fillRect(x, y, width, height);
}//Paint Component
public Dimension getPreferredSize() {
return new Dimension(800, 850);
}//Dimension
}//DrawPanel
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
new Snake2();
}
});
}// main
}//Snake Class
/* Tracks where mouse is clicked */
class MouseListener extends MouseAdapter {
public void mouseReleased(MouseEvent me) {
if (me.getX() >= 101 && me.getX() <= 131 && me.getY() >= 94 && me.getY() <= 115){
System.exit(0);
}
String str="Mouse Released at "+me.getX()+","+me.getY();
System.out.println(str);
}
}//MouseAdapter
"but since it's a snake game I need the sprite to keep moving without stopping"
You need to use a javax.swing.Timer. The basic constructor is like this
Timer(int delay, ActionListener listener) // delay is in milliseconds
A basic implementation would be something like this
Timer timer = new Timer(100, new ActionListener(){
public void actionPerformed(ActionEvent e) {
// do something
}
});
timer.start();
Basically what the timer does, is fire an ActionEvent every so many milliseconds. This is how you work animation with swing. So without you pressing any keys, the sprite will move by itself. It works pretty much like a button does. When you press a button an event is fired. But in the case of the timer, the timer is the one that fires the event. You can specify any delay you want. You many even want to change the duration dynamically when a certain level is reached. timer.setDelay(someInt)
UPDATE
I made very few changes to your current code. I did pretty much what I stated in the comment below
Ok so I thought about it, and I would do something like this: Have a direction variable. For the actions, all you should do is change the direction. In the timer actionPerformed thats where all you logic should go. Check for the direction. If if equals "left" then continue going left like you would on a key press. And so on
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.ArrayList;
public class Snake2 extends JFrame {
String direction = "right";
//String duration =
/* Sprite: snake head co-ordinates */
int x = 400;
int y = 450;
int width = 10;
int height = 10;
/* Sprite: snake body */
int length = 0;
ArrayList<Integer> bodyX = new ArrayList<Integer>();
ArrayList<Integer> bodyY = new ArrayList<Integer>();
/* Score */
int point = 0;
/* Sprite: mouse co-ordinates */
Random rand = new Random();
int addx = (rand.nextInt(10)) * 10;
int addy = (rand.nextInt(10)) * 10;
int mx = ((rand.nextInt(5) + 1) * 100) + addx;
int my = ((rand.nextInt(6) + 2) * 100) + addy;
DrawPanel drawPanel = new DrawPanel();
Timer timer;
public Snake2() {
addMouseListener(new MouseListener());
timer = new Timer(50, new TimerListener()); ////////////// <<<<<<<<<<<<<<<<<< TIMER
timer.start();
System.out.print(mx + " " + my);
/* move snake up */
Action upAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
direction = "up"; ////////////// <<<<<<<<<<<<<<<<<< direction only change
}
};
/* move snake down */
Action downAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
direction = "down";
}
};
/* move snake left */
Action leftAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
direction = "left";
}
};
/* move snake right */
Action rightAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
direction = "right";
}
};
InputMap inputMap = drawPanel
.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = drawPanel.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
actionMap.put("rightAction", rightAction);
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
actionMap.put("leftAction", leftAction);
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "downAction");
actionMap.put("downAction", downAction);
inputMap.put(KeyStroke.getKeyStroke("UP"), "upAction");
actionMap.put("upAction", upAction);
add(drawPanel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}// Snake2()
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) { /////////////////// <<<<<<<<<<<<<< All logic here
if ("right".equals(direction)) {
x += 10;
if (x >= mx && x <= mx + 9 && y >= my && y <= my + 9) {
addx = (rand.nextInt(10)) * 10;
addy = (rand.nextInt(10)) * 10;
mx = ((rand.nextInt(5) + 1) * 100) + addx;
my = ((rand.nextInt(6) + 1) * 100) + addy;
point += 100;
length++;
bodyX.add(0, x);
}
if (x > 699) {
new GameOver();
dispose();
}
} else if ("left".equals(direction)) {
x -= 10;
if (x >= mx && x <= mx + 9 && y >= my && y <= my + 9) {
addx = (rand.nextInt(10)) * 10;
addy = (rand.nextInt(10)) * 10;
mx = ((rand.nextInt(5) + 1) * 100) + addx;
my = ((rand.nextInt(6) + 1) * 100) + addy;
point += 100;
length++;
bodyX.add(0, x);
}
if (x < 99) {
new GameOver();
dispose();
}
} else if ("up".equals(direction)) {
y -= 10;
if (y >= my && y <= my + 9 && x >= mx && x <= mx + 9) {
addx = (rand.nextInt(10)) * 10;
addy = (rand.nextInt(10)) * 10;
mx = ((rand.nextInt(5) + 1) * 100) + addx;
my = ((rand.nextInt(6) + 1) * 100) + addy;
point += 100;
length++;
bodyY.add(0, y);
}
if (y < 99) {
new GameOver();
dispose();
}
} else if ("down".equals(direction)) {
y += 10;
if (y >= my && y <= my + 9 && x >= mx && x <= mx + 9) {
addx = (rand.nextInt(10)) * 10;
addy = (rand.nextInt(10)) * 10;
mx = ((rand.nextInt(5) + 1) * 100) + addx;
my = ((rand.nextInt(6) + 1) * 100) + addy;
point += 100;
length++;
bodyY.add(0, y);
}
if (y > 799) {
new GameOver();
dispose();
}
}
drawPanel.repaint();
}
}
private class GameOver extends JFrame implements ActionListener {
JLabel answer = new JLabel("");
JPanel pane = new JPanel(); // create pane object
JButton pressme = new JButton("Quit");
JButton replay = new JButton("Replay?");
GameOver() // the constructor
{
super("Game Over");
timer.stop(); ////////////////////// <<<<<<<<<< Stop TIMER
setBounds(100, 100, 300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane(); // inherit main frame
con.add(pane);
pressme.setMnemonic('Q'); // associate hotkey
pressme.addActionListener(this); // register button listener
replay.addActionListener(this);
pane.add(answer);
pane.add(pressme);
pane.add(replay);
pressme.requestFocus();
answer.setText("You Lose");
setVisible(true); // make frame visible
}// GameOver()
// here is the basic event handler
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == pressme)
System.exit(0);
if (source == replay) {
dispose();
EventQueue.invokeLater(new Runnable() {
public void run() {
new Snake2();
}
});
}
}// actionPreformed
}// GameOver
private class DrawPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Font ith = new Font("Ithornît", Font.BOLD, 78);
/* Background: Snake */
g.setColor(Color.darkGray);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.gray);
g.fillRect(100, 100, 600, 700);
g.setColor(Color.white);
g.drawRect(99, 99, 601, 701);
g.drawString("Quit", 102, 86);
g.drawRect(100, 70, 30, 20);
g.drawString("Score: ", 602, 86);
g.setFont(ith);
g.drawString("SNAKE", 350, 60);
/* Sprite: Mouse */
g.setColor(Color.black);
g.fillRect(mx, my, width, height);
// System.out.print(mx + " " + my);
/* Sprite: Snake Body */
if (length != 0) {
for (int i = 0; i >= length; i++) {
g.setColor(Color.darkGray);
g.fillRect(bodyX.get(i), bodyY.get(i), width, height);
}
}
/* Sprite: Snake head */
g.setColor(Color.white);
g.fillRect(x, y, width, height);
}// Paint Component
public Dimension getPreferredSize() {
return new Dimension(800, 850);
}// Dimension
}// DrawPanel
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Snake2();
}
});
}// main
}// Snake Class
/* Tracks where mouse is clicked */
class MouseListener extends MouseAdapter {
public void mouseReleased(MouseEvent me) {
if (me.getX() >= 101 && me.getX() <= 131 && me.getY() >= 94
&& me.getY() <= 115) {
System.exit(0);
}
String str = "Mouse Released at " + me.getX() + "," + me.getY();
System.out.println(str);
}
}// MouseAdapter

Creating a snake game, when you press two directions quickly snake eats itself [Java]

I hope this makes sense, but I'm trying to make a snake-type game in Java and if you press two directions at the same time/ too fast the snake goes on top of itself making you lose.
For example if you you're going downwards, and hit right then up very fast, you get the snake going straight up on the same column and killing itself, but it should go right one then up one. If anyone can help me that'd be great, thanks!
package tk.sketchistgames.Snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
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 java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener{
/**
* Main graphical area for Snake
*/
private static final long serialVersionUID = 4085437479211945011L;
private final int WIDTH = 600;
private final int HEIGHT = 600;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 1200;
private final int RAND_POS = 59;
public static int DELAY = 90;
private int x[] = new int[ALL_DOTS];
private int y[] = new int[ALL_DOTS];
private int dots, food_x, food_y, pdown_x, pdown_y, rdouble_x, rdouble_y, powerUp_x, powerUp_y, half_x, half_y;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private boolean inGame = true;
private int score = 0;
private int fruitEaten = 0;
private boolean Bonus = false;
private boolean RDouble = false;
private boolean bpower = false;
private boolean halfpower = false;
private Timer timer;
private Image food;
private Image head;
private Image body;
private Image pdown;
private Image rdouble;
private Image powerUp;
private Image half;
public Board() {
addKeyListener(new TAdapter());
setBackground(Color.decode("0x3F919E"));
ImageIcon iid = new ImageIcon(this.getClass().getResource("/images/body.png"));
body = iid.getImage();
ImageIcon iia = new ImageIcon(this.getClass().getResource("/images/food.png"));
food = iia.getImage();
ImageIcon iih = new ImageIcon(this.getClass().getResource("/images/head.png"));
head = iih.getImage();
ImageIcon iipd = new ImageIcon(this.getClass().getResource("/images/pdown.png"));
pdown = iipd.getImage();
ImageIcon iird = new ImageIcon(this.getClass().getResource("/images/pup2.png"));
rdouble = iird.getImage();
ImageIcon iipu1 = new ImageIcon(this.getClass().getResource("/images/pup1.png"));
powerUp = iipu1.getImage();
ImageIcon iihd = new ImageIcon(this.getClass().getResource("/images/halfDown.png"));
half = iihd.getImage();
setFocusable(true);
initGame();
}
public void initGame() {
dots = 5;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z*10;
y[z] = 50;
}
locateFood();
timer = new Timer(DELAY, this);
timer.start();
}
public void checkApple() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if ((x[0] == pdown_x) && (y[0] == pdown_y)){
dots -= 1;
score -= 50;
AudioInputStream audioIn = AudioSystem.getAudioInputStream(Board.class.getResource("/sounds/powerdown.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
bpower = false;
}
if ((x[0] == half_x) && (y[0] == half_y)){
dots = dots /2;
score = score /2;
AudioInputStream audioIn = AudioSystem.getAudioInputStream(Board.class.getResource("/sounds/powerdown.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
clip.start();
halfpower = false;
}
if ((x[0] == powerUp_x) && (y[0] == powerUp_y)){
dots += 4;
score += 100;
AudioInputStream audioIn = AudioSystem.getAudioInputStream(Board.class.getResource("/sounds/powerup1.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
Bonus = false;
}
if ((x[0] == rdouble_x) && (y[0] == rdouble_y)){
dots = dots * 2;
score += 1000;
AudioInputStream audioIn = AudioSystem.getAudioInputStream(Board.class.getResource("/sounds/powerup2.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
RDouble = false;
}
if ((x[0] == food_x) && (y[0] == food_y)) {
dots++;
long r = Math.round(Math.random() * 10);
if(r == 4){
locatePowerUp();
Bonus = true;
}
long half = Math.round(Math.random() * 175);
System.out.println(half);
if(half == 89){
locateHalfDown();
halfpower = true;
}
long rdouble = Math.round(Math.random() * 100);
if(rdouble == 50){
locateDoubleUp();
RDouble = true;
}
long badpower = Math.round(Math.random() * 25);
if(badpower == 25 || badpower == 20 || badpower == 15|| badpower == 10 || badpower == 5|| badpower == 0){
locatePowerDown();
bpower = true;
}
score += (50 + fruitEaten);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(Board.class.getResource("/sounds/eat.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
fruitEaten++;
locateFood();
}
}
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
if(halfpower){
g.drawImage(half, half_x, half_y, this);
}
if(Bonus){
g.drawImage(powerUp, powerUp_x, powerUp_y, this);
}
if(RDouble){
g.drawImage(rdouble, rdouble_x, rdouble_y, this);
}
if(dots <= 0) gameOver(g);
g.setColor(Color.white);
Font small1 = new Font("arcadepix", Font.PLAIN, 20);
g.setFont(small1);
g.drawString("Score: " + score + " Food Eaten: " + fruitEaten + " Length: " + dots, 15, 15);
g.drawImage(food, food_x, food_y, this);
if(bpower){
g.drawImage(pdown, pdown_x, pdown_y, this);
}
for (int z = 0; z < dots; z++) {
if (z == 0)
g.drawImage(head, x[z], y[z], this);
else g.drawImage(body, x[z], y[z], this);
}
if(Menu.pause){
g.drawString("Paused! 'P' To unpause!", 20, 100);
for (int z = 0; z < dots; z++) {
x[z] = 50 - z*10;
y[z] = 50;
}
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}else{
gameOver(g);
}
}
public void gameOver(Graphics g) {
if(dots >= 300){
String msg = "You won!";
Font small = new Font("arcadepix", Font.PLAIN, 20);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
g.drawString("Total Score: " + score +"!", (WIDTH - metr.stringWidth(msg)) /2 - 12, (HEIGHT / 2) - 18);
g.drawString("Total Food Eaten: " + dots + "!", (WIDTH - metr.stringWidth(msg)) /2 - 72, (HEIGHT / 2) - 38);
g.drawString("Press Space to play again!", (WIDTH - metr.stringWidth(msg)) /2 - 77, (HEIGHT / 2) + 18);
setBackground(Color.red);
}else{
String msg = "Game Over";
Font small = new Font("arcadepix", Font.PLAIN, 20);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
g.drawString("Total Score: " + score, (WIDTH - metr.stringWidth(msg)) /2 - 12, (HEIGHT / 2) - 18);
g.drawString("Press Space to Continue", (WIDTH - metr.stringWidth(msg)) /2 - 77, (HEIGHT / 2) + 18);
setBackground(Color.decode("0x3F919E"));
}
}
public void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (left) {
x[0] -= DOT_SIZE;
}
if (right) {
x[0] += DOT_SIZE;
}
if (up) {
y[0] -= DOT_SIZE;
}
if (down) {
y[0] += DOT_SIZE;
}
}
public void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] > HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] > WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
}
public void locateFood() {
int r = (int) (Math.random() * RAND_POS);
food_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
food_y = ((r * DOT_SIZE));
}
public void locatePowerDown() {
int r = (int) (Math.random() * RAND_POS);
pdown_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
pdown_y = ((r * DOT_SIZE));
}
public void locateDoubleUp() {
int r = (int) (Math.random() * RAND_POS);
rdouble_x= ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
rdouble_y = ((r * DOT_SIZE));
}
public void locatePowerUp() {
int r = (int) (Math.random() * RAND_POS);
powerUp_x= ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
powerUp_y = ((r * DOT_SIZE));
}
public void locateHalfDown() {
int r = (int) (Math.random() * RAND_POS);
half_x= ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
half_y = ((r * DOT_SIZE));
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
if(Menu.pause){
}
try {
checkApple();
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
checkCollision();
move();
}
repaint();
}
public void reset(){
left = false;
right = true;
up = false;
down = false;
inGame = true;
score = 0;
fruitEaten = 0;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z*10;
y[z] = 50;
}
dots = 5;
bpower = false;
locatePowerDown();
RDouble = false;
locateDoubleUp();
locatePowerUp();
locateFood();
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE){
if(inGame){
if(Menu.pause){
Menu.pause = false;
}else if(!Menu.pause){
Menu.pause = true;
}
}
if(!inGame){
reset();
}
}
if(key == KeyEvent.VK_P){
if(Menu.pause){
Menu.pause = false;
}else if(!Menu.pause){
Menu.pause = true;
}
}
if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) && (!right)) {
left = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_RIGHT ||key == KeyEvent.VK_D) && (!left)) {
right = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_UP || key == KeyEvent.VK_W) && (!down)) {
up = true;
right = false;
left = false;
}
if ((key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) && (!up)) {
down = true;
right = false;
left = false;
}
}
}
}
Based on your description, your code is changing the boolean variables before the move occurs. A simple solution would be to store all moves in a Queue, and process them by removing them, that way you ensure you won't overwrite a move.
In other words, every time you record a key event that would change the booleans, store some sort of signal (an int, a String, an enum, etc.) in a Queue, and in your move method, simply remove the signal from the front of the Queue and process it like you process the boolean variables. If you were to use an enum for UP DOWN RIGHT LEFT it would be fairly readable and you could use a switch-case to process each movement.
ex.
switch (movement) {
case UP: /* up code */ break;
case LEFT: /* left code */ break;
case RIGHT: /* right code */ break;
case DOWN: /* down code */ break;
}
where movement is the signal you removed from the queue, and UP DOWN RIGHT LEFT are enum's (for that matter they could be int constants, but as Bloch recommends in Effective Java, prefer enum types to int constants.)
private enum Movement { UP, DOWN, RIGHT, LEFT }
This allows you to refer to these types in the switch above, and instantiate the Queue as follows:
Queue<Movement> movementQueue = new ArrayDeque<Movement>();
Which in turn means you can add whichever movement you need simply by doing the following:
movementQueue.offer(UP); // or DOWN or RIGHT or LEFT, whichever you want.
And when you're ready to use them, access them as follows:
Movement movement = movementQueue.poll();
For more info on Queues: http://docs.oracle.com/javase/6/docs/api/java/util/Queue.html
For more info on enums: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Also because you said you're new to Java, I'd recommend Head First: Java for an overhead view, and Effective Java to learn a good amount of best practices.

Java ball object doesn't bounce off of drawn rectangles like it's supposed to.

Sorry for the awful title. The purpose of the Java applet is as such: A ball is bouncing around the screen. The size and speed of this ball can be changed via scrollbars. The user can press and drag the mouse on the screen to draw rectangles. The ball will bounce off of these rectangles as well. The bounds of these rectangles are stored in a vector. When a rectangle is clicked, it (and all other rectangles at that point) are removed from the vector (and the screen).
The problem I'm having is two-fold: One, when I click a rectangle to remove it, it doesn't get removed, but that can be solved later.
Two: The ball doesn't bounce off of the rectangles like it's supposed to. When I draw a rectangle in either the same row or column as the ball, the ball bounces around inside of a tiny rectangle, like it's stuck.
Here's my code to detect if the ball is hitting the boundaries of either the applet or any of the rectangles:
public void move()
{
//if it will hit the right or left boundary, flip the x direction and set it
if (loc.x+size >= boundx || loc.x <= 0)
{ dx *= -1; }
//if it will hit the top or bottom boundray, flip the y direction and set it
if (loc.y+size >= boundy || loc.y <= 0)
{ dy *= -1; }
for (int i = 0; i < r.size(); i++)
{
temp = new Rectangle(r.elementAt(i));
int rx = temp.x;
int ry = temp.y;
int rh = temp.height;
int rw = temp.width;
//If the ball hits either side of the rectangle, change the x direction
if((loc.x > rx && loc.x > ry && loc.x < (ry + rh))||(loc.x < (rx + rw) && loc.x > rx && loc.x <(ry + rh)))
{dx *= -1;}
//If the ball hits either the top or bottom, change the y direction
if((loc.y > ry && loc.y > rx && loc.y < (rx + rw))||(loc.y < (ry + rh) && loc.y > ry && loc.y <(rx + rw)))
{dy *= -1;}
}
//Increment or decrement the location of the ball based on the X and Y directions.
loc.x += dx;
loc.y += dy;
}
If you want to view the code in its entirety, it's here: http://ideone.com/R1hpBx
Thanks in advance for all the wonderful help.
I finally found a edge detection system I like...
Basically, the magic happens here...
// Detect if we collided with any one (collision is the rectangle, bounds is us)
if (collision.intersects(bounds)) {
// Determine the intersect of the collision...
insect = collision.intersection(bounds);
// Flags...
boolean vertical = false;
boolean horizontal = false;
boolean isLeft = false;
boolean isTop = false;
// Left side...
if (insect.x == collision.x) {
horizontal = true;
isLeft = true;
// Right side
} else if (insect.x + insect.width == collision.x + collision.width) {
horizontal = true;
}
// Top
if (insect.y == collision.y) {
vertical = true;
isTop = true;
// Bottom
} else if (insect.y + insect.height == collision.y + collision.height) {
vertical = true;
}
// Technically, we can really only collide with a single edge...more or less
if (horizontal && vertical) {
// Basically, we try and give precedence to the longer edge...
if (insect.width > insect.height) {
horizontal = false;
} else {
vertical = false;
}
}
// We collided with a horizontal side...
if (horizontal) {
dx *= -1;
// Move the ball to the approriate edge so we don't get caught...
if (isLeft) {
bounds.x = collision.x - bounds.width;
} else {
bounds.x = collision.x + collision.width;
}
// We collided with a vertical side...
} else if (vertical) {
dy *= -1;
// Move the ball to the approriate edge so we don't get caught...
if (isTop) {
bounds.y = collision.y - bounds.height;
} else {
bounds.y = collision.y + collision.height;
}
}
}
Now, I only have a single obstacle, but I doubt it would take much effort to get it working with a series of obstacles... ;)
public class TestBouncingBall {
private Rectangle insect;
public static void main(String[] args) {
new TestBouncingBall();
}
public TestBouncingBall() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new BallPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BallPane extends JLayeredPane {
private Ball ball;
private Timer timer;
private Rectangle world;
public BallPane() {
// world = new Rectangle(random(400), random(400), random(100), random(100));
world = new Rectangle(100, 100, 200, 200);
ball = new Ball();
ball.setSize(ball.getPreferredSize());
ball.setLocation(10, 10);
add(ball);
timer = new Timer(16, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
ball.move(getBounds(), world);
invalidate();
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
protected int random(int max) {
return (int) Math.round(Math.random() * max);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.GRAY);
g2d.fill(world);
if (insect != null) {
g2d.setColor(Color.RED);
g2d.fill(insect);
}
g2d.dispose();
}
}
public class Ball extends JPanel {
public int maxSpeed = 10;
private BufferedImage beachBall;
private int dx = 10 - (int)Math.round(Math.random() * (maxSpeed * 2)) + 1;
private int dy = 10 - (int)Math.round(Math.random() * (maxSpeed * 2)) + 1;
private int spin = 20;
private int rotation = 0;
public Ball() {
try {
beachBall = ImageIO.read(getClass().getResource("/ball.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
setOpaque(false);
}
#Override
public Dimension getPreferredSize() {
Dimension size = beachBall == null ? new Dimension(48, 48) : new Dimension(beachBall.getWidth(), beachBall.getHeight());
size.width += 4;
size.height += 4;
return size;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (beachBall != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
int x = (getWidth() - beachBall.getWidth()) / 2;
int y = (getHeight() - beachBall.getHeight()) / 2;
AffineTransform transform = g2d.getTransform();
AffineTransform at = new AffineTransform();
at.translate(getX(), getY());
at.rotate(Math.toRadians(rotation), getWidth() / 2, getHeight() / 2);
g2d.setTransform(at);
g2d.drawImage(beachBall, x, y, this);
g2d.setTransform(transform);
g2d.dispose();
}
}
public void move(Rectangle world, Rectangle collision) {
Rectangle bounds = getBounds();
bounds.x += dx;
bounds.y += dy;
if (bounds.x < 0) {
bounds.x = 0;
dx *= -1;
}
if (bounds.y < 0) {
bounds.y = 0;
dy *= -1;
}
if (bounds.x + bounds.width > world.width) {
bounds.x = world.width - bounds.width;
dx *= -1;
}
if (bounds.y + bounds.height > world.height) {
bounds.y = world.height - bounds.height;
dy *= -1;
}
if (collision.intersects(bounds)) {
insect = collision.intersection(bounds);
boolean vertical = false;
boolean horizontal = false;
boolean isLeft = false;
boolean isTop = false;
if (insect.x == collision.x) {
horizontal = true;
isLeft = true;
} else if (insect.x + insect.width == collision.x + collision.width) {
horizontal = true;
}
if (insect.y == collision.y) {
vertical = true;
isTop = true;
} else if (insect.y + insect.height == collision.y + collision.height) {
vertical = true;
}
if (horizontal && vertical) {
if (insect.width > insect.height) {
horizontal = false;
} else {
vertical = false;
}
}
System.out.println("v = " + vertical + "; h = " + horizontal);
if (horizontal) {
dx *= -1;
if (isLeft) {
bounds.x = collision.x - bounds.width;
} else {
bounds.x = collision.x + collision.width;
}
} else if (vertical) {
dy *= -1;
if (isTop) {
bounds.y = collision.y - bounds.height;
} else {
bounds.y = collision.y + collision.height;
}
}
}
rotation += spin;
setBounds(bounds);
repaint();
}
}
}

Categories