Java - Graphics pixels aren't correctly updating? - java

Right now I just wanted to get my game window shown on the screen and I thought all would be well, except for the fact that the color red isn't shown on the whole window. Not sure why, I've gone over my code a few times and can't figure anything out. This is my code:
package game;
import java.awt.BorderLayout;
import java.awt.Canvas;
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 final String GAME_NAME;
private final int GAME_WIDTH;
private final int GAME_HEIGHT;
private final int SCALE;
private boolean isRunning;
private JFrame frame;
private BufferedImage image;
private int[] pixels;
public Game(String name, int width, int aspectRatio[], int scale) {
// Variabe Initialization
this.GAME_NAME = name;
this.GAME_WIDTH = width;
this.GAME_HEIGHT = width / aspectRatio[0] * aspectRatio[1];
this.SCALE = scale;
this.image = new BufferedImage(GAME_WIDTH, GAME_WIDTH, BufferedImage.TYPE_INT_RGB);
this.pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
this.frame = new JFrame(GAME_NAME);
Init();
}
private void Init() {
// Frame Setup
setMinimumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
setMaximumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
setPreferredSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
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 void Start() {
isRunning = true;
new Thread(this).start();
}
private void Stop() {
isRunning = false;
}
#Override
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
final double ns = 1000000000d / 60d;
double delta = 0d;
int frames = 0;
int updates = 0;
while (isRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
updates++;
Update();
delta--;
}
Render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frame.setTitle(GAME_NAME + " | " + updates + " ups, " + frames + " fps");
updates = 0;
frames = 0;
}
}
}
public void Update() {
for (int y = 0; y < GAME_HEIGHT; y++) {
for (int x = 0; x < GAME_WIDTH; x++) {
pixels[x + y * GAME_WIDTH] = 0xffff0000;
}
}
}
public void Render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
requestFocus();
return;
}
Graphics g = bs.getDrawGraphics();
{
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
g.dispose();
bs.show();
}
public static void main(String[] args) {
new Game("Game", 160, new int[]{12, 9}, 1).Start();
}
}

You've accidentally passed GAME_WIDTH as the height argument of the BufferedImage constructor call:
public Game(String name, int width, int aspectRatio[], int scale) {
// Variabe Initialization
this.GAME_NAME = name;
this.GAME_WIDTH = width;
this.GAME_HEIGHT = width / aspectRatio[0] * aspectRatio[1];
this.SCALE = scale;
this.image = new BufferedImage(GAME_WIDTH, GAME_WIDTH, BufferedImage.TYPE_INT_RGB);
// ^^^^^^^^^^
this.pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
this.frame = new JFrame(GAME_NAME);
Init();
}
You probably meant to use GAME_HEIGHT:
this.image = new BufferedImage(GAME_WIDTH, GAME_HEIGHT, BufferedImage.TYPE_INT_RGB);

Related

Java - ImageIO not getting picture

I am trying to add the image sprite_sheet.png to my game project but I get these errors:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at az.rehman.gfx.SpriteSheet.<init>(SpriteSheet.java:16)
at az.rehman.game.Game.<init>(Game.java:29)
at az.rehman.game.Game.main(Game.java:127)
Here are my classes:
package az.rehman.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.getResource(path));
} catch (IOException e) {
System.out.println("SpriteSheet file exception");
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]);
}
} // constructer end
} // class end
package az.rehman.game;
import az.rehman.gfx.SpriteSheet;
import javax.swing.*;
import java.awt.*;
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 {
public 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 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("\\sprite_sheet.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);
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
public 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(ticks + " ticks, " + frames + " frames");
frames = 0;
ticks = 0;
}
}
}
public void tick() {
tickCount++;
for (int i = 0; i < pixels.length; i++) {
pixels[i] = i + tickCount;
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new Game().start();
}
}

JFrame sprite sheet black screen

