How to get the best graphical performance out of Java2D [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
This question does not apply to libraries such as LWJGL, JavaFX, etc. The goal here is to determine how to get the best performance out of Java2D. More particularly, how to get the best performance out of Java2D to create visually appealing yet efficient 2D games. I have come across quite a lot of practices: drawing directly onto a JFrame, overriding JPanel or JComponent, overriding Canvas to provide some nice double buffering strategies, using a library (ehm), etc.
In the past, I have simply overriden a JPanel and its paintComponent method, and, in the end, it ran at a steady 35 FPS! Not really the pinnacle of performance for a simple top-down 2D Java game (or maybe it is?).
So, what is the best strategy to get every drop of performance out of Java2D?

If you want to use Java's Graphics2D class to draw objects with nice performance, I recommend using VolatileImage. It is available since Java 1.4 and uses hardware accelerated processing (if possible) to render its stuff. It extends java.awt.Image so there are not so much changes for you, if you used BufferedImage or so before. But there are some tricky things using this class. For example before you render its content you need to check whether it is still valid, but usually it is not so hard to manage this stuff· But therefor you get pretty nice performance boost to your app, without the need to change a lot of code. :)
This link should give you good advice using this class.

Related

Trying to think an approach for creating a feature flag in React [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I'm trying to create a feature flag in my react project (I believe I have to add a feature flag in the appsettings.json file), the flag is to hide the features that are currently being developed as we don't want users to have those features when those are yet to be tested fully. So the current approach I have is to wrap the newly developed code with a condition like if(flag){new code} I'm sorry if the approach looks silly but that's all I can come up with as a rookie, so can you guys help with some feasible approach if possible?
So I've tried adding the if condition to an existing feature in the code base as the newly developed code is yet to be committed but it didn't work.
Based on your comment from three hours ago I was able to figure out what exactly you wanted to do.
So: already having the flag all there is to do is to use it. There are at least three ways you can do it:
make it hidden - i think MDN docs explain it well enough
make it transparent - this is what you did: setting opacity to 0
not render it at all
not rendering is achieved like this:
return (
<SomeParentComponent>
{EnableDivision &&
<Division/>
}
</SomeParentComponent>
)
It's not like one of them is better than others: they are all situational.
hidden element is rendered, and therefore has state and can be found in the DOM tree, but doesn't take any space
transparent is like hidden, but takes up space
not rendered on the other hand can't be found in the DOM tree, doesn't take any space, the funcion or class creating it is never run and therefore it also can't have any state
Hopefully this should dispel your doubts about how to do it.
Also if you ever needed use some flags in many different components in various places of your component tree take look at what React Context does.

Shrink an image to 1/4 of its original size. [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am a programming newbie. I am asked to shrink an image to 1/4 its original size. And tutor told me I can replace 4 pixel with one pixel, so as to make it 1/4. How Can I do replacing work in java. Can anyone give me an example?
If you are new to programming then this is absolutely a lesson you should not be undertaking because it involves file IO, loops, data structures, and math. None of these are relevant to the basics you should be learning now.
The basic algorithm would be that you read the pixels in the image into a matrix, and every 2x2 square of pixels could be replaces by one pixel by averaging the colors.
I am not going to give you a full answer because it would involve lots of API lookups to create a fully functional application to do this. There would be a lot of code you probably wouldn't even understand if I showed it to you.
If this is for school, you are either far behind in your studies, or else your teacher is giving an unecessarily complex early assignment. Regardless of where this is coming from, you need to ask for a simpler assignment.
Either way, I recommend you take a step back and solve some simpler problems first so that you understand the components necessary to solve this problem.

Clustering: Finding a Average Reading [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am looking into finding algorithm within the area of clustering or machine learning which will facilitate or creating a typical data reading for a group of readings. The issue is that it must facilitate time series data; thus some traditional (k-means) techniques are not as useful.
Can anyone recommend places to look or particular algorithms that would provide a typical reading and relatively simple to implement (in Java), manipulate and understand?
As an idea. Try to convert all data types into time, then you will have vectors of the same type (time), then any clustering strategy will work fine.
By converting to time I actually mean that any measurement or data type we know about has a time in its nature. Time is not a 4-th dimension, as many think! Time is actually 0-dimension. Even a point of no physical dimensions which may not exist in space, exists in time.
Distance, weight, temperature, pressure, directions, speed... all measures we do can be converted into certain functions of time.
I have tried this approach on several projects and it payed back with really nice solutions.
Hope, this might help you here as well.
For most machine learning problems in Java, weka usually works pretty well.
See, for example: http://facweb.cs.depaul.edu/mobasher/classes/ect584/weka/k-means.html

What coding issues can be significant in damaging a game's performance? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have just finished a 2D game in Java that I've been working on for a while.
I found that on some computers it runs fine, and on others (which are not particularly slow in general), the game runs a little slower, and with minor interruptions every second or so. Not completely smooth.
Since I am a beginner to making games, and probably since I didn't plan everything about the game in advance, the code of the game is rather long, clumsy and probably inefficient.
I want to improve this in my next projects. So my question is - In general, what would be the main causes for a common 2D game to slow down on a computer?
What should I pay the most attention to, next time, in order to design an efficient game?
Making a small amount of classes? (Even if the classes are small
ones?)
Avoiding repetition of code? (Even small sections of code, such as
short if statements).
Avoiding too many threads running?
Anything else?
Obviously, all of the above are recommended for an efficient program.
But I'd like to know, what in a game's code, could be especially significant for making an efficient application, and what would be less important and will not save significant amounts of memory.
Any advice would be welcome - could be regarding game design, or regarding more specific coding issues.
I don't know if this matters, but please note that I'm talking mainly about real-time games, using a 'game-loop' that constantly updates the game and the dispaly.
The important thing when trying to improve the performance of any program, not just a game, is - don't guess.
If I or anyone says "it's in your collection classes", or "it's in your rendering", or "it's in your memory management", or "it's in your compiler optimization", can you trust it?
Short answer - No - because it's a guess.
It could be true. It could be false. Nobody knows, in your case.
People who say instead of guessing "Use a profiler" are on the right track.
In my opinion there's an even better method, spelled out here.
If you need to know why, I'll explain it further, but the hard part for any programmer is to stop trying to think it out, and let the tool tell you what to look at.

Starting flow free game development [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm starting the developpement of a flow free game on android.
I'm a bit confused about the architecture design.
Should I use grids/tablelayout available on android framework or just draw my own objects by specifying the position and which object to draw.
I created classes for each object as : POINT, EMPTY, SQUARE.
I want to create a prototype like on those links :
http://forums.gamesalad.com/discussion/48641/my-flow-free-inspired-game-development-thread
For the moment, I don't have to design the IA, I just need to focus on the game architecture and physics.
Should I use grids/tablelayout available on android framework or just draw my own objects by specifying the position and which object to draw.
You shouldn't use grids/tablelayout.
For 2D games where you need performance, use OpenGL or take a look at the andengine library.
Since this is going to be a simple 2D game, you should look at Canvas. Take a look at Android Sample Project > TicTacToeLib and Android Sample Project > TicTacToeMain (you need to use them both toghether). The GameView inside TicTacToeLib shows you how to use Canvas in a game.
What you will see when you compile TicTacToeMain with TicTacToeLib:

Categories

Resources