Best way for multilayered 2D Graphics on JFrame with partial repaint? - java

Basicly, I want to make a game of chess.
The idea is that I have a picture of a chessboard and the individual chess pieces. What I could get to work is a JPanel where I would repaint everything everytime with the new positions of the chess pieces, but this would require to get the positions of all chess pieces and repaint the board with up to 33 pictures, with double buffering and all.
A bit resource consuming I think. AFAIK, there is the option to only repaint a certain area, but I guess there're still better ways. What I could imagine is, just moving or removing one or two pictures or rather chess pieces each time rather than repainting something.
I sadly have only very limited knowledge of the classes out there and so I ask if there is such a way or even an entirely different one, that does the job more efficient than painting/repainting.

Instead of inventing the wheel again, use a game engine with sprite support like JGame to do the rendering.
Also note that today, the resource consumption to render chess is so small that spending a minute to optimize it is a minute wasted. What you should aim for is a framework which takes the least time to implement the rendering of the game so you don't have to spend too much time on this part of the game.
If you feel a game engine to be overkill, how about using a table with a custom cell renderer which draws each cell? The table will make sure that updates are rendered in an optimal way. You might even be able to use a custom TableModel to define the playing field.

Related

Correctly Sizing a Canvas inside a JFrame

my current project is a small desktop application in Java that functions like a room decorator similar to the IKEA software that lets you plan out your room before you go and buy furniture. When I thought about that concept my mind immediately jumped to the RTS Genre of games. Thinking about WC3 and StarCraft experiences I had my thought was to implement a basic grid interface where i can just place down squares as a starter demonstration type of deal.
I understand scaling a Canvas (java.awt.Canvas) inside a basic JFrame is .. weird since the Frame itself consumes space for its outer border display. So it converted the 1600/900 Frame width/height to 1584/861 Canvas width/height.
What I want is to be able to divide the display into a grid of 5x5 squares but, as you might immediately recognize, neither 1584 nor 861 is cleanly divisible by 5.
I fumbled a bit with Modulo ( % ) and quickly ran out of ideas so I turned to the internet for help yet google and stackoverflow was suprisingly void of information about the ratio at which jframes steal away size from its inner components.
My question now would be, is there any efficient method to tell the frame to wrap itself around a canvas of a certain size rather than taking the space it needs from it? or any other mechanic i can use to arrange a Canvas to a certain size disregarding the space the frame needs? Because just trial and error on fumbling together the right frame size for this to work seems to me like an ugly solution that only works for the specific case of 5x5 squares.

Split game loop and render in JavaFX simulation

