Re: moveable sizeable text box

Discussion in 'Java' started by Knute Johnson, Apr 19, 2008.

  1. RichT wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, Apr 19, 2008
    #1
    1. Advertising

  2. RichT wrote:
    > Knute Johnson wrote:
    >> RichT wrote:
    >>> 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.


    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.

    >>
    >> 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
    >>


    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.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Apr 19, 2008
    #2
    1. Advertising

  3. RichT wrote:
    >> 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
    >>


    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();
    }
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, Apr 20, 2008
    #3
  4. RichT wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, Apr 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon

    Moveable panel

    simon, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    440
    Eliyahu Goldin
    Nov 3, 2004
  2. Jesse Aufiero

    sizeable grid

    Jesse Aufiero, Oct 18, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    732
    Jesse Aufiero
    Oct 19, 2005
  3. Jason

    User sizeable panel/table

    Jason, Apr 24, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    604
    Jason
    Apr 25, 2006
  4. Replies:
    9
    Views:
    638
  5. Jeff Higgins

    Re: moveable sizeable text box

    Jeff Higgins, Apr 19, 2008, in forum: Java
    Replies:
    1
    Views:
    599
    Jeff Higgins
    Apr 20, 2008
Loading...

Share This Page