Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question im trying to make a new game but the code i have so far does not work i plz help

    Code:
     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 java.awt.image.DataBufferInt;
    import com.mime.game.graphics.Render;
    import com.mime.game.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 = "Epicness Game of Awesome PreAlpha 0.01";
    
    	private Thread thread;
    	private BufferedImage img;
    	private boolean running = false;
    	private Screen screen;
    	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 Fine");
    	}
    
    	public void stop() {
    		if (!running)
    			return;
    		running = false;
    		try {
    			thread.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    			System.exit(0);
    		}
    
    	}
    
    	public void run() {
    		while (!running) {
    
    		}
    
    	}
    
    	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];
    			System.out.println("Graphics up :)");
    		}
    
    		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.setResizable(false);
    		frame.setVisible(true);
    
    		System.out.println("Running...");
    		game.start();
    
    	}
    
    	public Render getRender() {
    		return render;
    	}
    
    	public void setRender(Render render) {
    		this.render = render;
    	}
    
    }
    I am new at this so go easy on me
    Last edited by Fou-Lu; 02-08-2013 at 12:25 AM.

  • #2
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I need help and quick if you dont mind

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there are more files and code if you need them. so just ask if you do

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This isn't the correct forum. And three posts in two minutes is too many. Moving to Java forum.
    What is the error you are having? You mentioned you are new at this but you have a GUI and threading in this. Are you familiar with the basics of java?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i am fimiliar with the basics yes and im using eclipse. the problem is is that im at least trying to make somthing in the window i have made but the problem is is nothing appiers at all...

    in case you need the other code
    Code:
    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 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];
    				
    			}
    
    		}
    	}
    }

  • #6
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    even more code...
    Code:
    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);
    	}
    
    }

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Okay, so lets look at what you are doing here.
    In the main, you issue a game.start() which creates a new thread of this and issues a start command on the thread. The main thread is now returned to the main as the game thread issues a run.
    In this run, it's job is to loop while !running, but perform no actions. Since the game is running, this immediately stops this thread's run. This thread is now terminated. Complete control is now just the main thread, which is sitting idle on the main. The application is complete awaiting input for the gui.

    So if you are looking to make this do something, you'll need to write what it needs to do in the run method. You'll also likely need to write the entire paint method for the canvas.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh you mean like this:
    Code:
    package com.mime.game;
    
    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.game.graphics.Render;
    import com.mime.game.graphics.Screen;
     
    public class Display extends Canvas implements Runnable
    {
     
    
     
            public static final int WIDTH = 800;
            public static final int HEIGHT = 600;
            public static final String Title = "Graphics Try pre-alpha 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;
                    try {
                            thread = new Thread(this);
                            thread.start();
                            System.out.println("Starting");
                    } catch (Exception e) {
                            e.printStackTrace();
                            System.exit(0);
                    }
     
            }
     
            public void run()
            {
                    while (running) {
                            tick();
                            render();
                    }
            }
     
            private void stop()
            {
                    if (!running) {
                            return;
     
                    }
     
                    running = false;
                    try {
                            thread.join();
                    } catch (Exception e) {
                            e.printStackTrace();
                            System.exit(0);
                    }
            }
     
            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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(WIDTH, HEIGHT);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
    		frame.setTitle("Game Title v0.01");
    
    		System.out.println("Running...");
    		game.start();
    
    	}
    
    }
    it works now thanks alot


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •