Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By jim829

Thread: A few questions about this code

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Rep Power

    Default A few questions about this code

    I am following a game tutorial and understand most of it. I will post the code and then tell you the problems so that maybe you can help.
    Java Code:
    	package game;
    	import java.awt.BorderLayout;
    	import java.awt.Canvas;
    	import java.awt.Color;
    	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 HEIGHT = WIDTH/12*9;
    		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.setLayout(new BorderLayout());
    		private synchronized void start() {
    			running = true;
    			new Thread(this).start();
    		private synchronized void stop() {
    			running = false;
    		public void run() {
    			long lastTime = System.nanoTime();
    			double nsPerTick = 1000000000.0/60.0;
    			int frames = 0;
    			int ticks = 0;
    			long lastTimer = System.currentTimeMillis();
    			double delta = 0;
    				long now = System.nanoTime();
    				delta +=(now-lastTime)/nsPerTick;
    				lastTime = now;
    				boolean shouldRender = true;
    					shouldRender = true;
    				}catch(InterruptedException e){
    					System.out.println(ticks +" ticks, frames "+frames);
    					frames = 0;
    					ticks = 0;
    		public void tick(){
    			for(int i = 0; i<pixels.length;i++){
    				pixels[i] = i+tickCount;
    		public void render(){
    			BufferStrategy bs = getBufferStrategy();
    			if(bs == null){
    			Graphics g = bs.getDrawGraphics();
    		public static void main(String[] args){
    			new Game().start();
    I am having trouble with the following:

    An animation is being played when you run the program, and I know the reason for the animation is the for loop that sets the array
    pixels[i] = i + tickCount, but I do not understand in any way how the pixels[] array relates to the BufferedImage that is actually being drawn in any way. My question is why is the pixels array the one that makes the animation when it is not even being drawn?

    Second: Why do we need a bufferstrategy and what exactly is it doing?

    Third: The line that sets pixels[] equal to the dataBufferInt.getData(), can someone please explain this? I know it is setting it equal to the array returned by getData(), but why are we doing this?

    Last:Does the while(delta>=1) loop check if it has been a second and then update?

    Thanks for bearing with my long question !!!

  2. #2
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Rep Power

    Default Re: A few questions about this code

    This is the tutorial:

    Setting up the JFrame - YouTube

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: A few questions about this code

    Yep, its the youtube-effect. You see a video and you think it is so wonderful, yet what did you end up with? Only more questions since the video only shows you where to click and doesn't explain you the how why and when. Tutorials are great as a guideline, but they're piss-poor as a learning tool for novices. Pardon my language, but I simply do not want to nuance it.

    Your questions are very valid, I suggest you direct them to Google and perform proper research to find out the anwsers. Watching videos is not it, that's what you could do AFTER you have done the research and can already fill in the blanks.

    Final note: I wonder if you understood the tutorial correctly since the code is mixing Swing (JFrame) with AWT (Canvas) and manual triple buffering (BufferStrategy); I would expect the JFrame to actually be an AWT Frame. If you did follow it correctly, the tutorial is poor to begin with. And that would be the tutorial-effect; they tend to be written haphazardly by people who are quite new to the material themselves, excited about it and feel a need to share before they're ready.
    kneitzel likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: A few questions about this code


    the pixels array are just another reference to the data of the buffered image. If you check the initialisation, then you see, that they are the data of the raster of the image.
    (But I don't understand the reasons for that code in detail.)

    The BufferStrategy defines, how the data is shown to the screen. You started another thread that is modifying the picture that is drawn. But you want the GUI to only display pictures that are drawn correctly already. So with a buffer strategy you can make sure that the picture that is modified is not the picture that is shown on the screen. Instead you modify a buffer and when you are done, it is used to be displayed.
    Just read the documentation of BufferStrategy: BufferStrategy (Java Platform SE 7 )

    Your third question is regarding the image and pixels which is more or less the same as the first one. The core is: The image is of type IntRGB so it holds Integer values for all pixels. And then the Pixels are just a reference to these Integer Values. So changinging pixels also changes the picture.

    And the logic is different. You define how many ns you want per tick. The code uses 1/60 second for the tick but you can change that.
    And then the code is simply performing its data manipulation multiple times (If it has the time for it)


  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: A few questions about this code

    Also, I do not believe the author fully understands some of the issues he is dealing with. Here are a couple of examples. What is the purpose of the field 'running'? Will it ever change during the context of the game? running should probably be declared volatile as a warning to the compiler to not optimize the code which might affect the behavior of the while(running) loop in a mult-threaded environment.

    What about the local variable 'shouldRender'? It never changes. So why have it at all?

    Why not just use JPanel and paintComponent? I eliminated the buffer strategy stuff and some other what appeared to be non-essential code and everything works. And as was previously said you should not mix AWT and Swing.

    This code might be useful to learn about 'how to create an interesting background.' But it should not be used as a guide on how to program.

    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Code Signing Certificate DigiCert and Other Questions
    By ElvisMachuca in forum Advanced Java
    Replies: 4
    Last Post: 04-06-2014, 03:46 PM
  2. Questions about the idea of the code
    By Innocent in forum New To Java
    Replies: 2
    Last Post: 04-03-2011, 09:42 PM
  3. Questions on my code!
    By socboy6579 in forum New To Java
    Replies: 8
    Last Post: 11-03-2010, 02:30 AM
  4. Memory Leak questions, code review
    By mensaFool in forum Advanced Java
    Replies: 9
    Last Post: 03-08-2010, 04:27 PM
  5. UML, robust code, structure questions
    By mac in forum New To Java
    Replies: 2
    Last Post: 12-22-2009, 11:06 PM

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