Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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  
  Really basic image loading question  (Read 1984 times)
0 Members and 1 Guest are viewing this topic.
Offline nelsoc3

Junior Newbie




More cowbell!


« Posted 2004-11-29 20:01:52 »

I must be braindead or something... I'm trying to get an image to load (in a JFrame, not an Applet) using Toolkit:

Code:
Image testCard;
testCard = Toolkit.getDefaultToolkit().getImage("http://img78.exs.cx/img78/9144/sydney-character-season1.png");

When I try to draw this image in my paint class it doesn't work.  I'll be adding a media tracker eventually, but I didn't think I should need it to just add this one test image...

Can anyone tell me why this might be screwed up?  Or recommend an different (and maybe easier) way to do it?

Thanks!
Offline woogley
« Reply #1 - Posted 2004-11-29 20:31:46 »

unfortunately an Image loaded from Toolkit will not be loaded as soon as you call it. I suggest you use ImageIO because 1) it loads immediately (without need of MediaTracker), and 2) it returns BufferedImage

it would be simple to do:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public BufferedImage getWebImage(String path) {
      try {
            return ImageIO.read(new URL(path).openStream());
      }
      catch (IOException e) {
            e.printStackTrace();
      }
      catch (MalformedURLException e) {
            e.printStackTrace();
      }
      return null;
}

then you can use:
1  
getWebImage(http://img78.exs.cx/img78/9144/sydney-character-season1.png");

to get your image. Now if you *really* need it to be Image and NOT a BufferedImage, you could always do this (if you're extending a Component):
1  
2  
3  
public Image convert(BufferedImage b) {
      return createImage(b.getSource());
}

and then you could do:
1  
Image testCard = convert(getWebImage("http://img78.exs.cx/img78/9144/sydney-character-season1.png"));
Offline Eliwood

Junior Member




Stencyl


« Reply #2 - Posted 2004-11-30 03:19:34 »

If this is for really basic and simple loading, it wouldn't hurt to use ImageIcon. Just bear in mind that this isn't optimal for normal game circumstances and is better fit for GUI stuff, hence its name.

Pass in the path (as a string) to the ImageIcon's constructor and then call getImage() on that to get it as an Image rather than an Icon. Like ImageIO, this also eliminates the need for MediaTracker.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nelsoc3

Junior Newbie




More cowbell!


« Reply #3 - Posted 2004-11-30 13:07:26 »

Quote
unfortunately an Image loaded from Toolkit will not be loaded as soon as you call it. I suggest you use ImageIO because 1) it loads immediately (without need of MediaTracker), and 2) it returns BufferedImage

it would be simple to do:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public BufferedImage getWebImage(String path) {
      try {
            return ImageIO.read(new URL(path).openStream());
      }
      catch (IOException e) {
            e.printStackTrace();
      }
      catch (MalformedURLException e) {
            e.printStackTrace();
      }
      return null;
}

