Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Best rendering method using pure java?  (Read 4709 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« 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.

Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« 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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline 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.

Offline 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.

Online kpars

JGO Wizard


Medals: 91
Projects: 4
Exp: 3 years


Extreme Typist.


« 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);

        // Render Stuff. I.E: screen.render(g);

        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.

Offline 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.
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« 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++!

Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #10 - Posted 2013-11-19 00:23:07 »

I started one, and uploaded it to Gist:

https://gist.github.com/ClickerMonkey/7534326

It's a software graphics library with blending, textures, and simple primitives.

Checkout https://gist.github.com/ClickerMonkey/7534326/raw/7a472d6541a9be059eba3735a7e6e19e7264e5e1/Test.java to see what the drawing code is like.

Offline philfrei
« Reply #11 - Posted 2013-11-19 01:06:14 »

Quote
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!

"It's after the end of the world! Don't you know that yet?"
Offline unenergizer

Junior Duke


Medals: 3
Exp: 1 year


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.  Huh

Thank you for posting this topic.  It has helped me too!  Smiley

JGO is AMAZING!
Steam: unenergizer
Offline Kyperbelt

Junior Duke


Medals: 1



« 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.
Online kpars

JGO Wizard


Medals: 91
Projects: 4
Exp: 3 years


Extreme Typist.


« Reply #14 - Posted 2013-11-19 07:40:50 »

Quote
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 Cheesy
http://kemoy.net/JevDocs/Java2DPhysx/

- Jev

Offline wessles

JGO Wizard


Medals: 67
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« 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  Smiley...

Offline lcass
« Reply #16 - Posted 2013-11-24 11:35:30 »

Cheesy I like the new boston , and his er "Extravagant" Backgrounds.
Offline 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;

/**
 *
 * @author Valkryst
 * --- Last Edit 24-Oct-2013
 */

public class ImageUtilities {
   private static GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
   
   /**
    * Checks if the specified image is already compatible with the current display.
    * If the image is compatible with the current display then return the image as-is.
    * If the image isn't compatible with the current display then run the code to make it compatible then return the compatible image.
    *
    * @param image The image to make compatible.
    * @return Returns either the same image that came in or the compatible version of that image.
    */

   public static BufferedImage toCompatibleImage(BufferedImage image) {
      // Check if the image is compatible with the current display. If it is then return the image, else make the image compatible and return it.
      if(image.getColorModel().equals(graphicsConfiguration)) {
         return image;
      } else {
         // Create a Buffered Image compatible with the current display.
         BufferedImage newImage = graphicsConfiguration.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
         
         // Create a graphics context and render the incompatible image onto the compatible image.
         Graphics2D g = newImage.createGraphics();
         g.drawImage(image, 0, 0, null);
         
         // Dispose the old image and return the new compatible image.
         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.
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« 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?

1  
2  
// Dispose the old image and return the new compatible image.
image.flush();


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.
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.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (53 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (43 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!