java code for TextBox in MS Paint - java

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.

Related

Text input in Java Game w/o Swing

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.

Using an image instead of JDialog/frame to hold swing components?

I currently have a JDialog (class that implements JDialog and is constructed like a jframe), and has 3 swing buttons placed on it. Currently I have it set, undecorated = true, to hide the outer frame. Is there any way to use my image to replace the default square frame?
This is what I aim for :
The blue square with shadow is the pre made image.
Regards
The blue square with shadow is the pre made image.
Well, the best way would be to set the background of the panel and then add a ShadowBorder to the panel. This will provide you with far more flexibility in the future as you can create many panels with different colors and reuse the same ShadowBorder instead of having to create an Image every time. I don't have an example of a ShadowBorder, but you might find one if you search the web.
Is there any way to use my image to replace the default square frame?
But if you really want to use your premade Image, then you can just:
create a JLabel and add your Image to the label as an Icon
add the label to the dialog
set the layout manager of the label
add your components to the label.

Draw round Button in LookAndFeel-Style

I'm experimenting with JRadioButton's to put them on a JToolbar and select the last one clicked. If i'd use JButtons they wouldn't keep the Selection.
Since JRadioButton always have that Dot, I need to draw them myself by overriding the paint-methods.
The Button's will be circles with an Icon in it. That works if I draw Images, but looks aweful. The problem I have is that I would like to draw the circle so that these Buttons always look like the JButtons with the current LookAndFeel.
How can i do that? I searched for a while now, but I didn't find methods to read some default-colors of the LookAndFeel which I could use.
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
See UIManager Defaults.
I need to draw them myself by overriding the paint-methods
Don't do custom painting in the component. If you don't like the default Icons, then create your own Icon and do the custom painting there or create an Image and use an ImageIcon. The you can use the setXXXIcon() methods.

Painting a JComponent without adding it to a container

I've implemented a custom JPanel, whose paint method I've extended to do a lot of manual rendering in full screen mode. Now I would like to integrate another JComponent to this (in my case a JPanel that contains a JScrollpane with a JTextPane as its viewport) that should appear on top of my first panel, but because my custom rendering pipeline is complex, adding the JComponent to my panel and having it painted the traditional way through the AWT system is not an option (I tried and it's quirky at best, not functional at worst), so my question is: is it possible to manually order the JComponent to be painted at one point in my program by calling its regular paint method without tying it to a JContainer and if yes, how do I do this?
Thanks in advance for your answers.
See the LabelRenderTest.java source on this thread. The label is eventually drawn to screen, but it is painted to BufferedImage before ever being displayed.
The important line of the source is..
textLabel.setSize(textLabel.getPreferredSize());
You can take a look at CellRendererPane and see how for example BasicTableUI paints component images with it.
Yes, just call the normal paint method on the object and pass the Graphics you want it to paint on. However, this is just going to paint it and it sounds like you want it to possibly scroll which means you will need to add it to your custom JPanel. In that case just add the panel and you a layout manager that will place the component where you need it.
You should set size for the component. Then to position it use your Graphics' translate(x,y) to position the component in desired Point.
if there is any container higher level in the hierarchy you can use
validate(); repaint();
pair to do that.
if not you can change it's size or bounds ( like +1 , -1 ) at the end to make it repaint itself.

How can i get the JFrame (parent) of a JInternalFrame?

I'm doing a program like Gimp and i have a JInternalFrame for change the color of shapes and
i paint the shapes in other JInternalFrame so i have to get my frame to get the JInternalFrame
where i want to paint. I don't know how to get the parent of the colors JInternalFrame to change the color in the JInternalFrame where I'll paint.
Look at SwingUtilities.getAncestorOfClass(Class c, Component comp). For example:
You can use SwingUtilities.getAncestorOfClass(JFrame.class,compinstance)
What ever is doing the painting needs a reference to the selected color. I would suggest not directly referencing one frame to another but instead have a central variable that holds the selected color that can then be used by whatever tool needs it. Once the use "paints" on the screen you will need to store what the paint in some format so that your Java code can continue to repaint it.

Categories