I am hoping someone can offer a strategy for the following Java GUI:
I am implementing a Scrabble-like algorithm and I would like to write a GUI where a user can compete with the algorithm. My experience with GUIs is limited and I am trying to avoid a big learning curve if someone can suggest a useful subset of components upon which I can focus.
I would like to display two playing areas side by side. Each playing area contains a board and a tile holder. Tiles from the holder can be dragged and dropped onto the board.
Obviously, the basic component is the tile, which is just a square displaying a letter. The tile holder and the board are both tile containers (grids of different sizes). The playing areas each hold the two tile containers and allow dragging and dropping from one container to the other.
The layout of the two playing areas is: the board on top, the tile holder on the bottom, and some neutral space between them.
The layout for the window is just to display the two playing areas side by side.
Perhaps naively, it seems to me I don't need much. But finding the right pieces is proving to be quite a task.
Any advice is appreciated.
Top level JPanel with a GridLayout, 1 row, 2 columns, will create for you a panel with two evenly split areas. After that you can add another JPanel for each side (just sequentially add them), and these two JPanels can have a BorderLayout. For these two BorderLayout JPanels, you can add to each the following:
A JPanel with a FlowLayout of ImageIcons that hold a pictures of a scrabble tile, and this JPanel will be added to the south position on the BorderLayout JPanel.
Another JPanel with a custom class you'll make called ScrabbleBoard extends JComponent. Here you can extend paintComponent and draw the board how you wish, probably making use of obtaining the width and height so you can draw a board that scales to how much space you have available for the component. This custom JComponent will be added to the center position of the BorderLayout JPanel.
A component added to the center position of a BorderLayout tries to be greedy and takes up as much room as possible, while components on the sides do not, so it's often a good choice for UI's that require a small navigation or menu control area, and a larger area to view what is considered the main graphics.
Related
This is a mock up for what I have in mind as a layout for my project:
The way I tried to accomplish this is:
I set the entire frame to a border layout and then cut it horizontally with two panels, we'll call them north and south panels. The south panel is Panel 3 from the first picture.
I set the north panel to a border layout as well and cut it vertically with two panels. These become panel 1 and panel 2 in the first picture. The problem occurs when I try to resize the window. I would like the panels to scale proportionally to eachother so the size ratio's between the panels stay the same. The problem is, instead of resizing, the panels just move away from each other like so:
Any ideas for creating the desired design? Am I on the right track or is there another swing layout that is better suited to my needs?
I would like the panels to scale proportionally to eachother so the size ratio's between the panels stay the same.
Try using a horizontal BoxLayout. I believe it will allocate extra space proportionally up to the components maximum size if extra space is available.
Or if that doesn't work you can use a GridBagLayout. You can use the weightx constraint for each component you add to the panel. This will control how much extra space is given to each component.
Read the section from the Swing tutorial on Layout Managers for working example of each.
I'm working on a card game and I'm stuck on how to proceed with giving a simple visual feedback when the players are interacting with the cards that are placed on board.
As you can see in the image I'd like to implement drawing arrows between the cards that interact with different object on the board (for example when the player attacks with a starship a arrow will appear between the ship and its target). When the player commits his move, the arrow needs to be drawn on the opponents gui aswell when the opponent starts his defending phase.
The whole client gui aswell as the cards are coded with swing library and are JPanels with various layouts. The cards is one class and the board gui is composed of several gui classes (handGui, heroGui, unitFieldGui etc) added together in a mainWindow gui class.
I don't really know how to proceed to achieve this kind of visual feedback and would appreciate if you could point me in the right direction, what kind of technique would help me solve this problem?
Check out the section from the Swing tutorial on Glass Panes. a Glass Pane is a component that is painted over top of the frame and you can do custom painting.
You can use the SwingUtilities.convertPoint(...) method to convert the two card components location relative to the Glass Pane. Then you do your custom painting to draw the arrow from one component to the other.
I am creating a snake game and I am in need of using a JLayeredPane which will hold 2 Jpanels, this JLayeredPane will then be inserted into a JFrame. The reason I am doing this is because I need to have the base layer -- the game, and on top a layer which will spawn fruit onto the board. The reason I am not doing this directly onto the board is because the board is constantly being repainted, I need a method to randomly change the colour of the fruit for every time the player collects the fruit. The fruit however, will continuously change colour as the board is being repainted and a new random colour is generated based upon an array.
The problem with simply using a JLayeredPane is that I need the frame to fit perfectly with the components inside of it, setPreferredSize does not seem to do this as it does not take insets into account. When setResize(false), the insets do not match with the actual inset values, so I cannot simply add the insets.
So my question, how do I get the JLayeredPane to fit the JPanel's where the components within these JPanels have dimensions and then put this JLayeredPane into the JFrame.
As shown in How to Use Layered Panes: Setting a Component's Position Within Its Depth, use setBounds() to position components within a JLayeredPane.
Alternatively, override paintComponent() in a JCOmponent or JPanel and render the fruit layer beneath the snake layer. If no resampling is required, drawImage() is quite fast in this context.
I'd like some advice regarding structure of a game I'm working on. Specifically where to place painting methods.
Currently there is a applet wrapper class for a Jpanel which runs the game loop.
The game itself is meant to simulate a very large area. objects will have x&y values which themselves will part of a larger x&y grid.
i.e. object1 position is 150000x30000 in grid block 1,5.
objects will need to be able to move into neighbouring grids, however I'd rather not run each grid block until needed as 99% of them will be empty.
Currently the UI is a Jpanel with a few buttons + listeners, a large drawing pane is needed to display the objects.
my question is:
what class should this internal drawing pane be based on? I'd like to have control to zoom and pan around the grid. it only needs to draw what is visible, but object movements will continue in the game loop.
what painting strategy would be applicable for simple (icons really when zoomed out) moving around vast areas, I'm guessing relying on the EDT to repaint isn't going to be good enough?
I'm not really after specific code, I want to learn myself how to do this, I just need pointing in the right direction, as most things I read don't seam to quite cover what I'm after, or don't make use of JRE6+ features.
Many Thanks
Rather than paint each grid cell in your drawing pane, why don't you have each object repaint itself onto the grid?
I have two JPanels (let's call these Panel1 and Panel2). These panels are of the same width, but varying heights.
I want to put these JPanels into one big JPanel (lets call it Panel0), and stack them on top of each other (I decided to set Panel0's layout as GridLayout(0,1)).
The problem, is that both nested panels (panels 1 and 2) end up having the same dimensions (those of the biggest between the two), instead of the setPreferredDimension and setDimension that I set to them.
Sorry, I can't really provide any code (there's a lot of crap added to the panel's, and it's all for something work-related). Any advice? Thanks!
GridLayout forces all components to be the same size; that's why it's called a grid.
Since you only have two panels, I'd suggest using a BorderLayout with one panel at NORTH and the other CENTER. If you allow resizing, then the one in CENTER will be the expand to fill any extra vertical space, so just be aware of that.