I'm running a game/simulation on JavaFX and when I started it seemed reasonable to add an AnimationTimer to perform the tick updates. My Entity's are composed of a Polygon that holds their shape and position. I added the Polygon's to the scene via a Group and everything renders like magic. However, since it's an simulation, I now want to run millions of ticks with no rendering, to advance to the future and see the results. The problem is, since my Entity's (x,y) positions are inside the Polygon, every time handle() is called in the animation, it seems that the screen is updated.
What is the proper way to split the game loop and render, to be able to call render only after some amount of ticks?
I thought of creating my own MyPolygon class to hold the simulation data, and then when the time comes to draw, create on the fly one Polygon per Entity, but that seems also overkill for me (but maybe I'm wrong).
Also, I'm not sure how to change the ticks per second rate on the AnimationTimer. So I'm not sure it is suited for this specific need.
It seems like a very simple design choice, so there has to be a proper way to do it with JavaFX...

Grid building for Connect Four gui

I have been able to create a grid using an image file (serves as the empty circles), a loop, and GridLayout, but I am well aware that there's more functionality needed (like for dropping the token, though no animation is necessary yet) so I scrapped it and now I'm back to an empty grid. I am stuck and I'm not really sure how I can accomplish this. My code is a mess at the moment so I'm not sure if it'd even make sense for me to post it.
My main problem is how to build a grid, which will then just be filled with a solid color (I'm cancelling using an image file, it seems a little more complicated as far as I'm concerned) with empty circles, that I will be able to fill up with an image file of a token once the player clicks on a button that corresponds to the column he chose (and then reset everything after the game is over). In other words, a rectangle of solid color and with empty circles to be filled up by tokens, but not with solid color, but an image file.
I have been trying to familiarize myself with paint() but I only started learning GUI last week so there are still likely some more things I'll have to learn to probably understand it in a considerable degree.
I am running out of options tantamount to my knowledge of GUI (Swing in particular) and I have been trying to work on this for a week now.
Any hints?
There are multiple possible ways to solve this, but one easy one is to give a JPanel a GridLayout, and then fill it with JLabels with ImageIcons that show empty circles. When the column is selected, the appropriate JLabel is given a new ImageIcon via setIcon that shows a color filled circle.
Also,
Always strive to separate your program logic code from your GUI code, since the better your separation, the easier will be your ability to debug and enhance.
Work on small problems one at a time. Don't move on to the next problem until the current small step is solved.
Work out your logic and ideas on paper first before committing it to code.
Don't "work with paint". If you need to do Swing graphics, you'll want to override a JPanel or JComponent's paintComponent method. The paint method also concerns itself with drawing borders and children, and so overriding it can have nasty and unexpected side effects on these. Also paint is not double buffered by default, and this can lead to bad animation once you start working with animation.
Edit
You state in comment:
Will it be okay to use JButton though? Because that was what i used during my first attempt. I can use setIcon with it too right?
Do you mean use a JButton instead of a JLabel? That would work, and yes you can call setIcon on JButtons, but would make all your rectangles look like buttons. So if that's OK, then do it. Otherwise, you could still use JLabels, and then create a row grid of JButtons to put below or above your game grid, and then have the user press those buttons, and in their ActionListeners have them change the icons of a JLabel in the selected column.
But having said this, I mainly recommend that you use what works best for you. The learning will be in the creating, no matter what you create.
Edit 2
You ask:
do you think it'll be possible/a nice approach to store jlabels in an array and then lay them out in a panel?
Absolutely, either an array of JLabel[] or a List<JLabel> I think is not only possible but in fact essential for this to work well. I think that you're definitely on the right track here.

Representing a vehicle in SWING

As it stands, I have a map with roads on it and vehicles are able to drive back and forth from south to north and east to west (and opposite ways as well). I have used JPanels thus far to represent the vehicles. However now it's becoming a bit difficult to handle because I want to turn the vehicles at junctions and smooth lane changing etc.
So it doesn't seem like JPanel is the optimal choice for this. What I've tried so far is to use the Shape interface to draw polygons and use these as vehicles, however I'm not sure that this is the right choice.
I will eventually want to construct my own vehicle image so the solution will have to either be able to add the image as it's background or something similar and still be able to perform operations such as rotation, transformation etc.
Any guidance on this will be much appreciated.
Personally, using JPanel isn't a bad choice, what you need to be able to do is extend it's capabilities to allow you to paint changes in orientation of the vehicle as it turns.
This is going to require some animation - you going to want to know how long it would take a vehicle to complete a turn so you can calculate the angel of the vehicle over time.
For this, I would be using AffineTransformation, check here for some examples.
You will also need to change the size of the component as the vehicle turns, check here for the answer.
Lane changes would be a similar (if easier) concept. The basic idea would be to have a start position and end position of the translation and move to that position over time. Again, you would need to know the amount of time it would take to complete the translation.

What is the best way to accommodate for different screen sizes in a game?

I am creating a Java game for Windows and I have come across a problem: there are lots of different screens and resolutions when it comes to Windows. What would be the best way to make it so that it looks just about the same on all screens?
You basically have three options:
Fix the size of the game window to something small that will fit n all screens (800*600 maybe)? This is easy to do, but could annoy users with big screens.....
Make the game resolution-independent, so that that it is rendered to a scale to fit the current window size. This is how most FPS games work for example. The main downside of this is that you need to do some extra scaling maths in your code and there may be some runtime overhead for rescaling images etc.
Make the game screen dynamically resizable, so that the components within it rearrange and resize themselves to fit the available space (like with a web page). This is the hardest to implement as you have to make use of appropriate layout managers and test lots of different combinations, but can give the nicest user "experience". I've successfully used MigLayout to do this in the past with a Swing game.
Any of these options could be best for you depending on the circumstances. It will probably depend mainly on the type/design of your game and your willingness to spend time on making the more complicated methods work well.

Categories

Resources