Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  Loading just part of an image  (Read 1958 times)
0 Members and 1 Guest are viewing this topic.
Offline 0circle0

Junior Member


Medals: 7



« Posted 2012-01-20 04:51:46 »

1  
Block = getImage(this.getClass().getResource("/Sprites/maptiles.png"));


Is the code I use to load an image. Is there a way to only load a portion of the png file to Block. Trying to take a tile set and put it into an array without splitting the image physically and loading it that way.

Thanks.

I was trying to use a bufferedImage but couldn't figure out how to draw that buffered image to the "screen"
tried
1  
g.drawImage(Block, 0, 0, this);

and tried
1  
2  
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(Block, 0, 0, this);


and google is failing me today.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-01-20 04:54:43 »

You cannot only load a "portion" of an image file. To split up a spritesheet, look into the BufferedImage.getSubimage(int x, int y, int width, int height) method.

And what is wrong with both ways? What does that Graphics2D instance draw to? Does it draw directly on to the screen or a backbuffer? In the end, I need code to see exactly what might be wrong Smiley

Offline 0circle0

Junior Member


Medals: 7



« Reply #2 - Posted 2012-01-20 05:01:03 »

Code compiles with the Graphics2D but nothing is displayed and no error is in the console.

Note: I don't actually know how to program in Java. I am learning on my own with no references other than google and the API website associated with Java

But here is the code. It is complete and for me it compiles just fine just doesn't display what I want it to. There are a lot of dummy functions and an A* algorithm that I completed. You may also note that I am converting my program from JavaME to JavaSE. I have it working flawlessly on as a phone app but the syntax is a little different for SE. Code is long sorry. 770+ lines but from 187 and below is the A* algorithm and random map generation.

Replaced the code block with a link to the rar. Self compiling batch file and images included.
RPG Generation

Not an RPG as in Role playing game. But a demonstration of A*

Edit: bookmarked that link, that will help a lot with Buffered Images, thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2012-01-20 07:21:26 »

Heh if you don't know how to code in Java, then I strongly recommend you don't start with graphics so soon. I suggest you read this post Smiley

EDIT: Btw, the HTML file runs fine for me. I see a map of various tiles. I only get this in the console:
1  
2  
3  
4  
5  
6  
7  
Exception in thread "Thread-13" java.security.AccessControlException: access denied ("java.awt.AWTPermission" "watchMousePointer")
   at java.security.AccessControlContext.checkPermission(Unknown Source)
   at java.security.AccessController.checkPermission(Unknown Source)
   at java.lang.SecurityManager.checkPermission(Unknown Source)
   at java.awt.MouseInfo.getPointerInfo(Unknown Source)
   at RPG.run(Unknown Source)
   at java.lang.Thread.run(Unknown Source)

Offline 0circle0

Junior Member


Medals: 7



« Reply #4 - Posted 2012-01-20 14:34:58 »

The error if I am correct is due to having to accept a script. Not sure I haven't received that error.

ops...That is the entire picture. I am just trying to get a single tile off that with the "block[]" array which is the BufferedImage where Block is the Image. Sorry about that forgot to change that before I rar'd it.

1  
2  
3  
4  
5  
6  
public void paint(Graphics g)
   {
      //g.drawImage(dbImage, 0, 0, this);
     Graphics2D g2 = (Graphics2D)g;
      g2.drawImage(Block, 0, 0, this);
   }

should have looked like this, which doesn't display any image for me
1  
2  
3  
4  
5  
6  
public void paint(Graphics g)
   {
      //g.drawImage(dbImage, 0, 0, this);
     Graphics2D g2 = (Graphics2D)g;
      g2.drawImage(block[1], 0, 0, this);
   }


I am porting from C/C++, I understand the Java API, just not all of it. The Sprite Creator I posted here was my second project, this one was my first but it was originally designed for the phone. I just figured I could learn a bit more by converting it to the computer, that and I really don't care to have to test everything by uploading the app to my phone every time. The original code for JavaME is here if you have an applet viewer you can see what I am trying to do now that I have done with JavaME. It's code is basically the same just porting over. If you view the JavaME code just ignore the index.html file included with the .jar/.jad files Wink realized that you can't view the jar for an applet like you can with an app.

I was pointed to this
1  
2  
3  
4  
5  
public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        g2.drawImage(Block, 0, 0, this);
    }


I would
1  
2  
3  
4  
public void paint(Graphics g)
    {
        g.drawImage(Block, 0, 0, 32, 32, 64, 64, 96, 96, this);
    }

which does indeed draw a portion of the image. Am I able to take those sections of the image and put them into another image or do I have to use a BufferedImage? And if I have to use BufferedImage how would I draw that BufferedImage. I would like to put the smaller images into an array.

I guess if nothing else I could add a few variables to the map class to mark the location of each tile and where it is supposed to go and create a loop for drawing the tiles if I can't.

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #5 - Posted 2012-01-20 22:14:50 »

To split up a spritesheet, look into the BufferedImage.getSubimage(int x, int y, int width, int height) method.

I guess people these days don't read posts fully Wink

Offline 0circle0

Junior Member


Medals: 7



« Reply #6 - Posted 2012-01-24 10:15:38 »

only problem I am having though is that I can not for the life of me show the bufferedimage on the screen. From what I understand BufferedImage.getSubImage returns a bufferedImage
Offline Regenuluz
« Reply #7 - Posted 2012-01-24 11:00:45 »

You draw it to the screen like you'd draw a normal image. Smiley

This is how I do it:
1  
2  
BufferedImage sprite = sl.getSprite(0, 0);
g.drawImage(sprite, 10, 26, null);


and getSprite() looks like this:
1  
2  
3  
4  
5  
   public BufferedImage getSprite(int x, int y) {
      BufferedImage sprite = spriteSheet.getSubimage(x * tileSize, y * tileSize, tileSize, tileSize);

      return sprite;
   }
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #8 - Posted 2012-01-24 15:48:54 »

@Regenuluz
It is best to cut it all up and store in an array because getSubimage(...) is creating a new instance of BufferedImage....at 60FPS that is quite A LOT of memory Smiley

Offline Riven
Showcase Moderator

JGO Overlord


Medals: 610
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2012-01-24 15:51:47 »

@Regenuluz
It is best to cut it all up and store in an array because getSubimage(...) is creating a new instance of BufferedImage....at 60FPS that is quite A LOT of memory Smiley
getSubimage(...) shares the pixel-data with its 'parent', so basically, the overhead is minimal, even at 60fps.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #10 - Posted 2012-01-24 16:16:18 »

Quote
The returned BufferedImage shares the same data array as the original image.
Damn, I need to read the API more carefully :S

Offline 0circle0

Junior Member


Medals: 7



« Reply #11 - Posted 2012-01-24 17:02:35 »

You all have been a huge help thanks a ton. Just an insane amount of help on this site.

Edit:nvm found the problem with the question I was going to ask.
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.

xsi3rr4x (72 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (79 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (80 views)
2014-04-14 15:48:38

tom_mai78101 (104 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!