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  
  bufferedImage.setRGB(x,y,rgba) ?  (Read 2851 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Posted 2007-07-22 05:04:08 »

Hi,

I have a instance of BufferedImage. What I'm trying to do with this instance is to modify some pixels in it, by doing
1  
bufferedImage.setRGB(x,y,rgba)


This works, that is, I get some results. But I do not get the right color!

It only seems to "allow" me to set a rgba value that exists in the "colorspace" of that bufferedImage instance. So, if I loaded up a image that is 4 colors, red, green, blue, black, then I cannot change any pixels to anything but red, green, blue, black. If I were to do change some pixels to, let's say, cyan, then I would get weird results (no error/exception though).

I can't seem to find a easy way to add a new color to the colorspace. Any ideas?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline broumbroum

Junior Member





« Reply #1 - Posted 2007-07-22 05:11:05 »

What is the exact purpose? Do you want to colorize the image or is it intended to modify one particular pixel? There are easier ways to colorize the image, e.g. troughout Paint or Composite.  Huh

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #2 - Posted 2007-07-22 06:23:36 »

I have a world map. Each country is colored in a unique color (solid). When I click on this world map with the mouse I can look up on which pixel was clicked on by using mouseX,mouseY, and then I can see what the color is for that pixel. When I know the color that was clicked on, I know what country was also clicked on.
Also, I wish to replace all those pixels, that are in that countrys color, with another color...a highlight color. However, that highlight color does not exist in the colorspace, and that's where the problem is.

If I recreate the worldmap image, that is create a new BufferedImage with TYPE_INT_ARGB and then draw the worldmap image onto it, the speed of Java2D will downgrade to 30 something fps instead of 65ish.

Another question. Instead of recoloring each individual pixel, isn't it possible to simply replace the color in the colorspace with another color? (gonna experiment with it now)

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #3 - Posted 2007-07-22 19:29:55 »

Ok, I'm using TYPE_INT_RGB instead of TYPE_INT_ARGB, increasing the fps dramatically. Problem solved I believe.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline keldon85

Senior Member


Medals: 1



« Reply #4 - Posted 2007-07-22 19:41:02 »

You can also increase the FPS dramatically with the following code (bypassing setRGB)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
      ColorModel colorModel = new DirectColorModel(24, 0xff0000,
             0x00ff00, 0x0000ff);
     
      SampleModel sampleModel = new SinglePixelPackedSampleModel(
            DataBuffer.TYPE_INT, width, height, new int[] { 0xff0000, 0x00ff00,
                  0x0000ff });

      DataBuffer dataBuffer = new DataBufferInt(buffer, width * height);

      WritableRaster raster = Raster.createWritableRaster(sampleModel,
            dataBuffer, new Point(0, 0));
     
      image = new BufferedImage (colorModel, raster,true, new Hashtable());

Offline broumbroum

Junior Member





« Reply #5 - Posted 2007-07-23 00:13:03 »

(...)
Another question. Instead of recoloring each individual pixel, isn't it possible to simply replace the color in the colorspace with another color? (gonna experiment with it now)
This is as simple as to add a alpha-Composite-layer on the pictures in Photoshop. That looks as follows :
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  
public class HighlightByMouse extends MouseAdapter {
      /** this would be the surface area to highlight */
     public Dimension surfaceHighlight = new Dimension(20, 20);
     /***/
     public Color colorHighlight = Color.RED;
     /***/
     public Image theWorldMapBufferedImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);

     public void mouseReleased(MouseEvent e) {
          theWorldMapBufferedImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
     }

     public void mousePressed(MouseEvent e) {
      Graphics2D g2 = (Graphics2D)theWorldMapBufferedImage.getGraphics();
      Composite cps = g2.getComposite();
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
      Color c = g2.getColor();
      g2.setColor(colorHighlight);
      Rectangle highlight = new Rectangle(
              (int)((float)e.getX() - (float)surfaceHighlight.width / 2.0f),
              (int)((float)e.gety() - (float)surfaceHighlight.height / 2.0f),
              surfaceHighlight.width,
              surfaceHighlight.height
       );
      g2.fillRect(highlight.x, highlight.y, highlight.width, highlight.height );
      g2.setComposite(cps);
     g2.setColor(c);
    }
}
  Cheesy of course the WorldMap image should have contents to load. that means you may replace the blank image with one read from your favorite picture. 

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
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 (22 views)
2014-07-24 01:59:36

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

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

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

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

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

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

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

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

Riven (52 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!