Roguelike development - jlabel over jlabel - java

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.

Related

Can SpringLayout do all the job

I would like to ask if SpringLayout can do anything like absolute position because I think absolute position have problem when I maximize the frame and what do I have to use if I need to set JMenu and JToolBar and JTextField and JTable all in one line in order?
I tried to use Borderlayout but it give me very big JTextField. I tried Gridlayout it give also big JTextField I need it big but not as big as it shows up.
can SpringLayout do all the job
No. It is neither designed, nor intended to do 'all the job'.
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.
For absolute positioning, don't use a layout manager at all.
However, that's not really recommended. Usually you should use a layout that would scale.
See also this thread for more discussion.
i found note writing by java says
Note: This lesson covers writing layout code by hand, which can be
challenging. If you are not interested in learning all the details of
layout management, you might prefer to use the GroupLayout layout
manager combined with a builder tool to lay out your GUI. One such
builder tool is the NetBeans IDE. Otherwise, if you want to code by
hand and do not want to use GroupLayout, then GridBagLayout is
recommended as the next most flexible and powerful layout manager

Resize JFrame and Components Based On Computer Resoultion

I've made a java application in netbeans and am wondering how to have the size of the jframe half the width and height of the computer resolution and also having the components comply with this change. I tried putting code and it did make the frame half the height of the computer resolution but my components, such as buttons and textfields, stopped showing. How can I achieve this? Thanks.
(EDITED)
Set the JFrame's layout manager to GridLayout. In the properties window of the GridLayout itself (select in the navigator window) set columns to 1 and rows to 2. This should give you what you want and you won't have to get into the code.
This is the key code being called within the initComponents() method of your JFrame subclass (created by NetBeans) but it is important to understand where it is:
getContentPane().setLayout(new java.awt.GridLayout(2, 1));
I love Netbeans but you do have to understand the basics.
Good luck with your project. Swing is an awesome toolset that was way ahead of it's time.
As usual in this situation the key is using the right combination of layout managers for your containers. You're probably using NetBeans generated code (something I recommend you avoid until you are very comfortable with Swing coding), and it's probably having you use GroupLayout, a fine layout, but one that might not behave as well as you'd like on resizing components. I suggest that you go through the layout manager tutorial and try to nest JPanel containers and play with different layouts that re-size well such as GridLayout, GridBagLayout and BorderLayout to try to create the best layout that can re-size well.

How to auto resize java swing elements?

I'm trying to auto resize the left side of my application. I have a JTextField and a JTree on the left and 3 JButtons on the right. But I just don't know how to make the left side auto resizeable.
I did it with the Netbeans GUI Creator (or whatever it is called) but I don't know how to to it without Netbeans. (I usually don't program with Netbeans, this was just an exception to see if it's even possible to do so with Swing.
Here is the code Netbeans created: http://pastebin.com/ERwY4rBC
It's not that the code is completely unusable but I wanted to try it manually.
The GroupLayout looks nice, but the Oracle site says it's mainly for the use for GUI tools. So, using GroupLayout would be not "Java like" or how do I have to understand it? Or is there even a better way to achieve this without GroupLayout?
Thanks!
So, using GroupLayout would be not "Java like" or how do I have to understand it
GroupLayout is to put it simply really hard to hand-code, and results mostly in a lot of code. But it is not "not Java like", it is just not something you want to do by hand, and the code afterwards is hard to read as it is rather verbose.
What you try to achieve (according to the screenshot) is easily achievable using some 'nested layouts'. If your main panel uses a BorderLayout where you put the left, resizable panel in the BorderLayout.CENTER and the other, non-resizable panel in the BorderLayout.EAST you will obtain the desired resize behavior.
Then you just have to decide which LayoutManager to use for those individual panels. I think that both the BoxLayout as well as the FlowLayout will do just fine.
Do yourself a favour and use MigLayout for all your layout needs. It is especially convenient for coding UI by hand.
There is a WebStart application on their site that demos different layout situations with code samples provided.

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.

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