So I have been trying to follow a tutorial
But it isn't working. It is a tutorial on making a sprite sheet with JFrame. I could get other things to work, but for some reason this won't. It is supposed to display a tiled image, but instead it displays a black screen.
Game.java
package ca.colescheler.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 javax.swing.JFrame;
import ca.colescheler.game.gfx.Screen;
import ca.colescheler.game.gfx.SpriteSheet;
#SuppressWarnings("unused")
public class Game extends Canvas implements Runnable {
/**
*
*/
private static final long serialVersionUID = 9086760045199246082L;
private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
public boolean running;
private static final long serialVesionUID = 1;
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;
int tickCount=1;
private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");
private Screen screen;
public static long getSerialvesionuid() {
return serialVesionUID;
}
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 void init() {
screen = new Screen(WIDTH,HEIGHT,new SpriteSheet("/sprite_sheet.png"));
}
public void tick() {
tickCount++;
}
long now = System.nanoTime();
public synchronized void stop() {
// TODO Auto-generated method stub
}
public synchronized void start() {
// TODO Auto-generated method stub
//new Thread(this).start();
running = true;
run();
frames = 1;
ticks = 1;
}
static int frames = 0;
static int ticks = 0;
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000d/60d;
long lastTimer = System.currentTimeMillis();
double delta=0;
while (running) {
init();
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
if (shouldRender) {
render();
}
if (System.currentTimeMillis() - lastTimer > 1000) {
lastTimer += 1000;
System.out.println(frames + ", " + ticks);
frames=0;
ticks=0;
}
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
screen.render(pixels, 0, WIDTH);
Graphics g = bs.getDrawGraphics();
//g.drawRect(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();
}
}
SpriteSheet.java
package ca.colescheler.game.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) {
// TODO Auto-generated catch block
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);
//AARRGGBB
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]);
}
//85*0
}
}
Screen.java
package ca.colescheler.game.gfx;
public class Screen {
public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH];
public int[] colours = new int[MAP_WIDTH * MAP_WIDTH * 4];
public int xOffset = 0;
public int yOffset = 0;
public int width;
public int height;
public SpriteSheet sheet;
public Screen(int width, int height, SpriteSheet sheet) {
this.width = width;
this.height = height;
this.sheet = sheet;
for (int i=1;i<MAP_WIDTH*MAP_WIDTH;i++) {
colours[i*4+0] = 0xff00ff;
colours[i*4+1] = 0x00ffff;
colours[i*4+2] = 0xffff00;
colours[i*4+3] = 0xffffff;
}
}
public void render(int[] pixels, int offset, int row) {
for (int yTile = yOffset>>3;yTile <-(yOffset + height) >>3; yTile++) {
int yMin = yTile * 8 - yOffset;
int yMax = yMin + 8;
if (yMin < 0) yMin = 0;
if (yMax > height) yMax = height;
for (int xTile = xOffset >>3; xTile <=(xOffset+width) >>3; xTile++) {
int xMin = xTile * 8 - xOffset;
int xMax = xMin + 8;
if (xMin < 0) xMin = 0;
if (xMax > width) xMax = height;
int tileIndex = (xTile &(MAP_WIDTH_MASK)) + (yTile & (MAP_WIDTH_MASK)) * MAP_WIDTH;
for (int y = yMin;y<yMax;y++) {
int sheetPixel = ((y+yOffset) & 7) * sheet.width + ((xMin + xOffset) & 7);
int tilePixel = offset + xMin + y * row;
for (int x=xMin; x<xMax;x++) {
int colour = tileIndex * 4 + sheet.pixels[sheetPixel++];
pixels[tilePixel++]=colours[colour];
}
}
}
}
}}
Link to tutorial
In the file Screen.java, there is a typo. You should change height to width in the following line:
if (xMax > width) xMax = height;

Created a game where the Frame's width is accurate after I run it but the height never updates

