Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

moveable sizeable text box

Thread view: 
RichT - 19 Apr 2008 01:06 GMT
Hi,
Does anyone know how to create a text box that can be edited resized and
move about an image in a JComponent using the mouse?

Also I have a modal dialog and would like to be able to click the screen
and populate a text box with the coordinates.

When the dialog is closed I would like the top left corner of the
textbox to be drawn where the coordinates were selected.

Thanks in advance
Rich
Knute Johnson - 19 Apr 2008 03:25 GMT
> Hi,
> Does anyone know how to create a text box that can be edited resized and
> move about an image in a JComponent using the mouse?

The simplest thing I can think of is to put the text box in a Dialog.
Then you can resize the dialog and the text box will go along.

> Also I have a modal dialog and would like to be able to click the screen
> and populate a text box with the coordinates.

Put a MouseListener on the component that you want to know the
coordinates of and then set those values in the text box when the mouse
is pressed or clicked or something.

> When the dialog is closed I would like the top left corner of the
> textbox to be drawn where the coordinates were selected.

Don't use a layout manager on that component and then position it manually.

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

RichT - 19 Apr 2008 13:25 GMT
>> Hi,
>> Does anyone know how to create a text box that can be edited resized
>> and move about an image in a JComponent using the mouse?
>
> The simplest thing I can think of is to put the text box in a Dialog.
> Then you can resize the dialog and the text box will go along.
This sounds like a great idea, I set undecorated to true to remove the
title bar etc., and I am left with just an outline with the text area
filling the JDialog with a 5 border all around.

I have resizeable set to true, but I am unable to get a handle to resize
or move it, any ideas? I am using netbeans 6 by the way to design this.

>> Also I have a modal dialog and would like to be able to click the
>> screen and populate a text box with the coordinates.
>
> Put a MouseListener on the component that you want to know the
> coordinates of and then set those values in the text box when the mouse
> is pressed or clicked or something.
Thanks :)

>> When the dialog is closed I would like the top left corner of the
>> textbox to be drawn where the coordinates were selected.
>
> Don't use a layout manager on that component and then position it manually.
I am not sure I understand this bit, after the dialog ok button is
pressed  I wanted the text displayed in a moveable sizeable text box on
the JComponent which has the image drawn to it, then when save is
selected tat text will be drawn into the image and the text box will be
disposed of.

Cheers
Rich
Knute Johnson - 19 Apr 2008 17:02 GMT
>>> Hi,
>>> Does anyone know how to create a text box that can be edited resized
[quoted text clipped - 8 lines]
> I have resizeable set to true, but I am unable to get a handle to resize
> or move it, any ideas? I am using netbeans 6 by the way to design this.

A window without title bar probably cannot be resized.

>>> Also I have a modal dialog and would like to be able to click the
>>> screen and populate a text box with the coordinates.
[quoted text clipped - 17 lines]
> Cheers
> Rich

OK so what you are really trying to do is to draw text on an image and
you need the GUI part to accomplish that, yes?

So you really will have a couple of problems to solve, first how to
position the text on the image and then how to get the text in the first
place.  Your idea of using a moveable/resizable text box probably won't
work well as you want.  Why not have the user select from a menu the
option to draw text on the image, then draw a line box on the image as
the placement area and make that box movable/resizable and pop up a
dialog to enter the text into.  As text is entered into the dialog you
can draw it in the image box and you will be able to position it as
well.  I know that this sounds pretty complicated but I don't think it
will really be that hard to do.

Signature

Knute Johnson
email s/nospam/linux/

RichT - 20 Apr 2008 00:12 GMT
> A window without title bar probably cannot be resized.
boo ;) I thought this may be the case, what a shame thoughcould have
been a quick reasonable solution.

> OK so what you are really trying to do is to draw text on an image and
> you need the GUI part to accomplish that, yes?
Yes :)

> So you really will have a couple of problems to solve, first how to
> position the text on the image

I have an dear how to achieve this, if I make the dialog a listener to
the mouse, when I have focus of the coords text box on the dialog, I
could click a positon in the image and populate the coords text box from
the mouse coords yes?
and then how to get the text in the first
> place.  Your idea of using a moveable/resizable text box probably won't
> work well as you want.  
:( my ideas rarely do exactly as I had imagined.
Why not have the user select from a menu the
> option to draw text on the image, then draw a line box on the image as
> the placement area and make that box movable/resizable and pop up a
> dialog to enter the text into.  
I am unfamiliar with a line box, I would still like to have the text
displayed before it is committed to the image.

As text is entered into the dialog you
> can draw it in the image box and you will be able to position it as
> well.  I know that this sounds pretty complicated but I don't think it
> will really be that hard to do.
I oly really want to draw into the image just before saving, I really
want a place holder as you describe earlier for each piece of text, yes
there could be several pieces of text to render.

I already have he idea for multiple text boxes, I could use an arraylist
or similar and then iterate over it before saving to draw ach piece of text.

Thanks for your help so far
Rich
Knute Johnson - 20 Apr 2008 05:22 GMT
>> A window without title bar probably cannot be resized.
> boo ;) I thought this may be the case, what a shame thoughcould have
[quoted text clipped - 36 lines]
> Thanks for your help so far
> Rich

Here is a simple example of what I am suggesting.  You can make
selections for color, font and size if you wanted too.  This is similar
to the text box on the Gimp image program.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;

