Partially Updatable JFrame - java

I'm trying to make a building game (~Age Of Empire :). So, I want to write a program that divides a JFrame (containing the map of my game) to squares. Then allows every square of the frame to be modified (to contain an image). For example, I want to put an image in square (1,1), then add an image in square (4,2) and keep the image that I had in square (1,1).
How can this be done?

To do this inside the JFrame just use a GridLayout, create a control to match a square on the map (for now you could just use a JLabel or JButton) and add them to the screen.
I can't really recommend using Swing for developing a game though, you'd be better off looking into one of the many 2D (or 3D) for that matter game engines out there. They will allow you to get much more impressive results and do some of the work for you.

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.

How to make a GUI to display my game?

I have already coded a game which is played on a 2D grid. Now I just need to make a GUI to display it.
Each cell in my grid has an attribute that goes along with an image. For instance, if cell (0, 0) is water, I want to display an image of water in that pixel. I have already made images for each attribute (e.g. image of a character, water, land, etc.). I just don't know how to make the GUI.
It's a simple 2d map where each pixel (cell) is a specific texture/character/item. I also would like the character to move around, that is it.
Any resources or help would be appreciated. I've tried searching for a tutorial but they all seem so complicated and use Color to fill in their map instead of images.
Thanks.
Here's a 2D platformer that makes use of a simple thread, drawing, gamestates, etc. Although you might not need the full extent of everything it has, you might find interest in the main thread logic, etc. I believe he has a link to the source code in the comments. YouTube

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.

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

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.

Doing a non-square game board in Swing

I'm currently working on implementing a puzzle game, one of which is a game called sixpack. the pieces are triangular, and are assembled into hexagons, with groups of hexagons assembled into a diamond shape. it looks something like this:
my first thought was to put a bunch of jbuttons on top of that image you see, which i made in illustrator, using exact positioning. however i was wondering if there is a better, more scalable approach anyone else can think of that uses one of the swing layout managers.

Categories

Resources