Jump to content

Ok for some reason when I was following along with a tutorial on developing games for java I did something wrong. The height of the frame/ canvas not sure which is very short.

 

Here is the code that I have so far, it is displaying something to to make sure it worked.

package FusedLynx;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 static final long serialVersionUID = 1L;		public static final int WIDTH = 160; 	public static final int HEIGTH = WIDTH / 4 * 3; 	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();		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();	}}

Thanks for the help :)

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/
Share on other sites

Link to post
Share on other sites

Just a quick response to clarify.

 

Your Width = 160

but with your formula for height, Height = 120

That seems small to me...so maybe the formula needs adjusting or Width increased

It scales up by 3, making it 480x360 and if I change the width to a larger number only the width changes even though the height equation includes the width. Also changing the height does nothing either, it must be something somewhere else in the code.

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-734004
Share on other sites

Link to post
Share on other sites

It scales up by 3, making it 480x360 and if I change the width to a larger number only the width changes even though the height equation includes the width. Also changing the height does nothing either, it must be something somewhere else in the code.

 

Ok, sorry I haven't read all the code...I read through it quickly.

 

From what I see and have googled about the the Canvas class, you probably have to add

setSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

 

I know you set the max and min, but that doesn't necessarily run the check the size of the window is currently correct.

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-734711
Share on other sites

Link to post
Share on other sites

I'm not even sure how you got it to compile

 

but :

    public static final int HEIGTH = WIDTH / 4 * 3;

You miss-spelled Height here. 

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-735065
Share on other sites

Link to post
Share on other sites

It happens all the time, don't worry, :D

 

lol ....

A few years back when I was assisting in a programming introduction course I often searched for a lot of those small stupid mistakes.

I don't remeber anymore how many times I had to change the capital 'i' to a small 'l' in System.out.println ...

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-739767
Share on other sites

Link to post
Share on other sites

lol ....

A few years back when I was assisting in a programming introduction course I often searched for a lot of those small stupid mistakes.

I don't remeber anymore how many times I had to change the capital 'i' to a small 'l' in System.out.println ...

 

You see, the problem here is that java is just stupid like that, :P 

cout << "Java sucks!"

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-739802
Share on other sites

Link to post
Share on other sites

yeah ... because shift operators are something a programming noob will unterstand ;)

 

:| Obviously you have never used C++ at all..

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-739888
Share on other sites

Link to post
Share on other sites

I have ;) as well as many many other programming languages ...

 

Then you would have noticed that wasn't bitshifting (how on earth do you bitshift by passing the number of bits to shift as a string?) , it was the stringstream insert formatted output operator (and its counterpart extract formatted input) 

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-739990
Share on other sites

Link to post
Share on other sites

Then you would have noticed that wasn't bitshifting

 

Ofcourse you are not bitshifting, but still the bit-shift operator is overloaded

In a OO language an approach like System.out.println() imho is more consistent.

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
https://linustechtips.com/topic/54519-java-debug-help/#findComment-740028
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×