orel27bit
Junior Newbie
|
 |
«
Posted
2013-10-26 16:34:36 » |
|
Hello, i'm new here. I am very confused and curios. If you'll take a look over the internet for Java game programming tutorials you'll find that people are teaching two common ways for rendering(in pure Java): 1.Using just an Image at the size of the window: 1 2
| Image image = createImage(1024,768); image.getGraphics().drawString("My string!",100,100); |
2.Turning a BufferedImage into an Integer array for pixel manipulation: 1 2 3
| BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); pixels[myPixel] = 0xFFFFFF; |
I want to ask you which of them is better, and why? and if no, can you recommend me another method? I'm very curios about this and i want to develop my own game, but i'm still planning.
|
|
|
|
Opiop
|
 |
«
Reply #1 - Posted
2013-10-26 16:38:18 » |
|
BufferedImage. It loads data from a texture file and allows you to draw pixels straight to the screen. Its more low level, but the first way you have up there is much slower, but easier.
|
|
|
|
orel27bit
Junior Newbie
|
 |
«
Reply #2 - Posted
2013-10-26 16:39:51 » |
|
I'm programmng in java for like a year now, and still can't really get the idea of the bufferedimage with loading sprites and manipulating everything, but thanks for the fast answer.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Abuse
|
 |
«
Reply #3 - Posted
2013-10-26 17:09:05 » |
|
It depends upon:
- what precisely you're drawing, and, - how well the hardware acceleration is implemented on your target platform.
|
|
|
|
StumpyStrust
|
 |
«
Reply #4 - Posted
2013-10-27 00:40:53 » |
|
Please search forums first.
Loading an image is very easy. Just ImageIO.read(ClassLoader.getSystemResource(path)); where path is the location of your resource. Then use the Graphics object to draw the image.
g2d.drawImage(x,y,w,h) is the FASTEST method of drawing images in java2D. The speed is based on your drawing surface when doing this though. Drawing to a Volatile Image or canvas will be fast. Drawing to an other BufferedImage will be slow.
Reality check. If you want to do anything more then simple sprite based games, use some opengl engine.
|
|
|
|
Dxu1994
|
 |
«
Reply #5 - Posted
2013-10-27 00:43:55 » |
|
It depends on what you want.
First method uses Java2D to render, which tries to use DirectX, then OpenGL and if both fail it will use a software renderer.
Second method uses your own software renderer, but it can be a lot more flexible than Java2D.
|
|
|
|
kpars
|
 |
«
Reply #6 - Posted
2013-10-27 22:08:56 » |
|
I usually end up basing my core class off of the applet class, Then I just work from there. I create an image ( not a BufferedImage) called 'display', 'canvas', 'screen', etc. Then I define it in the run method as a volatileImage. These are handy little things and work quite well. This is what my usual render method ends up looking like, if I don't scale the game down: 1 2 3 4 5 6 7 8 9 10 11
| private void render() { Graphics g = display.getGraphics(); g.setColor(new Color(0, 0, 0)); g.fillRect(0, 0, WIDTH, HEIGHT);
g = getGraphics(); g.drawImage(canvas, 0, 0, WIDTH, HEIGHT); } |
I had the boilerplate code for some sort of demo that used this method, I'll upload the code whenever I have the time. - Jev.
|
|
|
|
lcass
|
 |
«
Reply #7 - Posted
2013-11-02 00:18:24 » |
|
Lots of tutorials on the youtubes to get you started such as thechernoproject which runs through producing a game in the buffered image manor. HOWEVER , only use the code as a basic template so only really go as far as learning how it draws the pixels and how to load images if you start messing around into other peoples rendering conventions you most likely will find yourself confused and stuck on meager issues that will effect further project development.
|
|
|
|
Opiop
|
 |
«
Reply #8 - Posted
2013-11-02 01:36:18 » |
|
+1 * Infinity for theChernoProject. He is possibly the most helpful and most thorough youtuber there is that deals with Java. He doesn't even just create game tutorials, he's also doing networking which involves GUI and other fantastic things! I have to say he's probably one of the major reasons why I switched to Java from C++!
|
|
|
|
lcass
|
 |
«
Reply #9 - Posted
2013-11-18 22:00:48 » |
|
Just a side note if you are looking for anything that is syntax based take a look at the newbostons intermediate and beginner java tutorials they explain almost everything.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
|
philfrei
|
 |
«
Reply #11 - Posted
2013-11-19 01:06:14 » |
|
I had the boilerplate code for some sort of demo that used this method, I'll upload the code whenever I have the time. @kpars - am looking forward to this upload!
|
|
|
|
unenergizer
Junior Devvie   Medals: 3Exp: 5 years
Work hard, practice often, and you will succeed!
|
 |
«
Reply #12 - Posted
2013-11-19 01:15:11 » |
|
@orel27bit Good question, I have been working the same thing myself.  Thank you for posting this topic. It has helped me too! 
|
JGO is AMAZING! Steam: unenergizer
|
|
|
Kyperbelt
|
 |
«
Reply #13 - Posted
2013-11-19 01:32:49 » |
|
i would suggest to just use the draw calls given by java. i too recently asked the same question and came to figure out that pixel manipulation of an image is much slower than just drawing said image. and you dont get all the built in controls (e.g transforms) . Also you cant have good resolutions with per pixel rendering since the better the res the slower it will run.
So like suggested before g.drawImage seems to do the trick quite nicely.
|
|
|
|
kpars
|
 |
«
Reply #14 - Posted
2013-11-19 07:40:50 » |
|
I had the boilerplate code for some sort of demo that used this method, I'll upload the code whenever I have the time. @kpars - am looking forward to this upload! I wrote something very similar a few days before I made that post, actually http://kemoy.net/JevDocs/Java2DPhysx/- Jev
|
|
|
|
wessles
|
 |
«
Reply #15 - Posted
2013-11-20 04:01:34 » |
|
I have to say he's probably one of the major reasons why I switched to Java from C++!
Same here. My youtuber was thenewboston... Bacon, Apple Pie, and Chicken got me through polymorphism  ...
|
|
|
|
lcass
|
 |
«
Reply #16 - Posted
2013-11-24 11:35:30 » |
|
 I like the new boston , and his er "Extravagant" Backgrounds.
|
|
|
|
tyeeeee1
|
 |
«
Reply #17 - Posted
2013-11-24 20:15:42 » |
|
What do you all think about this method of drawing a loaded image to the screen? I first load the image using this code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package utilities;
import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.image.BufferedImage;
public class ImageUtilities { private static GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); public static BufferedImage toCompatibleImage(BufferedImage image) { if(image.getColorModel().equals(graphicsConfiguration)) { return image; } else { BufferedImage newImage = graphicsConfiguration.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency()); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); image.flush(); return newImage; } } } |
Then just do a g.drawImage(); statement whenever I'm drawing the image. The images aren't being constantly loaded by the way. If I'm in a certain level then only images used in that level are kept loaded and reused until I don't need them.
|
|
|
|
CodeHead
|
 |
«
Reply #18 - Posted
2013-11-24 20:56:46 » |
|
What do you all think about this method of drawing a loaded image to the screen? Maybe it's just my old C++ habits talking, but shouldn't the responsibility of cleaning up created objects be left to the creator? In this case your class is freeing resources that weren't allocated by it. Maybe not a biggie in your use case, but something that popped out at me.
|
Arthur: Are all men from the future loud-mouthed braggarts? Ash: Nope. Just me baby...Just me.
|
|
|
|