public class AddTextJComponent extends JComponent implements
ActionListener {
    final BufferedImage image;
    final JTextField tf = new JTextField(20);
    final Rectangle rect;
    final JDialog dialog;
    boolean drawRect;
    boolean moveFlag;
    int startX,startY;

    public AddTextJComponent() throws IOException {
        image = ImageIO.read(new File("kittens.jpg"));
        setPreferredSize(new Dimension(
          image.getWidth(),image.getHeight()));

        MouseInputAdapter mia = new MouseInputAdapter() {
            public void mousePressed(MouseEvent me) {
                Point p = new Point(me.getX(),me.getY());
                if (rect.contains(p)) {
                    moveFlag = true;
                    startX = rect.x;
                    startY = rect.y;
                }
            }
            public void mouseReleased(MouseEvent me) {
                moveFlag = false;
                dialog.toFront();
            }
            public void mouseDragged(MouseEvent me) {
                if (moveFlag) {
                    rect.x = me.getX();
                    rect.y = me.getY();
                    repaint();
                }
            }
        };

        addMouseListener(mia);
        addMouseMotionListener(mia);

        DocumentListener dl = new DocumentListener() {
            public void changedUpdate(DocumentEvent de) {
                change(de);
            }
            public void insertUpdate(DocumentEvent de) {
                change(de);
            }
            public void removeUpdate(DocumentEvent de) {
                change(de);
            }
            void change(DocumentEvent de) {
                repaint();
            }
        };

        tf.getDocument().addDocumentListener(dl);

        rect = new Rectangle();

        dialog = new JDialog((Frame)getTopLevelAncestor(),
         "Enter Text",false);
        dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                drawRect = false;
                repaint();
            }
        });
        dialog.add(tf);
        dialog.pack();
    }

    public void actionPerformed(ActionEvent ae) {
        drawRect = true;
        dialog.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(image,0,0,null);

        g.setColor(Color.BLUE);

        FontMetrics fm = g.getFontMetrics();
        int textWidth = fm.stringWidth(tf.getText());
        int textHeight = fm.getHeight();

        g.drawString(tf.getText(),rect.x + 5,rect.y + textHeight);

        // adjust the size of the rectangle for the amount of text
        rect.setSize(new Dimension(textWidth + 10, textHeight + 10));

        if (drawRect)
            g.drawRect(rect.x,rect.y,rect.width,rect.height);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    AddTextJComponent comp = new AddTextJComponent();
                    f.add(comp);
                    JMenuBar mb = new JMenuBar();
                    f.setJMenuBar(mb);
                    JMenuItem m = new JMenuItem("Text");
                    mb.add(m);
                    m.addActionListener(comp);
                    f.pack();
                    f.setVisible(true);
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        });
    }
}

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

RichT - 20 Apr 2008 19:45 GMT
Thanks for this Knute,
pure brilliance :)

Rich
Knute Johnson - 20 Apr 2008 22:22 GMT
> Thanks for this Knute,
> pure brilliance :)
>
> Rich
   
There is a lot more that you can do with that.  Use a JTextArea for
input to get multi-line text, put a CANCEL option on the dialog, use a
more complex color and line scheme for the display box so that it will
be visible on all colors of background image, make some sort of snap to
grid or centering option on the display box, store the text data
somewhere so that it can be undone, there's a million possibilities.

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

RichT - 20 Apr 2008 23:18 GMT
>> Thanks for this Knute,
>> pure brilliance :)
[quoted text clipped - 7 lines]
> grid or centering option on the display box, store the text data
> somewhere so that it can be undone, there's a million possibilities.

millions of possibilities? if I have told you once I have told you a
trillion time not to exaggerate :)

Seriously thanks for your help this is just the kick start I needed, now
I have a base to work with I can expand and have already done so today

Thanks Knute Johnson
Rich
Jeff Higgins - 19 Apr 2008 20:10 GMT
> Hi,
> Does anyone know how to create a text box that can be edited resized and
[quoted text clipped - 5 lines]
> When the dialog is closed I would like the top left corner of the textbox
> to be drawn where the coordinates were selected.

<http://java.sun.com/developer/onlineTraining/Media/2DText/style.html#layout>
RichT - 20 Apr 2008 00:01 GMT
> <http://java.sun.com/developer/onlineTraining/Media/2DText/style.html#layout>

Hi,
thanks for this, but I have solved the drawing text to the image part, I
am now stuck on how to create a resizeable moveable text box that I can
move about during runtime.

Rich
Jeff Higgins - 20 Apr 2008 00:26 GMT
>> <http://java.sun.com/developer/onlineTraining/Media/2DText/style.html#layout>
>
> Hi,
> thanks for this, but I have solved the drawing text to the image part, I
> am now stuck on how to create a resizeable moveable text box that I can
> move about during runtime.

Where are you stuck?

Drawing a box (Rectangle?)?
Making it movable (Drag with mouse?)?
Making it resizable (drag with mouse?)?
Defining what it means to be a resizeable moveable text box,
within the confines of your drawing environment?
Have you considered using a higher level graphics API?
RichT - 20 Apr 2008 01:18 GMT
> Where are you stuck?
>
> Drawing a box (Rectangle?)?
Yes can do that, filled rect. :)
> Making it movable (Drag with mouse?)?
add mouse listener :)
> Making it resizable (drag with mouse?)?
not going to bother with resize now ;p
> Defining what it means to be a resizeable moveable text box,
> within the confines of your drawing environment?
something that behaves like a label/text area that can be moved around
the image at runtime without destroying the image as moving. I am
guessing some kind of Xor operation in paintComponent?
> Have you considered using a higher level graphics API?
I thought Java2D was high level?

Ok thinking about this I can crate a class extending a text area add a
mouse listener to check if mouse clicked on this object if so set an
allow dragging flag and then check in the mouse move event if the flag
is set, if so calculate new coords and redraw?

When the time is right, get text info from text box, find the bottom
(not top as I first stated in my post) corner of text box location and
then render the text into the image making it a permanent fixture so to
speak.

Thanks for this starter, sometimes it is hard to seethe wood for the trees.

Rich


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.