Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
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  
  Raster – Using getPixels and setPixels  (Read 10300 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Posted 2006-11-29 00:30:00 »

Okay, I've been trying to use getPixels and setPixels in the Raster within a BufferedImage, and it is making me completely insane. I don't know what the last parameter, the array, is for. It seems pointless, and no matter what I put in there it throws an exception and bugs out. I just want some pixels! Is that so hard?

All I am trying to do is copy one image's pixels into another so that I can use different image types. I'm sure there is some way to simply change a BufferedImage from RGB to RGBA, but I couldn't find it, and even if there is an easier way, it would be great if someone could tell me what those random parameters are for anyway.

Essentially I have loaded an image from a file which does not have an alpha channel (at least when I try changing pixels to alpha it does nothing) and I want it to allow transparency, and therefore the fourth parameters (alpha).

1  
2  
3  
4  
BufferedImage b = ImageIO.read(new java.io.File(System.getProperty("user.dir")+"/test.gif"));
BufferedImage r = new BufferedImage(b.getWidth(),b.getHeight(),BufferedImage.TYPE_INT_ARGB);
int[] pixels = null;
r.getRaster().setPixels(0,0,r.getWidth(),r.getHeight(),b.getData().getPixels(0,0,r.getWidth(),r.getHeight(), ???-->  pixels  <--??? ));


I put arrows next to what is confusing me. What the hell is that for? Every example I've found seems to put a null value in it. By the way, this code throws the exception
1  
2  
3  
4  
5  
6  
7  
8  
java.lang.ArrayIndexOutOfBoundsException: 1855838776
   at java.awt.image.SinglePixelPackedSampleModel.setPixels(SinglePixelPackedSampleModel.java:670)
   at java.awt.image.WritableRaster.setPixels(WritableRaster.java:549)
   at sun.awt.image.SunWritableRaster.setPixels(SunWritableRaster.java:311)
   at jpixel.PixelWindowTest$Panel.loadImage(PixelWindowTest.java:43)
   at jpixel.PixelWindowTest$Panel.<init>(PixelWindowTest.java:24)
   at jpixel.PixelWindowTest.<init>(PixelWindowTest.java:12)
   at jpixel.PixelWindowTest.main(PixelWindowTest.java:107)


I've gotten a million of these, because the required array size seems very unclear. This particular one is a result of the setPixels method (where the parameter's use is clear, because it's a mutator method and therefore passing something to it makes sense), what really confuses me is the getPixels method, and what its array parameter means, and what it's for.

So if anyone could enlighten me that would be great, and any ideas on how to change the initial type of a BufferedImage after it has been created (to TYPE_INT_ARGB so I can use alpha) would be excellent.

Thanks.

See my work:
OTC Software
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2006-11-29 01:29:09 »

Also, how do I get the RGB information of a pixel? I try to use image.getData().getPixel(x,y...) and getSample but both of them will only return one value from 0-255, which definitely cannot represent the colors I want well enough. So how does that work?

See my work:
OTC Software
Offline Kova

Senior Devvie





« Reply #2 - Posted 2006-11-29 01:45:57 »

see http://www.java-gaming.org/forums/index.php?topic=14236.0 about getting pixels and manipulating them.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2006-11-29 03:41:52 »

Ooh, that's excellent. Thank you!

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 

 
ivj94 (583 views)
2018-03-24 14:47:39

ivj94 (48 views)
2018-03-24 14:46:31

ivj94 (374 views)
2018-03-24 14:43:53

Solater (61 views)
2018-03-17 05:04:08

nelsongames (109 views)
2018-03-05 17:56:34

Gornova (151 views)
2018-03-02 22:15:33

buddyBro (693 views)
2018-02-28 16:59:18

buddyBro (91 views)
2018-02-28 16:45:17

xxMrPHDxx (493 views)
2017-12-31 17:17:51

xxMrPHDxx (733 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!