Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  I made my pixels[][] array to work  (Read 1250 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2013-08-15 17:14:27 »

I just cant understand bufferedimage pixel array so i made my own array
it is just pixels[][] array put x and y and you are ready to go Cheesy i think i will make i lib
so do you want to know how it works ??


MAIN :
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  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Main extends Canvas implements Runnable {

   private static final long serialVersionUID = 1L;
   public Thread thread;
   // my way to get width and height :D
  public int width = 150, height = (width / 10) * ((10 / 3) * 2), scale = 6;
   public static String title = "Pixels[][]";
   public boolean run = false;
   public int finalFrames = 0;
   public int[][] pixels;
   public Render r;
   public BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

   public Main() {
      setPreferredSize(new Dimension(width * scale, height * scale));
      pixels = new int[width][height];
      for (int x = 0; x < pixels.length; x++) {
         for (int y = 0; y < pixels[0].length; y++) {
            pixels[x][y] = 0xffffff;
         }
      }
      r = new Render(this, pixels, scale);
   }

   public void tick() {
      r.tick();
      pixels = r.getPixels();
      for (int x = 0; x < pixels.length; x++) {
         for (int y = 0; y < pixels[0].length; y++) {
            img.setRGB(x, y, pixels[x][y]);
         }
      }
   }

   public void render(Graphics g) {
      //r.render(g);
     g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
   }

   public void draw() {
      BufferStrategy bs = getBufferStrategy();
      if (bs == null) {
         createBufferStrategy(3);
         return;
      }

      Graphics g = bs.getDrawGraphics();
      g.clearRect(0, 0, getWidth(), getHeight());
      render(g);
      g.setFont(new Font("Arial", Font.BOLD, 12));
      g.setColor(Color.BLACK);
      g.drawString("FPS : " + finalFrames, 0, 10);

      bs.show();
      g.dispose();
   }

   public void run() {
      long lastTime = System.nanoTime();
      final double amountOfTicks = 60.0;
      double ns = 1000000000 / amountOfTicks;
      double delta = 0;
      int ticks = 0;
      int frames = 0;
      long timer = System.currentTimeMillis();

      while (run) {
         long now = System.nanoTime();
         delta += (now - lastTime) / ns;
         lastTime = now;
         if (delta >= 1) {
            tick();
            ticks++;
            delta--;
         }
         draw();
         frames++;
         if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            finalFrames = frames;
            frames = 0;
            ticks = 0;
         }
      }
   }

   public synchronized void start() {
      run = true;
      thread = new Thread(this);
      thread.start();
   }

   public synchronized void stop() {
      run = false;
      try {
         thread.join();
      } catch (InterruptedException e) {
         System.out.println("ERROR can not stop the thread!");
      }
      System.exit(0);
   }

   public static void main(String[] args) {
      JFrame jf = new JFrame();
      Main m = new Main();
      jf.add(m);
      jf.pack();
      jf.requestFocus();
      jf.setTitle(title);
      jf.setVisible(true);
      jf.setResizable(false);
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jf.setLocationRelativeTo(null);
      m.start();
   }
}


RENDER:

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  
import java.awt.Color;
import java.awt.Graphics;

public class Render {

   private int[][] pixels;
   private Main m;
   private int scale;
   
   public Render(Main main, int[][] pixels, int pixScale) {
      m = main;
      this.pixels = pixels;
      scale = pixScale;
   }

   public void tick() {
      pixels[10][10] = 0x000000;
   }
   
   public int[][] getPixels(){
      return pixels;
   }
   
   public void render(Graphics g) {
      for (int x = 0; x < pixels.length; x++) {
         for (int y = 0; y < pixels[0].length; y++) {
            g.setColor(new Color(pixels[x][y]));
            g.fillRect(x * scale, y * scale, scale, scale);
         }
      }
   }
}
}


This is version 1.2 and will be adding more features later Cheesy Cheesy
please support me

JAVAAAAAAAAAAAAAAAA Freak Cheesy!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-08-15 18:51:16 »

Moved to shared code Smiley
Go ahead and paste in your code into the first post.
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #2 - Posted 2013-08-16 16:40:47 »

How horribly inefficient   Cheesy

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-08-16 17:40:01 »

Why is it inefficient? From what I see it should work fine. Granted its not really worth putting it on the forum as mostly everyone knows how to do this, and it is Java2D so it would be rather slow, but other than that I don't see a problem!

Offline jonjava
« Reply #4 - Posted 2013-08-16 18:18:13 »

Using setRGB() instead of directly setting the value is comparatively inefficient since it jumps through a few hoops.

Not understanding how a 1d array can represent a 2d array shouldn't really be a legitimate problem since they're just different spectacles used to look at exactly the same data.

Offline GNecro1
« Reply #5 - Posted 2013-08-16 19:52:09 »

Yea it is not Java2D lol i dont use libs and for beginners it is simple to use
it is harder for them to understand if it is 1d array
ok lets try this
put a image on x 29 and y 4 the size of 32 x 32

2d array
1  
2  
3  
4  
5  
6  
int x1 = 29, y1 = 4;
for (int x = 0; x < 32; x++) {
   for (int y = 0; y < 32; y++) {
      pixels[x1 + x][y1 + y] = /* IMAGE PIXELS */pixelsImg[x][y];
   }
}

1d array

IDK because i dont understand how it works and 2d array i do so let me be

and if everyone knows how to do this why dont they Huh

JAVAAAAAAAAAAAAAAAA Freak Cheesy!
Offline GNecro1
« Reply #6 - Posted 2013-08-16 19:54:01 »

Why is it inefficient? From what I see it should work fine. Granted its not really worth putting it on the forum as mostly everyone knows how to do this, and it is Java2D so it would be rather slow, but other than that I don't see a problem!

and by the way i get from 1600 to 1750 fps !!! and it never changes what ever i do it stays the same !!!

JAVAAAAAAAAAAAAAAAA Freak Cheesy!
Offline jonjava
« Reply #7 - Posted 2013-08-16 21:01:02 »

1d array

IDK because i dont understand how it works and 2d array i do so let me be

Not understanding something isn't a good reason to give up.

and if everyone knows how to do this why dont they Huh

Would you drive a car with your feet on the steering wheel if you could do it?

Here's 1d form:
1  
2  
3  
4  
5  
6  
int x1 = 29, y1 = 4;
for (int x = 0; x < 32; x++) {
   for (int y = 0; y < 32; y++) {
      pixels[(x1 + x) + (y1 + y) * screen.width] = /* IMAGE PIXELS */pixelsImg[x + y * img.width];
   }
}

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #8 - Posted 2013-08-16 21:15:05 »

Ah, sorry I didn't even see he was using a 2D array. Nevermind, I do see problems. Never look at code on a smartphone, you always miss something!

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.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (41 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!