my 3 programs seem to work alone but when I try to run them together It seems to give me an out of bounds error but I cant tell where exactly if anyone can tell me where exactly it is and how to fix the error that would be great, I wanted to use a debugger but im having some problems getting it to work.
Here is my Code for the 3 Classes :
Render.java
package com.mime.Game.graphics;
public class Render {
public final int width;
public final int height;
public final int[] pixels;
public Render(int width, int height) {
this.width = width;
this.height = height;
pixels = new int[width * height];
}
public void draw(Render render, int xOffset, int yOffset) {
for(int x = 0; x < this.height; x++) {
int yPix = x + yOffset;
for(int y = 0; y < this.width; y++) {
int xPix = y + xOffset;
pixels[xPix + yPix * width] = render.pixels[x + y *
render.width];
}
}
}
}
Display.java
package com.mime.Game;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import com.mime.Game.graphics.Render;
import com.mime.Game.graphics.Screen;
import java.awt.image.DataBufferInt;
public class Display extends Canvas implements Runnable{
public static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
private Thread thread;
private Screen screen;
private BufferedImage img;
private boolean running = false;
private Render render;
private int[] pixels;
public Display() {
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}
private void start() {
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
System.out.println("Working");
}
public void stop() {
if(!running)
return;
running = false;
try{
thread.join();
}catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public void run() {
while(running) {
tick();
render();
}
}
private void tick() {
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
screen.render();
for(int i = 0; i < WIDTH * HEIGHT; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
Display game = new Display();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setSize(WIDTH, HEIGHT);
frame.add(game);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Chronicles of Walshy Alpha 0.01");
game.start();
}
}
screen.java :
package com.mime.Game.graphics;
import java.util.Random;
public class Screen extends Render{
private Render test;
public Screen(int width, int height) {
super(width, height);
Random random = new Random();
test = new Render(256, 256);
for(int i = 0; i < 256*256; i++) {
test.pixels[i] = random.nextInt();
}
}
public void render() {
draw(test, 0, 0);
}
}
In your Render.draw method you copy pixels from another Render object and you use this.width and this.height to access those pixels. This will fail when the other Render object's pixel size is smaller, which is the case in your code. You get an ArrayOutOfBoundsException when reading from render.pixels[x + y * render.width] because you're trying to read pixels from a 256*256 array as if it was a 800*600 array.
Additionally if you ever call draw with offset values, you will get another ArrayOutOfBoundsException, because you're simply adding the offsets to your x and y values without checking if the resulting index is too big when writing to pixels[xPix + yPix * width].
To fix this, you have to skip any value that goes beyond the width or height of either pixels array:
if (xPix > this.width || yPix > this.height || x > render.width || y > render.height)
{
continue;
}
I'm having an issue with my 2D game that I am following a tutorial for. I've scanned my code several times and cannot seem to resolve it. Sorry if my question sounds stupid, but I do welcome any help whatsoever.
Here's my source:
package game;
import gfx.SpriteSheet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable
{
private static final long serialVersionUD = 1L;
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH/12*9;
public static final int SCALE = 3;
public static final String NAME = "Game";
private JFrame frame;
public boolean running = false;
public int tickCount = 0;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
private SpriteSheet spriteSheet = new SpriteSheet("SpriteSheet.png");
public Game()
{
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private synchronized void start()
{
running = true;
new Thread(this).start();
}
private synchronized void stop()
{
running = false;
}
public void run()
{
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;
while(delta >= 1)
{
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException ex) {
}
if(shouldRender)
{
frames++;
render();
}
if(System.currentTimeMillis() - lastTimer >= 1000)
{
lastTimer += 1000;
System.out.println(frames + ", " + ticks);
frames = 0;
ticks = 0;
}
}
}
//Updates all of internal variables and logic and stuff.
public void tick()
{
tickCount++;
for(int i = 0; i < pixels.length; i++)
{
pixels [i] = i + tickCount;
}
}
//prints the updated logic.
public void render()
{
BufferStrategy bs = getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
//Main method
public static void main(String[]args)
{
new Game().start();
}
}
package gfx;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet
{
public String path;
public int width;
public int height;
public int[] pixels;
public SpriteSheet(String path)
{
BufferedImage image = null;
try
{
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e)
{
e.printStackTrace();
}
if(image == null)
{
return;
}
this.path = path;
this.width = image.getWidth();
this.height = image.getHeight();
pixels = image.getRGB(0, 0, width, height, null, 0, width);
for(int i = 0; i < pixels.length; i++)
{
pixels[i] = (pixels[i] & 0xff / 64);
}
for(int i = 0; i < 8; i++)
{
System.out.println(pixels[i]);
}
}
}
And here's a link to my video tutorial I'm using: Tutorial
I'm using eclipse on a 64 bit operating system(Windows).
Again, any help would be greatly appreciated, and thank you.
EDIT:
Error message:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at gfx.SpriteSheet.<init>(SpriteSheet.java:21)
at game.Game.<init>(Game.java:34)
at game.Game.main(Game.java:144)
Sorry, forgot error message. XD
The error message is telling you what's wrong: Your image path is wrong. The image path must be given relative to the class files location.
I'm currently trying to draw pixels to my window and I seem to come across and index out of bounds error when the program starts. I honestly have no idea how to fix it. Can anybody tell me where the problem is? I've checked plenty of times but it seems like it should be just fine to me. I've given all the code for my project below, but the error is on the line in the render method (Game class) where the for loop is.
EDIT:
The error is somewhere in the for loop in the Game class. it says
java.lang.ArrayIndexOutOfBoundsException: 48601" at the line
"pixles[i] = screen.pixels[i]"
Screen Class
public class Screen {
private int width,height;
public int[] pixels;
public Screen(int width,int height){
this.width = width;
this.height = height;
pixels = new int[width * height];
}
public void render(){
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
pixels[x + y * width] = 0xff00ff;
}
}
}
}
Game Class
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private int width = 300;
private int height = width / 16 * 9;
private int scale = 3;
private Thread thread;
private boolean running = false;
private JFrame frame;
private BufferedImage image = new BufferedImage(width * scale, height * scale,BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
private Screen screen;
public Game(){
screen = new Screen(width,height);
frame = new JFrame("Relm Of The Mad God Clone");
this.setPreferredSize(new Dimension(width * scale, height * scale));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
start();
}
public synchronized void start(){
thread = new Thread(this,"Running Thread");
thread.start();
running = true;
}
public synchronized void stop(){
try{
thread.join();
running = false;
}catch (InterruptedException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Game g = new Game();
}
private void update(){
}
private void render(){
BufferStrategy buffer = getBufferStrategy();
if (buffer == null){
createBufferStrategy(3);
return;
}
screen.render();
for (int i = 0; i < pixels.length; i++){
pixels[i] = screen.pixels[i];
}
Graphics g = buffer.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(),this);
g.dispose();
buffer.show();
}
#Override
public void run() {
while (running){
update();
render();
}
}
}
The screen variable is initialized with new Screen(width, height), so it probably has width*height pixels. On the other hand, the pixels variable is initialized from image, which has width*scale * height*scale pixels.
Since scale=3, this means that the pixels array is 3 times longer than the screen array. That is why you are getting the exception.
i was making a program, game in specific, so i started with basic things, but when i try to test it it drops out weird errors which doesn't appear in code, so i suppose my code is fine, but i don't know what could be causing this. :(
This is the error:
Exception in thread "main" java.lang.NullPointerException
at java.awt.image.BufferedImage.<init>(Unknown Source)
at ca.hawk.game.Game.<init>(Game.java:33)
at ca.hawk.game.Game.main(Game.java:126)
And here's the code (package):
package ca.hawk.game;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.IndexColorModel;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 3;
public static final String NAME = "Game";
private static final int BufferedImage = 0;
private static final IndexColorModel TYPE_INT_RGB = null;
private JFrame frame;
public boolean running = false;
public int tickCount = 0;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage, TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
public Game(){
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
private synchronized void stop(){
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while (running){
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while (delta >= 1){
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (shouldRender){
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer > 1000){
lastTimer += 1000;
System.out.println(frames +", "+ ticks);
frames = 0;
ticks = 0;
}
}
}
public void tick(){
tickCount++;
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public static void main(String[] args){
new Game().start();
}
}
change :
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage, TYPE_INT_RGB);
to:
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage, BufferedImage.TYPE_INT_ARGB);
you set a varialbe TYPE_INT_RGB that is null, so when you make a buffered image it throws null pointer exception.
TYPE_INT_RGB is a public static final variable on BufferedImage. Public means you can access it in your object, static means its a class variable not an object variable and final means its ts always the same value e.g if its 10 it will always be 10.
with BufferedImage.TYPE_INT_RGB you can access it.
I have made a simple 2D state change game using Bucky's slick Java tutorials, I modified this game and now want to set collisions on the map so that my player can not go through the house on the map. I think I kind of have a idea of how collisions work:
you make 2 rectangles using the following code:
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
1 for the player and 1 for the obstacle, how would I put this into my code and how would I tell java that the rectangle for the obstacle is different to the player rectangle?
Then after making the 2 rectangles I would set up a if statement saying something like if intersects do this...
Hopefully after this I think it would work. Some more information on the game, it being a state change game it has a few methods, methods like init, render and update (where do I place my rectangles and if statements, in the update method?) also, its a overhead view game kind of like pokemon if that helps. If you require my code please ask, I did not want to put it on now to overcrowd this post.
Edit1:
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState{
Animation bucky, movingUp, movingDown, movingLeft, movingRight, movingBL, movingBR, movingFL, movingFR;
Image worldMap;
boolean quit = false;//gives user to quit the game
int[] duration = {200, 200};//how long frame stays up for
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
worldMap = new Image("res/world.png");
Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation
Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")};
Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")};
Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")};
Image[] walkBL = {new Image("res/bl.png"), new Image("res/bl.png")};
Image[] walkBR = {new Image("res/br.png"), new Image("res/br.png")};
Image[] walkFL = {new Image("res/fl.png"), new Image("res/fl.png")};
Image[] walkFR = {new Image("res/fr.png"), new Image("res/fr.png")};
movingUp = new Animation(walkUp, duration, false);
movingDown = new Animation(walkDown, duration, false);
movingLeft = new Animation(walkLeft, duration, false);
movingRight = new Animation(walkRight, duration, false);
movingBL = new Animation(walkBL, duration, false);
movingBR = new Animation(walkBR, duration, false);
movingFL = new Animation(walkFL, duration, false);
movingFR = new Animation(walkFR, duration, false);
bucky = movingDown;//facing screen initially on startup
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
bucky.draw(shiftX, shiftY);//makes him appear at center of map
g.drawString("Suraj's X: "+buckyPositionX+"\nSuraj's Y: "+buckyPositionY,400,20);//tells us the position
if(quit==true){
g.drawString("Resume(R)", 250, 100);
g.drawString("Main(M)", 250, 150);
g.drawString("Quit Game(Q)", 250, 200);
if(quit==false){
g.clear();//wipe off everything from screen
}
}
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
//up
if(input.isKeyDown(Input.KEY_UP)){
bucky = movingUp;//changes the image to his back
buckyPositionY += 10;;//increase the Y coordinates of bucky (move him up)
if(buckyPositionY>162){//if I reach the top
buckyPositionY -= 10;//stops any further movement in that direction
}
}
//down
if(input.isKeyDown(Input.KEY_DOWN)){
bucky = movingDown;
buckyPositionY -= 10;
if(buckyPositionY<-600){
buckyPositionY += 10;//basically change the direction if + make -
}}
//left
if(input.isKeyDown(Input.KEY_LEFT)){
bucky = movingLeft;
buckyPositionX += 10;
if(buckyPositionX>324){
buckyPositionX -= 10;//delta * .1f
}}
//right
if(input.isKeyDown(Input.KEY_RIGHT)){
bucky = movingRight;
buckyPositionX -= 10;
if(buckyPositionX<-840){
buckyPositionX += 10;
}}
//2 key combos start here
if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_UP)){
bucky = movingBR;
buckyPositionX -= delta * .1f;
if(buckyPositionX<-840){
buckyPositionX += delta * .1f;
if(buckyPositionY>162){
buckyPositionY -= delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_UP)){
bucky = movingBL;
buckyPositionX -= delta * .1f;
if(buckyPositionX>324){
buckyPositionX -= delta * .1f;
if(buckyPositionY>162){
buckyPositionY -= delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_DOWN)){
bucky = movingFR;
buckyPositionX -= delta * .1f;
if(buckyPositionY<-600){
buckyPositionY += delta * .1f;
if(buckyPositionX<-840){
buckyPositionX += delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_DOWN)){
bucky = movingFL;
buckyPositionX -= delta * .1f;
if(buckyPositionY<-600){
buckyPositionY += delta * .1f;
if(buckyPositionX>324){
buckyPositionX -= delta * .1f;
}}}
//escape
if(input.isKeyDown(Input.KEY_ESCAPE)){
quit=true;
}
//when the menu is up
if(quit==true){//is the menu on the screen
if(input.isKeyDown(Input.KEY_R)){
quit = false;//resumes the game, makes menu dissapear
}
if(input.isKeyDown(Input.KEY_M)){
sbg.enterState(0);//takes you to the main menu
}
if(input.isKeyDown(Input.KEY_Q)){
System.exit(0);//quits the game
}
}
}
public int getID(){
return 1;
}
}
This is my Play class, the only other 2 class's I have are the main and the menu, I cant imagine the rectangle methods being made in the main or menu class so the only one left is the Play, but I dont understand how to make 2 different Rectangles (one for the player the other for the house) in the code I have done so far. If you require my main and my menu class please tell me.
Edit 3:
I have tried what you have said and made a Rectanglebase class and put the if sattement you had posted inside there but am getting errors, it is asking me to make a method for getX and getY in my player class also the public in fornt of the constructor also has a error:
public Rectanglebase{}//the public is saying syntax error
I also made a Home and Player class like you had said but am a bit confused on what I need to put in it, I put under the Home class:
return Rectangle(100,100,100,100);
but am getting errors, I am not sure if I did this correct or not.
Also, in the player class for the x,y positions how would I set my float variables from my Play class for my player?
Here is an example of a Game Loop / Game Logic and collision detection via Rectangle2D#intersects(..) method .
It uses JPanel to draw everything on and Rectangle2D is used for Entity class (which is any object needed to be drawn to GamePanel which is our JPanel where everything is drawn).
The updateGame() method is where you will find the collision checking:
private void updateGame() {
if (entities.get(0).intersects(entities.get(1))) {
System.out.println("Intersecting");
}
....
}
You are Player 1 and move with W,A,S,D. When you intersect Player 2, a println() will confirm the intersection.
GameLogic.java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
/**
*
* #author David Kroukamp
*/
public class GameLogic {
public GameLogic() {
initComponents();
}
final GamePanel gp = new GamePanel(500, 500);
private void initComponents() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Entity entity1 = new Entity(100, 100, 100, 100, createWhiteImage());
Entity entity2 = new Entity(300, 200, 100, 100, createBlackImage());
gp.addEntity(entity1);
gp.addEntity(entity2);//just a standing still JPanel
setGamePanelKeyBindings(gp, entity1);
frame.add(gp);
frame.pack();
frame.setVisible(true);
//start the game loop which will repaint the screen
runGameLoop();
}
//Starts a new thread and runs the game loop in it.
public void runGameLoop() {
Thread loop = new Thread(new Runnable() {
#Override
public void run() {
gp.running.set(true);
gp.gameLoop();
}
});
loop.start();
}
private void setGamePanelKeyBindings(GamePanel gp, final Entity entity) {
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "D pressed");
gp.getActionMap().put("D pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.RIGHT = true;
}
});
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "A pressed");
gp.getActionMap().put("A pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.LEFT = true;
}
});
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "W pressed");
gp.getActionMap().put("W pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.UP = true;
}
});
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "S pressed");
gp.getActionMap().put("S pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.DOWN = true;
}
});
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released D"), "D released");
gp.getActionMap().put("D released", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.RIGHT = false;
}
});
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released A"), "A released");
gp.getActionMap().put("A released", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.LEFT = false;
}
});
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released W"), "W released");
gp.getActionMap().put("W released", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.UP = false;
}
});
gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released S"), "S released");
gp.getActionMap().put("S released", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
entity.DOWN = false;
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GameLogic();
}
});
}
private BufferedImage createWhiteImage() {
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, img.getWidth(), img.getHeight());
return img;
}
private BufferedImage createBlackImage() {
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, img.getWidth(), img.getHeight());
return img;
}
}
class Entity extends Rectangle2D.Double {
private int speed = 5;
public boolean UP = false,
DOWN = false,
LEFT = false,
RIGHT = false;
private final BufferedImage image;
public Entity(int x, int y, int width, int height, BufferedImage image) {
super(x, y, width, height);
this.width = width;
this.height = height;
this.image = image;
}
public BufferedImage getImage() {
return image;
}
public void move() {
if (UP) {
y -= speed;
}
if (DOWN) {
y += speed;
}
if (LEFT) {
x -= speed;
}
if (RIGHT) {
x += speed;
}
}
}
class GamePanel extends JPanel {
private int width, height;
private int frameCount = 0;
private int fps = 0;
public static AtomicBoolean running = new AtomicBoolean(false), paused = new AtomicBoolean(false);
final ArrayList<Entity> entities = new ArrayList<>();
GamePanel(int w, int h) {
super(true);
setIgnoreRepaint(true);//mustnt repaint itself the gameloop will do that
setLayout(null);
width = w;
height = h;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
public void addEntity(Entity e) {
entities.add(e);
}
//Only run this in another Thread!
public void gameLoop() {
//This value would probably be stored elsewhere.
final double GAME_HERTZ = 30.0;
//Calculate how many ns each frame should take for our target game hertz.
final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
//At the very most we will update the game this many times before a new render.
//If you're worried about visual hitches more than perfect timing, set this to 1.
final int MAX_UPDATES_BEFORE_RENDER = 5;
//We will need the last update time.
double lastUpdateTime = System.nanoTime();
//Store the last time we rendered.
double lastRenderTime = System.nanoTime();
//If we are able to get as high as this FPS, don't render again.
final double TARGET_FPS = 60;
final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
//Simple way of finding FPS.
int lastSecondTime = (int) (lastUpdateTime / 1000000000);
while (running.get()) {
double now = System.nanoTime();
int updateCount = 0;
if (!paused.get()) {
//Do as many game updates as we need to, potentially playing catchup.
while (now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER) {
updateGame();
lastUpdateTime += TIME_BETWEEN_UPDATES;
updateCount++;
}
//If for some reason an update takes forever, we don't want to do an insane number of catchups.
//If you were doing some sort of game that needed to keep EXACT time, you would get rid of this.
if (now - lastUpdateTime > TIME_BETWEEN_UPDATES) {
lastUpdateTime = now - TIME_BETWEEN_UPDATES;
}
drawGame();
lastRenderTime = now;
//Update the frames we got.
int thisSecond = (int) (lastUpdateTime / 1000000000);
if (thisSecond > lastSecondTime) {
System.out.println("NEW SECOND " + thisSecond + " " + frameCount);
fps = frameCount;
frameCount = 0;
lastSecondTime = thisSecond;
}
//Yield until it has been at least the target time between renders. This saves the CPU from hogging.
while (now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES) {
//allow the threading system to play threads that are waiting to run.
Thread.yield();
//This stops the app from consuming all your CPU. It makes this slightly less accurate, but is worth it.
//You can remove this line and it will still work (better), your CPU just climbs on certain OSes.
//FYI on some OS's this can cause pretty bad stuttering. Scroll down and have a look at different peoples' solutions to this.
//On my OS it does not unpuase the game if i take this away
try {
Thread.sleep(1);
} catch (Exception e) {
}
now = System.nanoTime();
}
}
}
}
private void drawGame() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
repaint();
}
});
}
private void updateGame() {
if (entities.get(0).intersects(entities.get(1))) {
System.out.println("Intersecting");
}
for (Entity e : entities) {
e.move();
}
}
#Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
Graphics2D g2d = (Graphics2D) grphcs;
applyRenderHints(g2d);
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, getWidth(), getHeight());
for (Entity e : entities) {
g2d.drawImage(e.getImage(), (int) e.getX(), (int) e.getY(), null);
}
g2d.setColor(Color.BLACK);
g2d.drawString("FPS: " + fps, 5, 10);
frameCount++;
}
private final static RenderingHints textRenderHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
private final static RenderingHints imageRenderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
private final static RenderingHints colorRenderHints = new RenderingHints(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
private final static RenderingHints interpolationRenderHints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
private final static RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
public static void applyRenderHints(Graphics2D g2d) {
g2d.setRenderingHints(textRenderHints);
g2d.setRenderingHints(imageRenderHints);
g2d.setRenderingHints(colorRenderHints);
g2d.setRenderingHints(interpolationRenderHints);
g2d.setRenderingHints(renderHints);
}
}
if(house.getBounds().contains(player.getX(),player.getY()){//do something}
as long as your house and your player rectangles are defined in different classes, java will be able to tell the difference
create a class first that is a base class for dealing with rectangles:
public class Rectanglebase{
public void getBounds(){//write method}
//write other methods you will need to use for both rectangles here
public Rectanglebase{//default constructor}
}//end class definition
now write classes for the house and player:
public class House extends Rectanglebase{
//getBounds() is inherited, so just write stuff to do with the graphics of the house here
}
when you generate the house in your main code, you can make your own:
House house = new House();
then generate the class for the player in a similar way, then construct in in your main code:
Player player = new Player()
house and player are different variables, this is how java will tell the difference between your house and player