Pls help with my game pixel draw
What is wrong with my project?When run it gives me gray screen :confused:
Code java:
First Class--------------
package com.mime.minefront;
import java.awt.Canvas;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.Graphics;
import javax.swing.JFrame;
import com.mime.minefron.graphics.Render;
import com.mime.minefron.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 final String TITLE = "GAME Fazkyy Pre-Alpha 0.0.1";
private Thread thread;
private Screen screen;
private BufferedImage img;
private Render render;
private boolean running = false;
private int[] pixels;
private 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();
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.add(game);
frame.pack();
frame.setTitle(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
System.out.println("Running...");
game.start();
}
}
Second Class-------------
package com.mime.minefron.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*render.width];
}
}
}
}
Third Class------
package com.mime.minefron.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, 250, 250);
}
}
Re: Pls help with my game pixel draw
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Pls help with my game pixel draw
Okay i edit it.Can you help me now ?Тhanks in advance :)
Re: Pls help with my game pixel draw
Are the methods being called that do the drawing? Try debugging the code by adding some calls to the println method that print out messages when the various methods are called so you can see when or if the methods are called.
Re: Pls help with my game pixel draw
I just wached tutorial in youtube and i fix all bug and still nothing.Im novice in java programing so i need little help.
Re: Pls help with my game pixel draw
Did you try adding the println methods as I suggested?
Were the methods being called that do the drawing?
What will cause the methods to be called so they will do the drawing?
Re: Pls help with my game pixel draw
Ahh i dont know i will go read more about codes.Thanks anyway :)
Re: Pls help with my game pixel draw
That will be a good idea. You need to understand how a program works and what all the classes and methods do if you want to write a program and have it work correctly.
If you are just copying someone else's code, you are not learning much.
One thing you need to learn is how to debug code that doesn't do what you expect.
For debugging I add println statements to code to see where it is executing and how the values of variables change as the code executes.