Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 485 times)
0 Members and 1 Guest are viewing this topic.
Offline DazKins

Junior Member


Medals: 1
Projects: 1



« 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
Online 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.

Heroes' Keep is in development.
Meanwhile try Bloodridge
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
Offline DazKins

Junior Member


Medals: 1
Projects: 1



« 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
Offline DazKins

Junior Member


Medals: 1
Projects: 1



« 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 Knight


Medals: 28
Projects: 1


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

Junior Member


Medals: 1
Projects: 1



« 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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (139 views)
2013-05-17 21:29:12

alaslipknot (148 views)
2013-05-16 21:24:48

gouessej (178 views)
2013-05-16 00:53:38

gouessej (172 views)
2013-05-16 00:17:58

theagentd (182 views)
2013-05-15 15:01:13

theagentd (166 views)
2013-05-15 15:00:54

StreetDoggy (211 views)
2013-05-14 15:56:26

kutucuk (235 views)
2013-05-12 17:10:36

kutucuk (234 views)
2013-05-12 15:36:09

UnluckyDevil (239 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.248 seconds with 21 queries.