Image slider in Java - Swing - java

I want to create something like this http://flowplayer.org/tools/demos/scrollable/index.html but in Java Swing. I don't need all those effects, just scrolling of thumbnail images. Is good approach to use toolbar and ImageIcons, and then on click load image on some JPanel.
Is there any common practice for this?

Use a JPanel with a GridLayout containing a JLabel for each image. Then you can add the panel to a scrollpane. You can turn off the scrollbars so they are not visible.
You can then use the technique described in Action Map Action to create buttons to do the scrolling.

Check out Kirill Grouchnikov's Trident animation library, in particular Project Onyx, which looks like it might do what you need.
(Edit: It's probably not an out-of-the-box solution, but the library itself looks nice and you could probably adapt some of the example application to suit your needs).

Related

Roguelike development - jlabel over jlabel

it's me again.
I'm still developping my roguelike, but I have another problem. I use a 2D array of Jlabel to display my map on a GridLayout, which is working perfectly fine. But now, I want to draw my character, monsters, etc. over the floor. Am I forced to switch my layout to a JLayeredPane, or is there any way I can achieve this whith my GridLayout ?
You should use multiple JPanels.
If you are already done with map (floor), shift that code onto a JPanel (with GridLayout)
Make another JPanel for displaying monsters and stuff. (With whichever LayoutManager you like).
Add these JPanels to your frame which has JLayeredPane.
So you'd have advantages of both.
Note: Don't forget to call setOpaque(false) on the JPanel on top.
Good luck.
Using JLabel is extremely slow, you should either use a monospace font and write directly to the graphics object of your JPanel or use a library.
SquidLib
libjcsi
Blacken
I recommend mine, which is SquidLib. It is the most up to date, has the most features, and is the only one still in continued development. It is also the only one with a lot of examples and support for literally any font Java can load.
Blacken is good if you're more use to the curses or libtcod way of interfacing with a console, but they don't allow arbitrary font use.
No link to libjcsi because I don't have enough rep to post more than 2 links. It's easy enough to google though.

How to resize controls at runtime in java

Is there a way to resize a control, a JTextfield for example, at runtime in java? I want my textfield to have the resize cursors (just like when you point your cursor on the corner of a window) and will be able to resize on runtime. Ive read on the internet that vb6 and C# have those capabilities, is there anything for java? A sample code or a link to a good tutorial will be very much appreciated. Thank you.
It sounds like you are trying to implement a component editor, such as the GUI editors available in popular programing IDEs. The essential feature is a handle, a graphical object that can be selected and dragged to change the geometry. GraphPanel is a simple example of an object drawing program that illustrates many of the required techniques.
That depends on the Layout of the JTextField's container. A good tutorial is available at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
For a quick and cheap solution you could use a JSplitPane component, with the JTextField to be resized in the left side, and an empty JPanel in the right side. By default a JSplitPane is decorated with a border and a divider, but you can remove these by setting an empty border.

How to find if image is clicked in JApplet?

It is to support image dragging. How to find if an image is clicked in JApplet?
I'm sure there is an easy way to do this, but I've looked everywhere and cannot seem to find it.
Options:
Use a JLabel to hold the image, and give it a MouseListener. Simple.
Or create a JButtton and use the Image as the button's ImageIcon. Probably simpler.
See the Drag and Drop and Data Transfer lesson of the tutorial.
If the purpose of the dragging is to change the order in a slideshow or similar, look to a JList. See setDragEnabled(true) & How to Use Lists for more details.
For the display component, I would recommend a JLabel as suggested by #Hover. JList uses a JLabel as the rendering component by default.

Resizing a JFrame and all the contents inside

I am making a game, and I am wondering if there is any way that you can resize the frame so that everything inside will also resize with it too?
That is the job of the LayoutManager. Choose your LayoutManager carfully so that it arranges your components properly. For example, if you use a BorderLayout, the center component will be stretched to take all the space not occupied by NORTH-SOUTH or WEST-EAST.
You need to choose the right LayoutManager for you. You can have layouts within layouts by putting JPanels within JPanels.
If you find the Java Swing library annoying to work with like that, then I suggest using Netbeans IDE where you can design visually how your GUI will look like.

Is there a LayoutManager in the Java API that would allow me to do like this?

I'm programming this level creator for a game me and a few of my friends are doing but as of right now the GUI is using a null layout, which I don't want to. It works fine for now, but I'm still against it and I know everyone else also encourages you to ALWAYS use a LayoutManager. I'm not really willing to compromise the design as it is right now, so I pretty much want to know if there's a LayoutManager that allows me to create a GUI that looks like this:
IT HAS TO BE IN THE STANDARD JAVA API! :)
This looks like a good job for a BorderLayout. Put the buttons inside a nested container as the NORTH element. Add the JScrollPane as the CENTER component. The grid itself looks like it is a good candidate for a GridBagLayout or perhaps a GridLayout.
Short answer, yes: GridBagLayout. But that'll be a pain to work out and debug.
Long answer: It looks to me like you could do this best with a BorderLayout, a JPanel for the JButtons, and a JTable with custom TableCellRenderers and TableCellEditors.
Check the excellent documentation available for Java by Sun itself:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Can you spot the GridLayout and GridBagLayout? If you put it into a scrollable container, that should do the trick.
Use GroupLayout for the overall panel and a custom paint method for the map.
I don't think many people here would recommend GroupLayout because it's more complicated than the other layout managers. I like it because it produces great scalable results, so I invested the time in understanding it. Now, I hardly use anything else - especially for user interaction panels with buttons and text fields.
For the map, though, I would create a custom MapPanel and overwrite paintComponent(). Sure you have to write your own custom scrolling algorithm, but I think that's a small benefit for not having to deal with scroll bars. You could make it so someone could just drag the mouse around and move the map. Use the mouse wheel to zoom, and make the interface very intuituve. If you want to paint scrollbars, you can do that too.
I've built several interfaces using models like this. I've built several maps for games using this model, as well as a financial market charting package. It makes it very easy to add custom functionality to do some great things that would be a nightmare to try to do in a JTable.

Categories