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.
Related
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.
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.
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.
I've been looking around for a way to do this. I found some code to change the order of strings in a list, but i want to add buttons and such on each panel so that wouldn't work...
You may want to take a look at the docking frameworks. For example, Docking Frames has a good example of a chess board - basically, you can "dock" pieces on the chess board squares which server as the drop targets. Just launch the demonstration via the provided jnlp and then pick the Chess demonstration in the list on the left to see how it works. Here's a screenshoot of it:
You can see that each of the squares is a docking tile (i.e. a draggable object).
In your case, instead of 8x8 of squares, you can have Nx1 to simulate a one-column list instead of a table.
Use a JList. Look into the dragging behavior for the reordering ability.
See How to Use Lists for more details.
Well I am going to bed right now, and in next 2 days I have to code a simple program with animation that will simulate an inverted broom on a cart (a pole balancing genetic algorithm problem) the cart has to be pushed constantly from both sides to prevent the broom from falling down
You can see it in this video
http://www.youtube.com/watch?v=Ums3eGIVgks
or this picture image http://lis.epfl.ch/research/projects/EvolutionOfAnalogNetworks/ArtificialNeuralNetworks/images/mechanik_small.png
Well, I need to simulate physical behavior of this, but I have very little time, so I need something that I can understand and start using fast (the assignment is more into physics and genetic programming, so the simulation has to be just to show how it works).
Thank you
I'd use HTML5 Canvas and pure javascript. It's super easy, you don't need to compile. All you need is notepad, Chrome/Firefox/IE9, and a little time. There are tons of examples out there:
http://3.paulhamill.com/node/36
If you want an application, create a java application which uses a JPanel in a JFrame. In the JPanel override the paintComponent(Graphics g) method, and look at the graphics class which allows you to draw simple shapes like lines, rectangles, and ellipses.