Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
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  
  Scalling things up!  (Read 1650 times)
0 Members and 1 Guest are viewing this topic.
Offline JPG2000
« Posted 2014-07-02 03:46:44 »

Pre-note: I have looked for this topic before, and have not found one matching my needs, so please keep that in mind if there is a similar thread. Also, execute any punctuation issues, I am typing this on my phone.

Hello,

I am looking to scale up my java game. Currently, my game is 800 by 800 pixels,  and drawn by using the graphics object (and by invoking the drawImage method).

I am looking for a really easy way to upscale my images, please note that I am drawing many different images at the same time.

I have tried upscaling my self, but it hasn't worked. I troed upscaling each thing drawn by the amount of pixels above the original 800, but that ended up with some weird rendering issues. Is there a way to create a buffered images, and add other Images to that? If so, then I could simply use the two integer spots in the method to expand the image through the screen hight and width, witch are both 800 pixels.

A second note: I want to upscale the images,  not change the aspect ratio. So keep in mind I don't want to expand the camera range etc.

Could you guide me with upscaling, particularly by using Java's Swing?

Thanks!

- Jaks
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-07-02 03:58:44 »

If your paint() looked like this:

1  
2  
3  
public void paint(Graphics g) {
    g.drawStuff();
}


Change it to this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
BufferedImage buffer = new BufferedImage(800, 800, /* whatever you need */);

public void paint(Graphics g) {
    Graphics bg = buffer.getGraphics();
    bg.drawStuff();
    bg.dispose();

    Graphics2D gg = (Graphics2D) g;
    gg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // alternatively a different interpolation type, experiment with it

    gg.drawImage(buffer, 0, 0, getWidth(), getHeight(), null); // stretch buffer to fit (getWidth(), getHeight())
}


This method simply renders everything to a buffer, and then re-sizes the whole buffer on render. Yes, it incurs the penalty of an extra draw, but it shouldn't be too bad.
Since this is manual double buffering as well, you can setDoubleBuffering(false); on whatever it is you are rendering to, don't need to waste more render time.
Offline JPG2000
« Reply #2 - Posted 2014-07-02 04:07:45 »

Thank you!

I have a quick question though: What is the point of disposing the buffer graphics,  and what's the point of changing the rendering hint. Also why do you make a new graphics just toto draw,  could we of used the original g?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #3 - Posted 2014-07-02 04:14:13 »

You have to create the new graphics or else you aren't drawing to the buffer, and the rendering hint isn't strictly required, but it changes the image scaling algorithm used, the default is Nearest Neighbor, which can look quite awful in most situations. Again, experiment with the three different interpolation values to see what works best.

EDIT: we dispose the bg because we created it, and you should always dispose resources you acquired yourself, it's done each frame because I find that you can get weird issues with out of sync buffers if you try to cache the buffer's graphics between frames. It's not too expensive to my knowledge, so just getGraphics() and dispose() each frame.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (847 views)
2019-09-04 15:33:17

hadezbladez (5795 views)
2018-11-16 13:46:03

hadezbladez (2603 views)
2018-11-16 13:41:33

hadezbladez (6211 views)
2018-11-16 13:35:35

hadezbladez (1499 views)
2018-11-16 13:32:03

EgonOlsen (4734 views)
2018-06-10 19:43:48

EgonOlsen (5793 views)
2018-06-10 19:43:44

EgonOlsen (3276 views)
2018-06-10 19:43:20

DesertCoockie (4175 views)
2018-05-13 18:23:11

nelsongames (5501 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!