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 (600)
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  
  Image Cropping  (Read 1402 times)
0 Members and 1 Guest are viewing this topic.
Offline Xalzar

Junior Newbie




Java games rock!


« Posted 2004-06-22 01:28:55 »

Hi all, my first post here!

I'm trying to put all my character animations in one GIF file, read it in then chop it up and put each piece into an array. I got it working fine in a JApplet, using CropImageFilter ect, but I want it to happen in a JFrame.

The problem is you can't use getDocumentBase() in a JFrame, so reading in is a bit different. My question is, do I have to use ImageIcon, and if so, how do I chop an ImageIcon up?

Please be patient, I'm still new to graphics in Java.  Smiley
Offline kevglass

« JGO Spiffy Duke »


Medals: 211
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2004-06-22 04:31:25 »

Use ImageIO to read the image (javax.imageio.ImageIO). Loading it via the ClassLoader using getResource() or getResourceAsStream() might be worth while. If you use this then you'll get a BufferedImage on which you can call getSubImage() which will grab a section of the image.

Kev

Offline crystalsquid

Junior Devvie




... Boing ...


« Reply #2 - Posted 2004-06-22 07:44:50 »

Do you really want to do split the image up? We use multiple animation frames in our games but leave them all on a single image. I presume you want do draw a specific frame of animation, if so this is what you need:

1  
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)


This will draw a subsection of the image. To get a specific frame from the image either store the offsets into your array and read from them when you come to draw:
1  
2  
3  
4  
void drawFrame(Graphics g,Image i, int x, int y, int frame)
{
    g.drawImage(i, x, y, x+WIDTH, y+HEIGHT, frameOffsetX[frame], frameOffsetY[frame], frameOffsetX[frame]+WIDTH, frameOffsetY[frame]+HEIGHT, null);
}

WIDTH & HEIGHT are the sizes of the frames themselves.

If the frames are all the same size and arranged:
1  2  3  4
5  6  7  8
9 10 11 12
13 14 15 16
etc.

just convert the index into the offsets like so:
1  
2  
int sx1 = (frame&3)*WIDTH; // Same as (frame % 4) - modulus
int sy1 = (frame>>2)*HEIGHT; // Same as (frame / 4) - divide

This saves using the arrays for the offsets into the image.

Its good to get into the habit of doing this, as when it comes to accelerating images with hardware you get better perfromance by using a few large images than many small ones.

I hope this helps, otherwise it should give you something to think about Wink
Good luck with your game programming,

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

Junior Newbie




Java games rock!


« Reply #3 - Posted 2004-06-22 09:18:38 »

This works pretty well. Thanks a lot, just one quick question, is this the best way to read in an Image in a JFrame:

1  
tempImage = getToolkit().getImage("Hero.gif");


Or is there a faster way?
Offline Bombadil

Senior Devvie





« Reply #4 - Posted 2004-06-22 10:21:07 »

Quote
1  
tempImage = getToolkit().getImage("Hero.gif");

Or is there a faster way?

Faster I can't judge but I find the following more natural:
Quote
import javax.imageio.ImageIO
BuifferedImage bimg = ImageIO.read(new File("Pic.png"));

... because there's also an equivalent ImageIO.write() method.

With
Quote
String[] list = ImageIO.getReaderFormatNames();
and
Quote

list = ImageIO.getWriterFormatNames();

.. you'll get the implemented readers and writers. For Java 1.4.2 this currently is:
Quote

Read: [Png, Jpg, Gif]
Write: [Png, Jpg]

In a related thread Cas mentioned some code how to change the compression ration for Jpegs... http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1087844685;start=5#5
Offline Xalzar

Junior Newbie




Java games rock!


« Reply #5 - Posted 2004-06-22 12:15:04 »

Wow, you guys have been really helpful. I've got my own little guy moving around now!

So here's another question: can anyone point me to a good tutorial on making a scrolling tile-based map, if any exist? I've found a couple of games with source code but it all goes over my head.
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #6 - Posted 2004-06-22 12:33:03 »

A tutorial... mh... well the GAGE library has supports that kind of stuff.

However, it isn't that difficult Smiley

-each tile has a number
-the map is a 2d array with ints

let's say you use 640x480 and 32x32 tiles

640/32=20
480/32=15

So you have at least 20*15 tiles on screen.

If it scrolls horizontally it's 21*15.

If it scrolls vertically it's 20*16.

If it scrolls in both directions it's 21*16.

Why? Well, because you have tiles wich are partial visible at the edges Wink

So grab pen&paper and draw some sketches (thinking is fun Grin). I'm sure you are able to figure it out. Btw you can just ignore these cases were you have a row/column less Smiley

弾幕 ☆ @mahonnaiseblog
Offline kevglass

« JGO Spiffy Duke »


Medals: 211
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2004-06-23 04:32:18 »

Quote

Its good to get into the habit of doing this, as when it comes to accelerating images with hardware you get better perfromance by using a few large images than many small ones.


This is only true for systems with more video memory than your going to use. If you're going to be going over the memory size (targeting at low end machine maybe) then smaller images make more sense since this gives you a high granularity to swap in and out images. The graphics card is more likely to find an optimum set of images that don't require swapping during rendering of the paritcular level/area.

Kev

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 (29 views)
2014-12-15 09:26:44

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

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

BurntPizza (75 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 (47 views)
2014-11-29 21:32:03

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

toopeicgaming1999 (100 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!