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  
  creating new BufferedImage copy  (Read 3056 times)
0 Members and 1 Guest are viewing this topic.
Offline Serethos

Junior Member




Java games rock!


« Posted 2004-03-09 08:14:21 »

i have a bufferedimage and use the getSubimage() method. but it is important for me to create a new instance of the image in the size of the choosed subimage (i want to avoid referencing the whole image).

because bufferedimage has nothing like a copy-constructor
i tried somthing like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
...
{
      BufferedImage frame = createBufferedImage(img.getSubimage(0, 0, 0, 0));
}

public BufferedImage createBufferedImage(BufferedImage image)
      {
          ColorModel cm = image.getColorModel();
          boolean premultiplied = cm.isAlphaPremultiplied();
          WritableRaster raster = image.copyData(image.getRaster());
          return new BufferedImage(cm, raster, premultiplied, null);
      }



but i get the following exception:

java.awt.image.RasterFormatException: negative or zero width
       at java.awt.image.Raster.<init>(Raster.java:1094)
       at java.awt.image.WritableRaster.<init>(WritableRaster.java:114)
       at sun.awt.image.SunWritableRaster.<init>(SunWritableRaster.java:52)
       at sun.awt.image.ByteComponentRaster.<init>(ByteComponentRaster.java:140
)
       at sun.awt.image.ByteInterleavedRaster.<init>(ByteInterleavedRaster.java
:177)
       at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleav
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2004-03-09 11:31:53 »

Just use drawImage instead of getSubImage Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
BufferedImage[] tiles = new BufferedImage[6];

for(int i=0;i<6;i++)
{
      tiles[i] = gc.createCompatibleImage(32,32,Transparency.BITMASK);
      Graphics2D tg = (Graphics2D)tiles[i].getGraphics();
      tg.setComposite(AlphaComposite.Src);
      tg.drawImage(img,
            0,0,
            32,32,
            i*32,0,
            i*32+32,32,
            null);
      tg.dispose();
}


The snipped above slices a 192x32 image into 6 32x32 images.

Yes. It's really that simple Smiley

弾幕 ☆ @mahonnaiseblog
Offline Serethos

Junior Member




Java games rock!


« Reply #2 - Posted 2004-03-09 18:02:57 »

it doesnt work for me properly. first i got only disturbed images, then i tried to get it work for only one tile and used the following code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void paint(Graphics g)
      {
            BufferedImage frame;
            Graphics2D g2D = (Graphics2D)g;
     
           
           
            GraphicsEnvironment       ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
          GraphicsDevice                   gs = ge.getDefaultScreenDevice();
          GraphicsConfiguration       gc = gs.getDefaultConfiguration();
           
           
            frame = gc.createCompatibleImage(32,32,Transparency.BITMASK);
            Graphics2D tg = (Graphics2D)frame.getGraphics();
            tg.setComposite(AlphaComposite.Src);
            tg.drawImage(img,0,0,32,32,32,0,32,32,null);
           
            tg.dispose();
            g2D.drawImage(frame, 0, 0, this);
     
      }


// img is a valid bufferedimage

but it seems that im not able to adept your code. all i see is a rectangle of white nothing ... =)
i tried it with painting the whole image and a scaled version: therefore it works. for any reason i get no image painted with passing the four corners as parameters...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2004-03-09 18:35:50 »

tg.drawImage(img,
 0,0, //dx1, dy1 (upper left corner)
 32,32, //dx2, dy2 (lower right corner)
 i*32,0, //sx1, sy1 (upper left corner)
 i*32+32,32, //sx2, sy2 (lower right corner
 null);

see here

d=destination s=source

Use pen and paper to sketch it out... it isn't that complicated once you got your head around Wink

You can test my snipped from before with this image:

弾幕 ☆ @mahonnaiseblog
Offline Serethos

Junior Member




Java games rock!


« Reply #4 - Posted 2004-03-09 19:40:45 »

ohhh... once upon a time a wiseman said

"read the f**king manual!"

thought the corners were ment clockwise ...
but i havent said anything ...
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #5 - Posted 2004-03-09 20:28:18 »

> but i havent said anything ...

Well, you said "four corners" that was a bit odd, since there has to be two pairs Wink

Together with the problem you described and that piece of code wich just must result in something strange I gathered enough hints to spot your problem.

Don't forget to post your game once it's done Smiley

弾幕 ☆ @mahonnaiseblog
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 (21 views)
2014-07-24 01:59:36

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

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

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

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

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

danieldean (34 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!