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  
  can only create images etc in main class  (Read 1698 times)
0 Members and 1 Guest are viewing this topic.
Offline roland
« Posted 2009-10-12 10:12:38 »

for some reason I cannot create images in any classes but my main one which extends applet.
When I pass main into other classes to load images or something, there are no errors but the images dont show up. Does anyone have a solution to this? thanks
Offline Karmington

Senior Member


Medals: 1
Projects: 1


Co-op Freak


« Reply #1 - Posted 2009-10-13 01:13:41 »

1) check if image is null.
2) it probably is, check your paths.
3) still not working? post relevant code snippets

Offline roland
« Reply #2 - Posted 2009-10-13 04:38:48 »

Oh, I found out i can use Toolkit.getDefaultToolkit().getImage to load the images in other classes. Thanks.

I now have a new problem when trying to convert from Image to bufferedImage to find the colour of the pixels for collision detection. I am trying to find which pixels have 0 alpha
I am not sure if the buffered image contains anything as I can't draw it, but the image does that I'm trying to copy from.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
BufferedImage bufferedImage = new BufferedImage ( WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB  ); 
         Graphics g = bufferedImage.getGraphics();
         g.drawImage(image/*perfectly fine, just can't get alpha from it*/, 0, 0, null);
         g.dispose();

for (int x = 0; x < WIDTH; x++)
         {
                                while (true)
            {
               Color c = new Color(img.getRGB(x, y));
               if (c.getAlpha() == 0)
               {
                  break;
               }
               
               y++;
            }
}

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


Game Engineer


« Reply #3 - Posted 2009-10-13 16:18:39 »

If you've got TYPE_INT_ARGB, you can do this to check alpha:

1  
2  
3  
4  
if (image.getRaster().getPixel(x,y, new int[4])[3] == 0)
{
    //Alpha
}


Also, you shouldn't be using the Toolkit to read your images. Instead, use ImageIO.

1  
2  
3  
4  
5  
BufferedImage image = ImageIO.read(new File("/Computer/folder/image.png"));
//or
BufferedImage image2 = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("/resources/image.png");
//The first will look on your disk (or in a URL).
//The second will look in the classpath (useful for JAR files).


Usually when you read a BufferedImage from a file it isn't INT_ARGB, so I typically will create a new BufferedImage with the correct type and then draw into that.
1  
2  
3  
BufferedImage image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("/resources/image.png");
myImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
myImage.getGraphics().drawImage(image, 0, 0, null);

See my work:
OTC Software
Offline roland
« Reply #4 - Posted 2009-10-14 04:49:44 »

wow, thanks!
I didn't really want to use the toolkit either, I just didn't see an alternative other than ImageIO, and when I used that, I don't know why but it made my speed stay at 3 fps even when they had finished loading. (Normally 70-80fps). Do you think this maybe because of some load error that kept popping up even after the image is created which slows down the program? (my program is a plain applet - not JApplet or swing or anything)  and why would this happen? thanks.
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #5 - Posted 2009-10-14 10:54:41 »

wow, thanks!
I didn't really want to use the toolkit either, I just didn't see an alternative other than ImageIO, and when I used that, I don't know why but it made my speed stay at 3 fps even when they had finished loading. (Normally 70-80fps). Do you think this maybe because of some load error that kept popping up even after the image is created which slows down the program? (my program is a plain applet - not JApplet or swing or anything)  and why would this happen? thanks.

That slow-down could be a result of the version of Java you are using. Check you are upto-date 'java -version'.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #6 - Posted 2009-10-14 11:14:20 »

To me, it seems more a transparency problem. Java2D can become very slow when using alpha other than 0 or 255.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #7 - Posted 2009-10-14 16:25:12 »

wow, thanks!
I didn't really want to use the toolkit either, I just didn't see an alternative other than ImageIO, and when I used that, I don't know why but it made my speed stay at 3 fps even when they had finished loading. (Normally 70-80fps). Do you think this maybe because of some load error that kept popping up even after the image is created which slows down the program? (my program is a plain applet - not JApplet or swing or anything)  and why would this happen? thanks.
Yeah, Java2D kinda sucks when you want to squeeze anything out that isn't pretty simple. Bonbon is right that transparency really hurts it - most likely when you were using ImageIO the transparency actually started working, and then of course it took a lot longer. There can also be weird problems with Java2D keeping images cached even when you don't want it to anymore, so you can say myImage.flush() when you're done with it to try to alleviate that.

See my work:
OTC Software
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #8 - Posted 2009-10-14 19:05:04 »

If you've got TYPE_INT_ARGB, you can do this to check alpha:

1  
2  
3  
4  
if (image.getRaster().getPixel(x,y, new int[4])[3] == 0)
{
    //Alpha
}


A bit too complicated (not to mention lots of created garbage).

Just use BufferedImage.getRGB(x,y)&0xff000000:
getRGB(int x, int y)
          Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

Dmitri

Offline roland
« Reply #9 - Posted 2009-10-15 02:49:59 »

thanks for the repies.  Smiley
I am not using Graphics2D though
I will try BufferedImage.getRGB(x,y)&0xff000000, thanks
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


Game Engineer


« Reply #10 - Posted 2009-10-15 06:26:11 »

A bit too complicated (not to mention lots of created garbage).

Just use BufferedImage.getRGB(x,y)&0xff000000:
getRGB(int x, int y)
          Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

Dmitri


Good, thanks for that. I haven't ever used hex in Java before, actually. It's one of those things I stupidly avoided for a while. I've always hated dealing with Raster.getPixel() because of the weirdness of having to pass in another array.

So alpha is the first byte even though usually when setting it (like with the method I posted above) at occurs last? It makes sense that it is literally A then R then G then B, but it seems in most places the paradigm is to go RGBA instead.

See my work:
OTC Software
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #11 - Posted 2009-10-15 14:33:50 »

Good, thanks for that. I haven't ever used hex in Java before, actually. It's one of those things I stupidly avoided for a while. I've always hated dealing with Raster.getPixel() because of the weirdness of having to pass in another array.

So alpha is the first byte even though usually when setting it (like with the method I posted above) at occurs last? It makes sense that it is literally A then R then G then B, but it seems in most places the paradigm is to go RGBA instead.

I'd never noticed it myself, but it appears the javadoc for much of the java.awt.Color class is misleading in that regard.
In many places it refers to colors containing alpha as "RGBA", when it infact means "ARGB".
In particular the javadoc (and name of the int parameter!) for this constructor have been badly chosen.

1  
2  
3  
4  
5  
6  
7  
public Color(int rgba,
             boolean hasalpha)

    Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. If the hasalpha argument is false, alpha is defaulted to 255.

    Parameters:
        rgba - the combined RGBA components


Fortunately it also has the details explaining how the specific bits are interpreted, degrading it from being erroreous to just misleading.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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!