then you can use:
1  
getWebImage(http://img78.exs.cx/img78/9144/sydney-character-season1.png");

to get your image.


I don't have to have an image, so a BufferedImage should be just fine.  I put in the code (quoted above), but I've got some "cannot resolve symbol" errors now.  I've tried adding different packages to the import list (java.lang.Exception.*; javax.imageio.*), but that doesn't make them go away.  Any ideas?

Thanks for your help and quick response!  You guys are awesome!

Nels
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #4 - Posted 2004-11-30 15:28:21 »

http://www.eclipse.org

It'll figure out your errors and fix them for you! Smiley
It's really handy because it'll tell you where the error is, and exactly how to fix it, and will even let you click on the desired solution to have it execute for you.  If you don't want to use this handy IDE, perhaps you could give us the full error message and we can better address the problem

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline nelsoc3

Junior Newbie




More cowbell!


« Reply #5 - Posted 2004-11-30 17:19:45 »

Quote
http://www.eclipse.org

It'll figure out your errors and fix them for you! Smiley
It's really handy because it'll tell you where the error is, and exactly how to fix it, and will even let you click on the desired solution to have it execute for you.  If you don't want to use this handy IDE, perhaps you could give us the full error message and we can better address the problem


Funny you mention that... I actually have Eclipse, as well as NetBeans (just got v4b2).  I find that NetBeans is easier to use as a newb... I tried Eclipse and couldn't figure out how to build my file.  With NetBeans you just press a button and it goes.  I'm assuming this means that Eclipse is more powerful (apps that are harder to figure out are usually more powerful it seems), but for now, I'm sticking with NetBeans.  

As far as the errors I'm generating, they come straight from the code that woogley posted.  I like the simplicity of the code, and it seems like it should work, but here's what's happening...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
Compiling 1 source file to C:\aliasGame\build\classes
C:\aliasGame\src\AliasGame.java:129: cannot find symbol
symbol  : class URL
location: class src.AliasGame
        return ImageIO.read(new URL(path).openStream());
C:\aliasGame\src\AliasGame.java:131: cannot find symbol
symbol  : class IOException
location: class src.AliasGame
        catch (IOException e) {
C:\aliasGame\src\AliasGame.java:134: cannot find symbol
symbol  : class MalformedURLException
location: class src.AliasGame
        catch (MalformedURLException e) {
Note: C:\aliasGame\src\AliasGame.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
BUILD FAILED (total time: 2 seconds)


The lines it references are straight from the code I copied and pasted.  Like I said, I've tried importing other stuff to help find the symbols, but it hasn't worked yet.
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #6 - Posted 2004-11-30 19:04:55 »

that's cause you're not importing the RIGHT stuff.

Here's the 1.4.2 javadoc.
http://java.sun.com/j2se/1.4.2/docs/api/

Look up the packages that each of the Classes mentioned in your errors come from.

I'd suggest giving Eclipse another shot.  The only complication I've found is in how to set up a project, but once you've done it one time, you're set from there.  You create a new Project, with whatever name you want.  Then right-click in the project, make a new Class, and just edit it like you would in any other editor.  You save it, it compiles for you, and it'll tell you anything that's wrong in the console window.  You put your mouse next to any error in your code, and press Ctrl+Space, and if it doesn't automatically fix your error, it'll give you a little window listing solutions you can choose from.  You click on the one you want, and you're good to go.  Once all of your compile errors are gone, you click the little running man icon, or go to Run / Run as application.

There's no trick to building.  You save the file, and it automatically compiles for you.

Oh, and make sure you're in the Java Perspective as well, that helps a tad to organize things on your screen.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline nelsoc3

Junior Newbie




More cowbell!


« Reply #7 - Posted 2004-11-30 20:12:51 »

You're right about the importing!  I'd checked the API before, but somehow ended up with the wrong packages... I got it right now! Thanks!

As for Eclipse though... When I go to create a new file, there is no option to make a new Class - I only have File, Folder, and Project as choices.  In addition, there doesn't appear to be a "java" editor... if I use the default editor, it opens the file in NetBeans. If I use the text editor, it doesn't give me any color coding. I can live with that, but ... I don't have a running man, nor a Java Perspective.  I'm using: Version: 3.0.1, Build id: 200409161125, which I just downloaded yesterday (or maybe the day before).  I can select Run->External Tools to get "Run As", but that doesn't expand to anything (like, say, Applet, or Java application...)  Is it possible I'm missing a plug-in or something?  Doesn't seem like Eclipse is doing themselves any favors in the marketing department...

Offline Eliwood

Junior Member




Stencyl


« Reply #8 - Posted 2004-11-30 20:55:11 »

I think now's a good time for me to post up my "Getting Started with Eclipse" tutorial. Explains the hardest part of Eclipse - the first 5 minutes of setting things up, complete with a good amount of illustrations.

I wrote it for some people totally new to Java so keep that in mind when you see me explain something that any of us would take for granted.

http://zelda.acidblue.net/Eclipse/

p.s. Looks like you missed the most important step of all. Make sure that you are in the Java perspective, otherwise nothing will work at all, nothing. Check the tutorial to see where that actually is.

Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #9 - Posted 2004-12-01 00:50:51 »

yeah I added that at the bottom, but neglected to emphasize its true importance Wink  But bravo on that tutorial, it's perfect!  Hopefully it'll help out our OP here.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nelsoc3

Junior Newbie




More cowbell!


« Reply #10 - Posted 2004-12-01 13:18:49 »

Okay, I checked out the tutorial, and it looks very helpful.  But it still doesn't fix the fact that I don't have a Java Perspective.  I'm not kidding, and I'm not an idiot.  I went to Window->Open Perspective (which I'd done before, trust me, just because I'm new doesn't mean I'm stupid) ... there is no Java Perspective.

I've got:
CVS Repository Exploring
Debug
Resource (Default)
Team Synchronizing

Did I screw something up when I "installed" Eclipse?  Did I get an incomplete version or something?
Offline Eliwood

Junior Member




Stencyl


« Reply #11 - Posted 2004-12-01 15:38:54 »

I'm at a loss to explain this. Something is horribly wrong with your copy of Eclipse...

Try redownloading it fom a different mirror. If that doesn't work, download a different build.

Before you do that though, could you take a full snapshot of Eclipse and post it here?

Offline nelsoc3

Junior Newbie




More cowbell!


« Reply #12 - Posted 2004-12-01 17:04:14 »

Quote
Before you do that though, could you take a full snapshot of Eclipse and post it here?


I'll try getting another copy and see if that works... Thanks for all the help!

Here's the pic - I'm assuming that's what you mean by snapshot:
http://img16.exs.cx/img16/2606/eclipse-screen.jpg
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.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!