I want to make board(map) like this in Java.
Each small hexagon is image.
Suppose I have two Java classes. Canvas(big hexagon) and Hexagon. First is entire board from which I generate randomly all small hexagons. Both classes derived from JPanel. Now I have GridLayout. How can I arrange layout like this?
Why do you need the small hexagon panels?
I would rather just define List (list of Hexagons) each with desired position and just override paintComponent() method of main JPanel. You can use this http://java-sl.com/shapes.html to create hexagon shapes.
To track mouse click you can use contains() method of Shape.
you can
1) common way
by painting to the JPanel/JComponent by override paintComponent() (I asumed that there are Image/BufferedImage/Icon/ImageIcon)
2) by place Icon/ImageIcon to the JLabel
you have look at JLayer (since Java7) or use (former) JXLayer
OverlayLayout or customizations for OverlayLayout by #camickr
The point of layout manages is to make it possible for the layout to auto-adjust when components change their size or the window does.
It looks like your hexagons will always be the same size, so you really don't need a layout manager, and positioning the hexagons absolutely should be fine.
Related
How do I convert a paintComponent to something that I can manipulate with a layout in a JFrame?
So, I'm running into an issue. I haven't really been taught (and don't have access to a book) how to use layouts/GUI stuff in my courses yet.
My issue is this: I have a program that the user inputs a number. Based on this number, the program calculates a circle and draws it out with a paintComponent method that has a for loop inside of it. The "pixels" that the circle is drawn with are actually fillRect methods. The current method of getting a user-input that I am using is a JOptionPane showInputDialog. This is MOSTLY fine, but I want the user to be able to select from a set of pre-defined numbers. Somebody suggested that I use a JComboBox, but I don't know how I would convert the paintComponent to something that would be usable by a layout manager (which a JComboBox must use, as far as I've learned). I know the dimensions of the paintComponent (805px by 805px) and there is no situation where it will change. If I could get some help with this bit, I am confident that I can figure out using a layout manager myself.
Another way to paint (besides custom painting) is to paint to a BufferedImage. The image can then be displayed in a JLabel.
Examples:
Painting in a BufferedImage inside Swing A fairly complicated one.
Dynamic Graphics Object Painting Another one.
Yet another one.
You don't know the dimensions of paintComponent because it's a method, and methods don't have dimensions. You probably know the dimensions of a JPanel or a JFrame or whatever your component is.
You should separate the panel where you do the painting, and a different panel which would contain any comboboxes or other inputs you decide to put in. That way you can keep your drawing panel as is, and they won't interfere with each other. You'll want to search for the tutorial on LayoutManagers.
I am currently creating a GUI for a game. I am currently loading an image using JLabel onto my JPanel, I was wondering whether it would be possible to add buttons on various parts of the image e.g. (5,7) and (12,12).
If you wish to have JButtons appear over your image, a better approach would be to override the paintComponent of the JPanel and use drawImage to draw the image similar to this example . This will allow you to add components to the container.
Absolute positioning(null layout) is generally frowned upon for setting component locations, however. This DragLayout should be a better substitute taking care of component sizing.
DragLayout was designed to replace a null layout. It will respect the location of a component. By default it will use the preferred size of the component to determines its size. Finally, it will automatically calculate the preferred size of the Container.
Give that I have written a JPanel with many different components on it, is there a way to apply an overall "dilate" ability on the panel so that everything in it stretches proportionally when I resize my window?
That is, if I manually resize my window to be 1/4 the size, everything in the panel should also shrink by 1/4 so the new panel is just a dilation of the first. Given that I have not designed the individual components inside to do this (there are many) is there any easy way to make the panel behave this way?
UPDATE: In order to be more clear on the solution I need, I will describe the panel contents:
The panel is a "game" of sorts, with a single null-layout and dozens of ImageIcons flying around the screen at any time. The ImageIcons are preloaded PNG files, which already have a permanent size. Of course, I could manually resize each ImageIcon and reposition them relative to window size, but that would involve recoding many components.
There are no buttons or text to worry about, so what I'm really looking for is some kind of "postprocessed" resize where the panel simply shrinks whatever's rendered by some porportion (think of resizing an image in Photoshop).
One option is of course to give up swing all together and use some 3rd party widget component library which draws itself using any Graphics. Then you can either draw the widgets on the image and resize the image, or, better yet, apply a transform to the graphics object you pass to the library.
If you do want to stick with swing there is the SwingUtilities.paintComponent method, which you could use to paint the Panel onto a BufferedImage which you could then resize. (I've used this myself to do some nice transitions between "views" in a game.)
The problem is of course that you somehow need to translate the user input accordingly. I have no solution for this right now, but the above perhaps helps you in some way.
You can try to override paintChildren() method of the panel and scale graphics to achieve desired visible size.
You could try J(X)Layer, see http://www.pbjar.org/blogs/jxlayer/jxlayer40/
Using layout managers instead of absolute positioning of the widgets will give you this behaviour. See the oracle tutorials: Using Layout Managers.
Do you really want fonts to resize on resize events? I don't know a layout manager which will do that for you.
Hi
I have a panel on my frame which you can draw something on it.and also i have a "clean" button which clean my panel completely.but I don't know how can I do this work ?
I use netbeans .
thanks
Well, this really depends on the how you have got your panel to draw. Usually, someone would override the paint method of the panel and draw the screen based on Points or other geometric elements.
To clean the panel, you would need to clear the points, and then force a redraw, using the panel's repaint() method.
This however really depends on how you have implemented the first part of your solution. We really need more information to give a more precise answer.
Use the clearRect method to draw the background color over the whole area
Custom Painting Approaches shows two common approaches to do custom painting. It also shows the common approaches to clear the painting.
Is there any way I can print/show images on top of each other. The picture on top will always be positioned a little lower so that the one under will show partially. How can I decide which image is on top of what image? What layout lets me do this kind of positioning?
Is there any way that I can make a border appear on the image when I click it, and then move to (doesnt have to be animated, can be a "jump") where I click next inside the JFrame.
I've been trying to do this whole day now (I'm pretty new to swing), before I carry on I'd like to know if I'm trying something impossible.
So far I've been printing the images right on to the JFrame as JPanels... Inside the JPanel I add in the paintComponent(Graphics g) method: g.drawImage
Sounds like a Swing tutorial is in order.
What you're describing shouldn't be very hard. Instead of painting the images directly, load them up in ImageIcons, and pass those to JLabels. That way you can manipulate your images as JComponents, using layout managers, or direct coordinates by setting the layout to null. You can set the Z-Order with setComponentZOrder regardless of the layout you choose. You can draw borders by adding swing borders (see BorderFactory) to the JLabels. You can handle the manipulation with MouseListeners.
Look into Root Panes. You may be able to do something with the Layered Pane or the Glass Pane. I would try the Layered Pane first.