Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  [Help] Best method for relative image loading and display using SWING/AWT  (Read 2818 times)
0 Members and 1 Guest are viewing this topic.
Offline antelopeDJ

Junior Devvie


Projects: 2


Java Developer on the Weekends!


« Posted 2012-03-04 17:01:04 »

I want to display an image that is loaded into the program itself and displayed on the screen at X/Y coordinates I chose. It also needs to load relative or within the program (in a jar, for instance). Any help?
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-04 23:39:59 »

1  
BufferedImage image = ImageIO.read(<ClassName>.class.getClassLoader().getResource("res/myImage.png"));


The path is relative to the current working directory, aka the "bin" folder of Eclipse or the folder and the root of the JAR file.

Offline _Al3x

Senior Devvie


Medals: 7


Indie Games FTW!


« Reply #2 - Posted 2012-03-04 23:45:52 »

What he said, also:

1  
g.drawImage(image,x,y,null)


(Not sure what that null does tho... Huh)

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

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-03-05 01:26:36 »

The null is for an ImageObserver, it's only if you want to keep track of how much it's been painted on the screen. Mostly useless. Smiley

Offline antelopeDJ

Junior Devvie


Projects: 2


Java Developer on the Weekends!


« Reply #4 - Posted 2012-03-05 16:43:21 »

1  
BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("res/myImage.png"));


The path is relative to the current working directory, aka the "bin" folder of Eclipse or the folder and the root of the JAR file.

So... can I then call that image using the code Alex posted, which is the method I used when it was non-relative?

1  
g.drawImage(image,x,y,null)

Update: Due to some weird difference between Graphics and Graphics2D, loading an image only works with Graphics2D and throws an exception on Graphics.
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #5 - Posted 2012-03-05 17:29:53 »

Update: Due to some weird difference between Graphics and Graphics2D, loading an image only works with Graphics2D and throws an exception on Graphics.

There must be a problem somewhere in your code.  Graphics/Graphics2D don't have anything to do with the loading of images, and all Graphics instances are in fact Graphics2Ds, at least in Sun's implementation (this is why you can always just cast it without first doing an instanceof), so there shouldn't be any difference in behavior between them.  Post some code that shows the problem you're having.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2012-03-08 00:47:20 »

@BoBear2681:
Actually that is incorrect. According the Java API Graphics2D extends Graphics, thus not all graphics instances are graphics2ds. Although both classes can access a drawImage method...
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #7 - Posted 2012-03-08 01:03:07 »

Yes, but in *Sun's implementation,* it is indeed the case that all Graphics instances passed to Component#paint() are actually Graphics2D's.  That's why you see tons of examples everywhere where people cast to Graphics2D without checking first.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-03-08 01:42:55 »

The Graphics parameter is there only to preserve backwards compatibility.

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #9 - Posted 2012-03-08 05:19:45 »

FWIW, you can get a Graphics that doesn't subclass Graphics2D still today; for example, you could use javax.swing.DebugGraphics.  People don't use that in real life, of course.

Some places online also whisper of getting a different Graphics subclass when printing vs. painting a component, and indeed, there is a ProxyPrintGraphics class in the JDK, among others, that doesn't inherit from Graphics2D.  Not sure if/when it's still used, and of course I'm too lazy to try to print a Component to see what happens!

AFAIK other vendors' JVM implementations such as IBM, HP, etc. are always based off Sun's and always end up using Graphics2D's for standard UI painting.

But in short - yes, for an AWT-based game you'll always be getting Graphics2Ds.
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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!