Text input in Java Game w/o Swing - java

I'm working on a 2D RPG game and am currently working on a dialogue window that pops up, to prompt the user to enter the quantity of a given item they would like to buy/sell in a shop. For this I need the user to be able to provide a number as input.
I've read some docs on JTextFields, but I'm mostly using my own graphics in this game, drawing to the screen with a Graphics object and don't want to use the standard Java JTextField Swing graphics, as it would be out of place. I have my own, fully working, KeyManager handler class that handles keyboard input in my game. Do you know of any approaches that would allow me to make custom text fields, which are focusable (to type in)?
Another question I have is: Is it possible to remove the JTextField border and set the background to full transparent, so only the cursor for typing is visible and I can add my own implementations of a "textbox"?
Then finally: My game opens with a JFrame and the graphics are drawn to a Canvas. A JTextField should be added to a JFrame, to a JPanel, right? If this is the case, should my DialogueWindow class extend a new JFrame and contain a JPanel to add the JTextField to or do I have to add this to my existing JFrame?
I have little experience with using the Swing library, so hoping someone can answer these questions for me.
tl;dr:
I don't want my input field to look like the standard JTextField below, how to implement my own without using the standard Swing graphics?
"How I don't want it"

Try using the Graphics class. But that would be really complicated. You will implement a KeyListener to the entire container. Whenever you pressed a key, some text should be drawn to the screen. Whoops!!! the coordinates (x,y) of the text doesn't move automatically like JTextField does, you need to increment the x whenever you type. and reset it to 0 if the user hits ENTER and increment y. The value that will be added to x and y depends on Font Size.

Related

How to add a "Add a text" in a JFrame

I'm having difficulty in getting resources that will help me with an issue. I want to add a button that will let me insert text anywhere in my JFrame so I can click and add a text
Before that, I have been using JTextField and JTextArea, but these are useful in a predefined space (I believe).
What I want is like the classic Add Text button in Paint ("A" button) in which when you click on it, you will have the text cursor (like an "I") and then click somewhere in the frame so I can actually add text in that area. Which I call "adding text dynamically". Unfortunately I don't have any code to show because I'm not sure how to build the code from scratch or maybe use a method in Swing, but I'm more than keen to explain better if this is too generic for you guys.
You're going to need to be familiar with Java graphics and associated methods, particularly drawString(), so start by reading the official Graphics Tutorial.
Then learn about MouseListeners, which will help you recognize when and where the user clicks in your JFrame.
Then learn about KeyListeners, which will allow you to detect when your user is typing. You may wish to use a key bind instead, but as described, your UI is relatively simple with only one focusable component, so keylisteners are probably OK.
This general strategy should help you take a flying leap at the problem:
detect mouseclick with mouselistener
draw an insertion bar
detect keypress with keylistener
use graphics.drawstring to draw the character specified by the keypress
redraw the insertion bar
Make sure your keylistener handles the enter key, so that you know when to erase the insertion bar. You're going to paint your JFrame in between each of those steps.
The result will be crude, to say the least, and you'll notice that you have to handle a lot of issues that crop up along the way before things start to look professional. For example, you'll have to deal with font metrics. Not every character is going to be the same width, so you have to calculate the X argument to your drawString() based on the charWidth of whatever character the user types. You might do well to start with a monospaced font for that reason.

Java JPanel used as Canvas to draw text from keyboard input

I'm trying to make a Java11 GUI with Swing and I'm using a JPanel as a Canvas to draw on it images/drawing and I do also want to introduce some text on the Panel so that I can export the whole content of JPanel later.
Thought about using drawString() method in my PaintComponent() method, so should I use a JOptionPane to insert keyboard input or there are other better practices to write directly on JPanel until Enter key press or Escape.
Also, the main question I have regarding this, how I can make the text to be wrapped on multiple lines?

java code for TextBox in MS Paint

I am trying to create a code that simulates a MS Paint in java using Jframe. I want to create a textbox like field like MS Paint has,wherein you drag a box and according to your preference you set a size for it. What I do is I draw a rectangle first and then get the dimensions by mouse event listener and pass these values to a function that creates a JtextArea of given size and width. However, I need to extend the Jframe class which creates a new frame on top of the one that already exists. I try to pass my original frame as a parameter to draw upon for the JtextArea which does not work. Is there any way of implementing JtextArea without extending the frame class? And If possible any relevant example to draw a textbox which is similar to MS Paint. Please note that I don't want to use the Graphics.drawstring method. Thanx.
Since you don't want to use drawString() directly, java.awt.font.TextLayout is probably the best option for rendering text.
Create a temp JTextArea and add to your drawing panel with null layout to be placed over the rectangle.
When edit is done (text entered) remove the temp textarea, get the user entered text and draw it in the original rectangle.

How can I change colors of components when the mouse is pressed in a JFrame in Java?

I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p

on click add dynamic text area -like object in Java

The title is a bit confusing, but I will be using Java and Jframe. Basically, I want to be able to click anywhere on the form and have a "text area/box" show up (maybe use a JTextField or JTextArea ?). I want the user to be able to edit, delete and move this string around as well.
I am thinking I need an actionlistener to listen for clicks on the form. Each click will call for a new text"box" to be created. I am not sure how to make this "box" editable, deleteable, or moveable by the user though.
I need a way to store the string and co-ordinate data too. Would it be a good idea to simply extend the JTextField or JTextArea to add co-ordinate information to them? I see that swing is event based, so I need some kind of trigger to "save" the text (was thinking the enter key, but I realize I'd like the user to be able to enter multi-line strings).
Any thoughts would be appreciated. I am familiar with Java but only have a bit of experience with the UI portion.
Instead of an ActionListener you will need a MouseListener to track clicks.
Sounds like you need an undecorated JInternalFrame with a text box in it on JDesktopPane. However, I don't think you can create an undecorated JInternalFrame, maybe start with a normal JInternalFrame with a TextBox in it and create new frames on mouse clicks on the Desktop Pane. Then see if you can make the JInternalFrame more like a Window.
Another route is a custom component that does everything you need. This is possible, just a lot more custom code.

Categories