Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED]WHY DOES THIS WORK o.0  (Read 810 times)
0 Members and 1 Guest are viewing this topic.
Offline DazKins
« Posted 2012-12-17 20:41:57 »

So I've been making 2D java games with the same setup for a long time but i don't understand how it works

Here's the code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
package com.dazkins.alone;

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 java.util.Random;

import javax.swing.JFrame;

public class AloneComponent extends Canvas implements Runnable{

   private static final int WIDTH = 256;
   private static final int HEIGHT = 256;
   private static final int SCALE = 3;
   
   private boolean running = false;
   
   private BufferedImage img = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
   private int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
   
   public static void main(String args[]){
      AloneComponent c = new AloneComponent();
      JFrame frame = new JFrame("Alone");
     
      frame.add(c);
      frame.setVisible(true);
      frame.setResizable(false);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
     
      c.start();
   }
   
   public AloneComponent(){
      Dimension d = new Dimension(WIDTH*SCALE,HEIGHT*SCALE);
     
      setPreferredSize(d);
      setMaximumSize(d);
      setMinimumSize(d);
   }
   
   public void start(){
      running = true;
      Thread t = new Thread(this);
      t.start();
   }
   
   public void stop(){
      running = false;
   }
   
   public void tick(){
     
   }
   
   public void render(){
      BufferStrategy bs = getBufferStrategy();
     
      if(bs==null){
         createBufferStrategy(3);
         return;
      }
     
      Graphics g = bs.getDrawGraphics();
     
      for(int i=0;i<pixels.length;i++){
         pixels[i] = new Random().nextInt(0x0000FF) << 16;
      }
     
      g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
     
      g.dispose();
      bs.show();
   }
   
   public void run() {
      while(running){
         tick();
         render();
      }
   }
   
}


My question is about that the array of pixels that is used to store colour data for each pixel. I understand how it represents colours, but i don't understand atall at what part in the code that pixel array is asigned to the BufferedImage or rendered to the screen

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline SimonH
« Reply #1 - Posted 2012-12-17 20:47:40 »

The pixel array is acquired from the image in line 22, modified in lines 69-71 and rendered to the screen in line 73.
The pixel array stores the image data, so changing the pixels automatically changes the image.

People make games and games make people
Offline h3ckboy
« Reply #2 - Posted 2012-12-17 20:48:21 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void render(){
      BufferStrategy bs = getBufferStrategy();
     
      if(bs==null){
         createBufferStrategy(3);
         return;
      }
     
      Graphics g = bs.getDrawGraphics();
     
      for(int i=0;i<pixels.length;i++){
         pixels[i] = new Random().nextInt(0x0000FF) << 16;
      }
     
      g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
     
      g.dispose();
      bs.show();
   }


Is this not what your looking for? I'm sorry if I misunderstand the question.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #3 - Posted 2012-12-17 20:51:08 »

Where did you learn that setup? If you got it from some tutorial, I propose you read it again. If it doesen`t say there, then look for some simmilar tutorials, which might explain what you want to know.  Smiley

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline DazKins
« Reply #4 - Posted 2012-12-17 20:52:03 »

Let me rephrase it slightly, I want to know at what point in the program does the program know to render that pixel array to the screen, because if i rename it, it still works.

In the render function the pixel array is never called apart from to edit it, so how does the program know to use it?

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2012-12-17 20:57:07 »

Take a look at this: http://lmgtfy.com/?q=java2d+tutorials

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline DazKins
« Reply #6 - Posted 2012-12-17 21:06:38 »

If you can find me a page that tells me let me know because ive tried that many times :S

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #7 - Posted 2012-12-17 21:15:07 »

Do you see that line
private int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();


You are telling the program to use
 int[] pixels 
to store the image pixels. Everything in Java acts like a pointer reference. So the pixel data for your array is referenced to the same pixel data in your image, therefore all changes you make will be instantly translated. It does not matter where you edit that array, or if you change the name to that array, the pointer will stay the same until you write something like
 int[] pixels = new int[4] 
.

and the line where it draws the image...

 g.drawImage(img, 0, 0, getWidth(), getHeight(), null); 


Does that clear anything up?

Offline DazKins
« Reply #8 - Posted 2012-12-17 21:17:22 »

YES

thank you so much dude that was exactly what i was looking for Smiley

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (63 views)
2014-04-15 18:08:23

BurntPizza (61 views)
2014-04-15 03:46:01

UprightPath (74 views)
2014-04-14 17:39:50

UprightPath (57 views)
2014-04-14 17:35:47

Porlus (74 views)
2014-04-14 15:48:38

tom_mai78101 (100 views)
2014-04-10 04:04:31

BurntPizza (160 views)
2014-04-08 23:06:04

tom_mai78101 (255 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!