I've made a concentration-pairs game using netbeans.More details for the game here
http://en.wikipedia.org/wiki/Concentration_(game) .
before the game starts the user has the ability to choose the game version (computer vs human,human vs human),their names and the memory of cards that computer has to remember in a computer vs human version,and also to choose dimensions and jokers.He can choose a specified size and jokers or his own preferred sizes (columns,rows,jokers).
Of course i have a limit for row and columns : 52.But playing with setResizable doesn't help at all when the user uses eg 52x52 or 50x3 as dimensions for the game... it looks horrible (Btw the images are 73x97 pixels).
My components are not buttons they are objects of my own gui class concerning cards (and has a connection with the logic of the card from another package).So i should try gridbaglayout instead of gridlayout? Gridbadlayout can be a real headache cause i've already used it for my radiobuttons.
right now i am using gridlayout but when i had setEnabled(true) so the eg 10 rows would actually fit on screen,the graphic card would stay small and not "fill" the whole component...
http://postimg.org/gallery/53g9ag0i/667f8271/ What i mean here
Should i resize all my components or the limit is too big and i should make it smaller ?
GridLayout is usually fine when you want components that have all the same dimensions. I recommend simply putting your grid of cards inside a JScrollPane.
Alternatively, if having the game area scrollable when the grid dimensions are large is unacceptable, you can take a look at the answer to this question: How to lock aspect ratio of a gridLayout in Java?.
Yet another possibility is extending GridLayout so that it tries to keep fixed aspect ratio for the cards. (For this you obviously need cards that are capable of drawing themselves properly at different sizes).
Why dont you compute the best size? Calculate what the maximum value for cards in a row is. Then estimate the minimal icon size of a card so that anybody can read it. Then you just have to calculate: ScreenDimensions / mininmalCardSize. And then you can adjust the desired size from the user. This is how i would do it. But i would like to use the Null Layout.
Related
I just finished my first coding class (IB Computer Science, if that helps), and I decided I wanted to make my first game.
The game world I have currently is a top-down view of the terrain. Each part of the terrain is made up of squares (it is very similar to the way Dwarf Fortress looks). I have been able to get it to output to console, using characters for stand-in graphics, but my course covered very little on graphics work.
What is the best way to create a grid of sprites or colored squares inside a JPanel? I have been able to display BufferedImages before, but have not been able to align multiple BufferedImages to get a grid.
As of now, I have an '2D' ArrayList, just an ArrayList of an ArrayList, making up my game world. It all works great when I use a double for loop and System.out.print("");
Check the oracle documentaion for Layout Managers. The one you are looking for is Grid Layout.
If you feel fancy, you can upgrade to JavaFX, and use a Grid Pane instead.
I have gone through lots of tutorials which teach about layouts in Java Swing, but they don't seem to suffice my need. I am creating a solar system GUI using Java Swing, and i want to place the planets in the GUI according to the values i fetch from my micro controller, which are usually float point values. I cannot use the Grid Bag Layout, as to position a label i have to specify grid x and grid y, which cannot be the case since I receive float point values from the micro controller. The best resource i found is to use absolute layout where i can specify the position of the planet by giving mere X and Y Co-ordinates, which will be fetched from the micro controller. The problem I am facing now is that the absolute layout does not have auto re-size feature.
What would be the best possible option to adopt the auto re-size feature in absolute layout?
Swing tutorials were not generally meant for situations like this -- they were meant for people who want to write more normal GUI applications, using buttons, drop-down boxes, check boxes, radio buttons, menus, and have layout that follows currently accepted practices in terms of positioning those on the screens. If any of that applies to the part of your program that is not displaying planets, I encourage you to use what they have to say about it.
But you want to place things according to calculations of your own. I recommend doing that in a panel, calculating the size and position of your objects according to the size of the panel at the point of drawing. When the panel resizes, you will need to trap the event that says it is resizing and redraw. You will need to deal with your own minimums and maximums, etc.
I don't recommend the custom layout manager suggested elsewhere for a couple of reasons. Firstly, it won't save you any work at all -- you are still going to have to write the code that determines positions of things, if you just then draw your own graphic instead of attempting to position a UI element, I think it will actually be less work. And that's the second reason -- layout managers' purpose is to position UI elements within the panel, and the pieces of your solar system don't really have any need to be UI elements, just graphics on the screen.
Good luck.
I'm currently doing an assignment that involves me creating an elevator simulation. This is currently where I'm at:
The way I've got it working is that when the combo box on the right is changed, the floor lines automatically redraw themselves according to the panel sizes. The tricky part now is I just want to print the floor numbers in the left 'floors' panel, just on top of the floor line. However, I have no idea how to go about it. I toyed with the idea of setting a null layout and positioning the jlabels according to the floor heights (which I have saved in an array). But everywhere I've looked, people have said this is a bad idea.
So could someone suggest a way to accomplish this with a layout manager? Thanks.
Start by breaking down you UI into manageable chunks, this will allow you to focus on the individual requirements of each section without getting overwhelmed...
...To start with...
This would suggest the use of a BorderLayout, placing the red section in the BorderLayout.NORTH position and the green in the the BorderLayout.CENTER position.
...Next...
The header is broken into (at least) two sections, this would suggest a GridLayout...
...Next...
The body suggest three equal columns, this could be achieved with a GridLayout or even a GridBagLayout, if you need the widths to be different for each column...
... Next...
This is probably the most complex. To start with the "blue" section would probably benefit from either a GridLayout, if the Number of floors label and JComboBox resided on their own JPanel (may be using a GridBagLayout) or aGridBagLayout` if you wanted more control
The red section is probably a using a GridLayout
All three sections would probably be glued together with a GridBagLayout...
For example...
I have just completed an application for my final year project and I need to create the interface for it now. The application will not include many different screens, just one introduction screen with a simple tutorial and the main screen with 5 JPanel and a JMenu. I have each part of the application providing its own JPanel, and the GUI I am about to make should put all those panels together and provide the intro.
What I want to ask is how I can properly set the sizes of different
components so that they are displayed the same on different screen sizes.(not getting really close to each other on small screens / big blank spaces on larger screens)
Should I manually set their preferred sized based on some percentage of the screen dimensions ?(e.g. 20% * width,40% * height) Or there is some other way to do it ?
Also, having one week ahead to complete this part, would it be any benefit to try and learn some library like MigLayout? I read a lot that is easier to use than standard Swing.
p.s
The JPanels include trees,textAreas,toolBar, buttons,checkboxes,comboboxes and textfields. Each one of those panels are quite simple to make.
The answer to this type of question is pretty application dependent, simply because what is 'reasonable' depends on the application and user expectations, but...
If you want the content of the frame to dictate the size of each frame, you can just call frame.pack() and an appropriate size will be guessed at based on the size requirements of the frame's children.
However, if it makes more sense to let the screen size dictate the frame dimensions, you can use Toolkit.getScreenSize() to get the screen size and do your positioning based on what you find.
I have an Swing app whose main panel is divided into a 3x2 grid of charts, and the app can be resized, with the charts (JFreeChart) auto scaling. One of these panels I would like to display the Apdex rating in, which is just text (e.g. '0.89 [0.5]*'). We use the application to display on a monitor visible to everyone, and scale multiple instances of the app that monitor different data centers. Scaling the Apdex font size to fit available panel space is what I'm after.
Any good ideas?
After re-reading and rethinking the question I would suggest for you to try calculating it yourself by use of FontMatrics stringWidth with the string and iteratively increasing the font size until you can, i.e. the size evaluated by you versus the available space.
A ready algorithm would be nice but I didn't hear of any.
Good luck, Boro.
I'd render it off-screen at some suitably large point size, as shown here, and then down-sample it using AffineTransformOp, as shown here.