Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  drawImage not working  (Read 1436 times)
0 Members and 1 Guest are viewing this topic.
Offline Pinguinpanic

Senior Newbie


Medals: 2



« Posted 2011-12-31 12:42:16 »

Hello there, Java-Gaming people. I'm new to the forum, so maybe a little introduction is in order. So I'm this guy from the Netherlands, I have programmed in Gamemaker quite extensively, with which I started at a young age. Mind you I didn't do the Drag and Drop stuff, but "proper" coding, and I have in gamemaker constructed some stuff that I think is pretty nifty, I have for example at a point programmed an online top down shooter from scratch (as far as that goes in gamemaker). However, this year at university (First year Software Science), I was introduced to Java, and after some tests I found out that it is an insane amount faster then Gamemaker, so I decided to move to Java, and I have succeeded(sp?) in creating a simple mario platformer, with image animations, a particle system and some gibberish, but I have now moved on to a new more structured project (An isometric RTS), BUT somewhere something must be going wrong because it does not draw images.

Tl;dr:
This is an extract from my script, this is the image initialization:
1  
2  
3  
4  
5  
6  
public class Tile extends GameImage
{
  int x,y;
  int type;
  TileGrid grid;
  Image image=Toolkit.getDefaultToolkit().getImage("Grass.png");

And this is the "draw" script, which I know to work, since it does draw the lines, but it does not draw the image, and I have no idea why.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
  public void draw(Graphics2D g)
  {
    int dx,dy;
    dx=grid.X(x);
    dy=grid.Y(y);
    g.drawImage(image,dx-16,dy-8,null);    
    g.drawLine(dx-16,dy,dx,dy-8);
    g.drawLine(dx,dy-8,dx+16,dy);    
    g.drawLine(dx-16,dy,dx,dy+8);
    g.drawLine(dx,dy+8,dx+16,dy);        
  }


Help would be appreciated (:, in the meanwhile this forum has quite some resources and information to absorb.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #1 - Posted 2011-12-31 12:50:43 »

What are the image's width and height? If they are both -1, then that means your image was not found and an empty Image object was returned.

Offline Pinguinpanic

Senior Newbie


Medals: 2



« Reply #2 - Posted 2011-12-31 12:52:40 »

Dimensions are 32x16, but that is not stated anywhere in the code, should the program somehow know these values?

EDIT:
I added System.out.println(image.getWidth(null));to test what you said, and it returns -1, so you seem to have nailed the problem. Also that was a pretty fast response Tongue. So how would I fix it loading an empty image?
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #3 - Posted 2011-12-31 12:55:26 »

You get the Image's width and height by doing:
1  
System.out.println(image.getWidth(null) + " " + image.getHeight(null));

Offline Pinguinpanic

Senior Newbie


Medals: 2



« Reply #4 - Posted 2011-12-31 13:00:17 »

I fixed the problem by replacing the getImage URL with this:
Image image=Toolkit.getDefaultToolkit().getImage("C:/Users/Bob/Desktop/Java game engine/Skeleton/Grass.png");

How would I go about loading Grass.png from my current path? Because it is a bit silly to do it like this.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #5 - Posted 2011-12-31 13:09:45 »

It is best to use the Class.getResource(String) method. Using "grass.png" will look for the file inside the package (folder) of that Class. Using a forward slash ("/") at the beginning will look from the root of your package hierarchy.

So let's say I have a class called MyClass inside package com.ra4king.mypackage, then MyClass.class.getResource("grass.png") or myClassInstance.getClass().getResource("grass.png") will look for that file inside the mypackage folder. The same file will be found if I do MyClass.class.getResource("/com/ra4king/mypackage/grass.png") or the getClass() way.

Offline Pinguinpanic

Senior Newbie


Medals: 2



« Reply #6 - Posted 2011-12-31 13:49:17 »

All's working smooth now, thanks for your quick and usefull responses!
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #7 - Posted 2011-12-31 14:22:50 »

Glad to help Smiley

Don't forget to Appreciate (click the Appreciate button to the right of the Quote button) anyone that helps you on this forum. Keeps moods up Grin

Offline nsigma
« Reply #8 - Posted 2011-12-31 14:46:29 »

Don't forget to Appreciate (click the Appreciate button to the right of the Quote button) anyone that helps you on this forum. Keeps moods up Grin

Me love you long time.

ra4king, you're a gratuitous medal whore!  Tongue

 Grin

Praxis LIVE - Open-source graphical environment for developing intermedia performance tools, projections and interactive spaces.
Praxis LIVE on Twitter
Offline Pinguinpanic

Senior Newbie


Medals: 2



« Reply #9 - Posted 2011-12-31 14:58:16 »

Where is this appreciate button you speak of?


EDIT: it just appeared, maybe you need 5 posts to be able to appreciate?
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #10 - Posted 2011-12-31 15:18:21 »

Don't forget to Appreciate (click the Appreciate button to the right of the Quote button) anyone that helps you on this forum. Keeps moods up Grin

Me love you long time.

ra4king, you're a gratuitous medal whore!  Tongue

There's a reason he ranks #1 in awkwardness!
   http://www.java-gaming.org/index.php?action=social
Oh how I love my closed source queries.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #11 - Posted 2011-12-31 15:51:02 »

I may be loud, but you can't ignore me Grin

Offline dishmoth
« Reply #12 - Posted 2011-12-31 16:48:29 »

I may be loud, but you can't ignore me Grin

ra4king -> Post visibility -> Hide all posts Tongue

Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #13 - Posted 2011-12-31 17:22:28 »

But I know you won't do that because you <3 me persecutioncomplex

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (87 views)
2013-05-16 21:24:48

gouessej (117 views)
2013-05-16 00:53:38

gouessej (112 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (185 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.143 seconds with 21 queries.