I'm learning to make games using a a tutorial on youtube. Everything seems fine except when I run the program. A frame shows up with the accurate width I want but the height looks like it sets to a default no matter what value I give it.
package ca.vanzeben.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 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; // able to move screen
public static final String NAME = "Juego";
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() { //game constructor
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); // closes game completely
frame.setLayout(new BorderLayout());
frame.add(this,BorderLayout.CENTER); //adds canvas to JFrame and centers it
frame.pack();//keeps everything sized correctly
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public synchronized void start() {//so u can start from the applet
running = true;
new Thread(this).start();
}
public synchronized void stop(){
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000.0/60; //nanoseconds per tick or per update
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0.0; //how many unprocessed nano seconds
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 + " frames " + ticks + " ticks ");
frames = 0;
ticks = 0;
}
}
}
public void tick(){ //updates the game, updates the logic
tickCount++;
for(int i =0;i<pixels.length;i++){
pixels[i] = i + tickCount;
}
}
public void render(){ //prints out ^
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args){
new Game().start();
}
}
You defined Height and used HEIGHT. Change it to:
public static final int HEIGHT = WIDTH / 12 * 9;
This is how it looked like to me:
Before:
After:

Illegal Argument Exception thrown when trying to load a sprite sheet

I have linked the file I want to load and when I debug, the SpriteSheet constructor shows that the path variable is storing the path I specify. When it trying to run the image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); line of code, it crashes with an Illegal Argument Exception. The only thing I can think is wrong is that the file I specified will not load, but I have no idea why.
Game class:
package com.swainchris.twodgame;
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;
import com.swainchris.twodgame.gfx.SpriteSheet;
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 = "2D Game";
public static boolean running = false;
private JFrame frame;
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("/SS.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);
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
public synchronized void stop() {
running = false;
}
public void tick(){
tickCount++;
for(int i = 0; i < pixels.length; i++){
pixels[i] = i - tickCount;
}
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs==null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image,0,0,getWidth(),getHeight(),null);
g.setColor(Color.BLACK);
g.fillOval(50,50,50,50);
g.dispose();
bs.show();
}
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--;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(shouldRender){
frames++;
render();
}
if(System.currentTimeMillis() - lastTimer >= 1000){
lastTimer += 1000;
frame.setTitle("2D Game! FPS: " + frames + " UPS: " + ticks);
frames = 0;
ticks = 0;
}
}
}
public static void main(String[] args) {
new Game().start();
}
}
SpriteSheet class:
package com.swainchris.twodgame.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]);
}
}
}
Error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.swainchris.twodgame.gfx.SpriteSheet.<init>(SpriteSheet.java:20)
at com.swainchris.twodgame.Game.<init>(Game.java:30)
at com.swainchris.twodgame.Game.main(Game.java:137)
Works now, all i did was copy all the code to a new project file. Didn't change anything at all besides the name of the project. No idea why this worked but it did.

Java 3d game random pixels not appearing

I have been watching tutorials on how to make a 3D game in Java using Eclipse. I have copied all the code word for word and am not getting the same result and it is very frustrating. At the moment all I am trying to do is create a small square of randomly generated pixels and all I'm getting is a blank window. This is the code and classes I am using.
Class1 = Display
package com.mime.testgame2;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import com.mime.testgame2.graphics.Render;
import com.mime.testgame2.graphics.Screen;
public class Display extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int width = 800;
public static final int height = 600;
public static String title = "3D Game Pre-Alpha 0.0.1";
private Thread thread;
private boolean running = false;
private Render render;
private Screen screen;
private BufferedImage img;
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();
}
private 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.add(game);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setTitle(title);
}
}
Class2 = Render
package com.mime.testgame2.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 y = 0; y < render.Height; y++) {
int yPix = y + yOffset;
for(int x = 0; x < render.Width; x++) {
int xPix = x + xOffset;
pixels[xPix + yPix * Width] = render.pixels[x + y * rend er.Width];
}
}
}
}
Class3 = Screen
package com.mime.testgame2.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);
}
}
Can anyone help me?

Categories

Resources