Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Printing a functioning JTextField on Graphics2D

  1. #1
    Decimal314 is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Printing a functioning JTextField on Graphics2D

    Hey, I recently started making some Java games, and now I'm having some troubles,
    It seems like I can't place a functioning JTextField on my Graphics2D object, I can place images, even the image of the component itself:
    Java Code:
    Graphics2D g2d = (Graphics2D) this.getGraphics();
    JTextField jtf = new JTextField();
    jtf.setSize(150, 70);
    jtf.setLocation(220, 220);
    jtf.print(g2d);
    How can I make it work? I really need it for character name creation, chat, etc... so making a JDialog isn't an option for me.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Printing a functioning JTextField on Graphics2D

    What you're doing is like trying to catch a mouse with a picture of a mousetrap. Use a suitable layout and add the JTextField to your Container.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Printing a functioning JTextField on Graphics2D

    Quote Originally Posted by DarrylBurke View Post
    What you're doing is like trying to catch a mouse with a picture of a mousetrap. Use a suitable layout and add the JTextField to your Container.
    db
    Yeah. Experience will show that you can only catch a picture of a mouse with a picture of a mouse trap ;)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Printing a functioning JTextField on Graphics2D

    Well this is for a game and I'll assume that this involves the game being custom painted into for example a JPanel inside a JFrame which is not resizable or even fullscreen, so probably this would be the rare exception where absolute positioning (IE: with a null-layout) would be the way to go.

    But I'm only guessing as the question gives no context at all.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Printing a functioning JTextField on Graphics2D

    Quote Originally Posted by jim829 View Post
    Yeah. Experience will show that you can only catch a picture of a mouse with a picture of a mouse trap ;)

    Regards,
    Jim
    Only if you bait it with a picture of cheese
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Printing a functioning JTextField on Graphics2D

    Quote Originally Posted by gimbal2 View Post
    Well this is for a game and I'll assume that this involves the game being custom painted into for example a JPanel inside a JFrame which is not resizable or even fullscreen, so probably this would be the rare exception where absolute positioning (IE: with a null-layout) would be the way to go.

    But I'm only guessing as the question gives no context at all.
    OverlayLayout with setAlignmentX / Y (and maybe setOpaque(false)), along with an EmptyBorder to space the component away from the edge, could be a viable option in this scenario.

    db
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Printing a functioning JTextField on Graphics2D

    I bow to your master knowledge, oh untitled one.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 0
    Last Post: 04-19-2014, 09:41 AM
  2. Threading not functioning
    By Feonx in forum AWT / Swing
    Replies: 2
    Last Post: 10-31-2011, 09:57 PM
  3. JDialog Not Functioning Properly
    By StevenTNorris in forum AWT / Swing
    Replies: 5
    Last Post: 10-26-2011, 07:24 PM
  4. Word Wrap not functioning correctly?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-08-2009, 07:41 PM
  5. Applet not functioning
    By Shadaw in forum Java Applets
    Replies: 2
    Last Post: 12-18-2008, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •