TextBox tool for drawing application - JavaFX - java

I'm trying to create some drawing functionality, effectively very similar to MSPaint. I want to make a TextBox tool, such that I can draw a textbox on my canvas and have a user type in it like a real textbox. Once the user clicks off of the textbox or presses escape, then I would "stroke" the text to the GraphicsContext.
I'm not sure how to do this. My current attempt has me using StackPane to stack two canvases on top of each other. I let the user "draw" a textbox with the mouse, and then I create a textbox programmatically which I am trying to pass the keyevents into. Then when the user clicks off, I write textbox.getText() to the GraphicsContext. This has not been very successful. Specifically, I'm not sure how to handle special keys like BACKSPACE and DELETE and letting the user click to place the cursor.
I'm looking for advice on a better approach.

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 Swing - Having multiple pages and using buttons to go through them

I am trying to make a program that essentially allows the user to switch through different areas of content for the user. The content area is essentially a place where you can draw different shapes.
For example, when the user starts up the program, they are able to edit and do things to the content area that pops up. However, there is the menu with the buttons "New", "Previous", "Next", "Delete".
When you click "New" you should be able to essentially create a new content area (not a new page).
When you make multiple content areas, you should be able to click "Next" or "Previous" to essentially go through the different content areas as soon as you click the button.
When you click "Delete" you should be able to delete the current content area you are at.
Finally, you should be able to not be able to click "Next" when you are at the end of the content areas and same with "Previous" when you are at the start.
Also, there should not be a creation of multiple pages, but rather the content area is essentially changing only. So, the menu bar stays in the same place with the content only changing when clicking the button. In this case, the content area is a space where you can draw shapes and when you add a new content area it should be a blank space and you can draw new things. As you access the previous content space, you should be able to see the old drawings
The card layout option seems like an option, but how would I take into account for there being a large number of pages?
Any ideas of how to approach this?

Java SWT: Show Zoombox around cursor

I need your help with my application.
I'm writing a program that displays big images in tiles, using the SWT MapWidget (http://mappanel.sourceforge.net/swt/).
The user has the ability to place markers on the map, when he pressed the mouse for a couple seconds. Additionally, I also need a little ZoomBox next to the cursor that shows the area around the cursor, so that the user has better precision when placing a marker. This ZoomBox is to be displayed while the user keeps the mouse pressed. Placing the markers ia already working fine.
I have problems with implementing the ZoomBox, specifically displaying it. I found a post that does just what I need (Zoom box for area around mouse location on screen). The problem is, this post is for Swing and not for SWT.
I have already built a SWT_AWT-Bridge, so I have the ZoomPanel already in my shell. What I need is the functionality of displaying it on top of the MapWidget, while the user keeps the mouse pressed.
I figured, I need to use a StackLayout, but I don't know how.
EDIT
To clarify, my question is, how can I display the composite containing my ZoomBox on top of my image, while the user keeps the mouse pressed?

Make JButton Look Like It Has Input Focus

Is there any way to make a JButton look like it has the input focus when it doesn't?
I am creating a search window that has a text area for the input and a 'Find Next' button to start the search. I want the 'Find Next' button to appear to have the focus even though keyboard input is going to the text area. The idea is notify the user that pressing <Enter> will start the search, similar to applications like Microsoft Word.
I know that I can paint the button myself, but I'm looking for an easier way.
This is not the same as input focus; it's just that the button should be the default button of the dialog.
Get the JRootPane using getRootPane() on your dialog/frame/window and use setDefaultButton on that instead.
L&Fs might draw the default button even differently than one having input focus, so a user might be quite confused if there are two apparent controls having focus.

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