Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2013, 12:18 AM   PM User | #1
newrecycle
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
newrecycle is an unknown quantity at this point
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..
newrecycle is offline   Reply With Quote
Old 02-08-2013, 12:18 AM   PM User | #2
newrecycle
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
newrecycle is an unknown quantity at this point
I need help and quick if you dont mind
newrecycle is offline   Reply With Quote
Old 02-08-2013, 12:19 AM   PM User | #3
newrecycle
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
newrecycle is an unknown quantity at this point
there are more files and code if you need them. so just ask if you do
newrecycle is offline   Reply With Quote
Old 02-08-2013, 12:24 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 02-08-2013, 12:30 AM   PM User | #5
newrecycle
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
newrecycle is an unknown quantity at this point
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];
				
			}

		}
	}
}
newrecycle is offline   Reply With Quote
Old 02-08-2013, 12:31 AM   PM User | #6
newrecycle
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
newrecycle is an unknown quantity at this point
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);
	}

}
newrecycle is offline   Reply With Quote
Old 02-08-2013, 01:52 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 02-08-2013, 09:22 PM   PM User | #8
newrecycle
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
newrecycle is an unknown quantity at this point
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
newrecycle is offline   Reply With Quote
Reply

Bookmarks

Tags
code, coding, game, java

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 AM.


Advertisement
Log in to turn off these ads.