Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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] Java issue with referencing  (Read 963 times)
0 Members and 1 Guest are viewing this topic.
Offline DazKins
« Posted 2012-12-25 08:50:10 »

So i'm trying to draw the pixels of a Bitmap to the screen with a modified colour. What i want, is for the image to be drawn with an offset colour but to not change the actual pixels stored for that image. Currently im having issues with this.

Here's my method

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public void renderWithColourOffset(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col1[],int col2[]){
      Bitmap b0 = new Bitmap(b);
     
      for(int i=0;i<b0.pixels.length;i++){
         for(int c=0;c<col1.length;c++){
            if(b0.pixels[i]==col1[c]){
               b0.pixels[i] = col2[c];
            }
         }
      }
     
      render(b0,xp,yp,xFlip,yFlip);
   }


The issue is that when it does this it changes to pixels in the original Bitmap parsed to the function so it wont work whenever the function is called again.

This seems to be an issue with java referencing but i've tried a lot of things and simply can't get it to work :S

Oh and by the way, Merry Christmas Cheesy

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline SHC
« Reply #1 - Posted 2012-12-25 09:09:23 »

Which library are you using? Could you show the Bitmap class

Offline DazKins
« Reply #2 - Posted 2012-12-25 09:11:33 »

I'm using default Canvas with graphics stored in an RGB pixel array. The Bitmap class stores a pixel array, a width and a height

Here's the full class: http://pastebin.com/sE8PcwKb

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-12-25 09:14:38 »

Line 15. Arrays are objects. You are simply referring to the same array as the Bitmap's you passed in. You should instead copy the array:
this.pixels = Arrays.copyOf(b.pixels, b.pixels.length);
(Arrays is a class in java.util package)

I suggest you learn more about Java and OOP before going into graphics.

Offline DazKins
« Reply #4 - Posted 2012-12-25 09:18:18 »

I understand what you're saying and i realize this should work. But it doesn't, it still assign the pixels to the original Bitmap parsed

DazKins

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

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-12-25 09:21:22 »

Your code doesn't work, you're right. If you replaced line 15 with my suggestion it should work. Please re-read my post....

EDIT: Just noticed you also need to replace line 29 too. You may need to replace line 22 only if you don't want to modify the original BufferedImage.

Offline SHC
« Reply #6 - Posted 2012-12-25 09:25:39 »

It's not needed to create a Bitmap class. You can simply use the Java2D.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public static BufferedImage mask(BufferedImage bimg, int[] cols1, int[] cols2){
    for (int y=0; y<bimg.getHeight(); y++){
         for (int x=0; x<bimg.getWidth(); x++){
              int col = bimg.getRGB(x, y);
              for (int i=0; i<cols1.length; i++){
                   if (col==cols1[i]){
                       bimg.setRGB(x, y, cols2[i]);
                       break;
                   }
              }
         }
    }
    return bimg;
}

Offline DazKins
« Reply #7 - Posted 2012-12-25 09:25:53 »

Ahhh...

It wasn't an issue with referencing, it was an issue with the array of bytes that i used to store data for each sprite Smiley

Thanks anyway

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.

Mr.CodeIt (9 views)
2014-12-27 04:03:04

TheDudeFromCI (12 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!