Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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  
  Making Images In Java  (Read 613 times)
0 Members and 1 Guest are viewing this topic.
Offline Bearded Cow

Senior Newbie




¬..¬


« Posted 2011-12-01 22:34:59 »

I recently discovered the Java4k competition, after looking at a few of the games that were on the website, it began to amaze me how good they were. The thing that I am trying to get at is that looking at the games most of them involve use sprites, and after looking at some of the source code, I don't quite understand how the images are done. Could any of you please explain how they do it. Thanks. 
Offline Z-Man
« Reply #1 - Posted 2011-12-01 22:40:09 »

The way I'd do it is to just create an image then draw on it, but I've never done Java4k so I don't know how the pros do it. Here is a very simple example.
1  
2  
3  
4  
5  
6  
7  
// Create an image of the size you want
BufferedImage img = new BufferedImage(10, 10);
// Get the Graphics2D object for the image
Graphics2D g = img.createGraphics();
// Do all your drawing
g.setColor(Color.BLACK);
g.fillRect(0, 0, 10, 10);
Offline Skarion

Senior Member


Medals: 2
Projects: 1



« Reply #2 - Posted 2011-12-01 22:41:43 »

http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

If you using a .jar you will need to use something can track it's position, for example .getResource().
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bearded Cow

Senior Newbie




¬..¬


« Reply #3 - Posted 2011-12-01 22:52:06 »

Take a look at this.

http://www.java4k.com/index.php?action=games&method=view&gid=332


I have used, the simple g.draw/fill function a lot but these seem to do it a lot more effectively.

Offline Groboclown
« Reply #4 - Posted 2011-12-01 23:06:19 »

As the link above showed, one method is to use Image references:
1  
2  
// create a sprite image using alpha + RGB channels
BufferedImage sprite = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

You can then set the individual pixels from an int array:
1  
2  
setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 
sprite.setRGB(0, 0, width, height, pixel_array, 0, 8);

and draw it using Graphics.drawImage.

The trick, then, is filling the pixels.  These are encoded per integer as per-byte values:
1  
pixel_array[offset + (y-startY)*scansize + (x-startX)] = (alpha_channel << 24) | (red_channel << 16) | (green_channel << 8) | blue_channel;


Likewise, you can use a single BufferedImage for the whole screen, and write directly to that, and draw it with one call to drawImage.

Offline Bearded Cow

Senior Newbie




¬..¬


« Reply #5 - Posted 2011-12-01 23:52:01 »

Thanks.
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 (50 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (208 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!