Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Pls help with my game pixel draw

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    Bulgaria
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy Pls help with my game pixel draw

    What is wrong with my project?When run it gives me gray screen

    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);
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    metul4o (February 28th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    Bulgaria
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Pls help with my game pixel draw

    Okay i edit it.Can you help me now ?Тhanks in advance

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    metul4o (February 28th, 2013)

  7. #5
    Junior Member
    Join Date
    Feb 2013
    Location
    Bulgaria
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    metul4o (February 28th, 2013)

  10. #7
    Junior Member
    Join Date
    Feb 2013
    Location
    Bulgaria
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Pls help with my game pixel draw

    Ahh i dont know i will go read more about codes.Thanks anyway

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    metul4o (February 28th, 2013)

Similar Threads

  1. A Pixel Out Of Place
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 4th, 2012, 07:06 PM
  2. Can not get individual pixel values using getRGB
    By maheshchavan in forum AWT / Java Swing
    Replies: 1
    Last Post: June 17th, 2012, 11:37 AM
  3. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  4. Get Pixel
    By Ophe in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 27th, 2011, 04:24 PM
  5. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 20th, 2009, 11:30 PM