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.
Related
I've been using a JPanel and overriding paintComponent() in order to perform my drawing in Swing games. I'm now trying to add an inventory, which will contain different items the player can drag around and move to different slots in his "backpack" on the screen. Should Swing games only draw on a single JPanel or other component (example: draw images of item at mouse location) or can you add JButtons whose icons are pictures of the items?
Should the game only have the one drawing component, or can you include more?
Principally, you can have any number of components that you want to have. Swing – as any other sufficently elaborate library such as SWT for Java or Qt and WxWidgets for C++ – is intelligent enough to draw to the screen only what actually really is necessary.
As long as you do not run into performance issues, there is no problem with that. If this actually happens, you might first want to look at your own paintComponent implementation as this is the most probable location where you lose efficiency.
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.
I want to make something akin to a diagram editor - an application that allows the user to create, view and edit a bunch of shapes on a canvas. My GUI has essentially three parts - a standard JMenuBar, one JPanel on the right side for showing info about the shapes and a JPanel next to it that should be used for visualizing the shapes.
Aside from that, I have a package that defines the shapes. For simplicity's sake say there is just a Square, containing the following information: coordinates on the canvas, size, user-defined name and description, color.
The main class of the project (an extension of JFrame) contains lists of Squares.
Now, I could visualize the shapes by simply drawing them in the JPanel using drawrect and whatnot, but I want to create an interactive editor - if the user right-clicks on a visualized shape, a context menu would pop-up allowing him to move it, change its properties or remove the shape altogether. Clicking an empty spot in the JPanel would allow the user (again, via a menu) to create a new shape.
Is there an automated way to do this as opposed to manually keeping a matrix mapping each pixel of the canvas to a certain shape and checking it upon right click? One where I could say, for example, draw this here within the JPanel and do something onclick...
A related question, when I edit the shape in the JPanel, how do I access the Square/Circle list in the main application class, so that I can really change it? I suppose this is simple, but right now I don't know how I would do it.
Please point me in the right direction, anything will be appreciated, tips, examples, links to relevant tutorials...
As suggested by #eugener, classes that implement the Shape interface have a contains() method that is useful for this. GraphPanel is an example that illustrates several of the features you mention.
The most common way to accomplish this is to allow shapes to determine if point is inside the shape. Hopefully your code is object oriented and each type of your shape is a class.
All you have to do is to define a method such as boolean isInside( point: Point) for each shape. Once you have those all you have to do is walk the shapes in the reverse z-order (from top to bottom) and and see where the mouse click point lends. If it does not lend on any shape - you clicked the canvas. Once you have this info you can show an appropriate menu.
Hope this helps
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'm working on a domino game and it's going pretty well, now I want to drag a domino tile from one JPanel to another, my dragging implementation works, it's just that I can't find how to drag shapes between two jpanels.
Here's how it looks:
It is called Drop Location Rendering.