HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

What more can be done to add quality to Swing JPanel drawings?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
qualitycandrawingswhatmoreswingjpaneldoneadd

Problem

I'm sorry if I have not worded the question title to well, so let me explain.

I am creating a game, actually a few and I see that most of the time when painting ect I use a JPanel, so I went and found a good method of painting (i.e it has great performance when multiple sprites etc are being painted) see here: https://stackoverflow.com/questions/1963494/java-2d-game-graphics. I implemented that into my own kind of GameJPanel which will allow me to always have performance when painting on my games JPanels. Now to that I have added the methods like setDoubleBuffered(true) and RenderHints on when drawing:

if (isTextRenderHintsOn()) {
                    bg.setRenderingHints(textRenderHints);
                }
                if (isImageRenderHintsOn()) {
                    bg.setRenderingHints(imageRenderHints);
                }
                if (isColorRenderHintsOn()) {
                    bg.setRenderingHints(colorRenderHints);
                }
                if (isInterpolationRenderHintsOn()) {
                    bg.setRenderingHints(iterpolationRenderHints);
                }
                if (isRenderHintsOn()) {
                    bg.setRenderingHints(renderHints);
                }


now without going to other libraries etc what more can I do to adjust the quality and or performance of whats being drawn on the JPanels?

It might also help to know that I use the GraphicsEnviroment and set the JFrame with the JPanel to full screen to gain a hardware accelerated image via a Buffer Strategy (not the linked performance drawing in swing also used some great techniques as is) below is an example of my GameDrawLibrary which implements the Java2D game graphics links' method and the rendering hints, the FullScreen class will be used for setting to and from full screen mode and the Test driver is just that:) :

Test Driver:

```
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.Action

Solution

It's a rather interesting piece of code, I'm going to enjoy reading it further when I have more time ;)

About the only things that jump out at me is how you are scaling your graphics. I'd suggest having a read through http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html as it discuss getScaledInstance and some alternative algorithms.

That aside, I was told, some years ago, you can fake anti aliasing by scaling a image down by 4. That is, if you want an output of a image at 800*600, you need to start with an image 4 times that size and scaling it down, which will produce a "fake" anti aliasing that does not rely on the hardware to render.

This may no longer hold true, but it might be of some worth to you

Context

StackExchange Code Review Q#14460, answer score: 6

Revisions (0)

No revisions yet.