Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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 2490 times)
0 Members and 1 Guest are viewing this topic.
Offline Pinguinpanic

Junior Member


Medals: 3



« Posted 2011-12-31 11: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: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-12-31 11: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

Junior Member


Medals: 3



« Reply #2 - Posted 2011-12-31 11: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!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


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

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

Offline Pinguinpanic

Junior Member


Medals: 3



« Reply #4 - Posted 2011-12-31 12: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: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-12-31 12: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

Junior Member


Medals: 3



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

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

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-12-31 13: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 13: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 intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Pinguinpanic

Junior Member


Medals: 3



« Reply #9 - Posted 2011-12-31 13: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!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2011-12-31 14: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
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


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

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

Offline dishmoth
« Reply #12 - Posted 2011-12-31 15: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: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-12-31 16: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.

 

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

The first screenshot will be displayed as a thumbnail.

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

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

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

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

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

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

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

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

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!