Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By Fubarable

Thread: Making text look like it belongs in the input box

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Making text look like it belongs in the input box

    Ok, I have made a little form using Graphics, here is what it looks like:
    Making text look like it belongs in the input box-wowowo.png
    My question is: how can I make it so the text "The quick brown fox jumps over the lazy dog" fit into the box? I mean like make it so it looks like an actual input box. Like so if the text is too long, it will cut off the end of the text and move it so it looks normal. Like this:
    Making text look like it belongs in the input box-like-.png

    Sorry if my title and my wording doesn't make any sense.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Making text look like it belongs in the input box

    Are you using a JTextField? If so, it will automatically do this for you.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Making text look like it belongs in the input box

    Quote Originally Posted by Fubarable View Post
    Are you using a JTextField? If so, it will automatically do this for you.
    No, I'm just painting text on the screen using Graphics2D.
    Is it possible to this using the Text API? If so do you know how?

    Here is my progress so far:
    Java Code:
    public void renderTextForInput(String text, int width, int x, int y, int color){
    		AttributedString as1 = new AttributedString(text);
    		as1.addAttribute(TextAttribute.FONT, font);
    		
    		screen.g2d.setColor(Misc.intToColor(color));
    		screen.g2d.drawString(as1.getIterator(), x, y);
    	}
    I have no idea where to go from here.

    I guess that all I need to do is make the text scroll.

    UPDATE ------

    Ok, I have made it so the text scrolls, I just need to cut it off. Any ideas on how to do that?
    Here is my code so far:
    Java Code:
    public void renderTextForInput(String text, int width, int x, int y, int color){
    		AttributedString as1 = new AttributedString(text);
    		if(text.length() == 0){
    			return;
    		}
    		as1.addAttribute(TextAttribute.FONT, font);
    		
    		int textWidth = getTextWidth(text);
    		int difference = width - textWidth;
    		
    		System.out.println(difference);
    		int toAdd = 0;
    		if(difference < 0){
    			toAdd = difference;
    		}
    		
    		screen.g2d.setColor(Misc.intToColor(color));
    		screen.g2d.drawString(as1.getIterator(), x+toAdd, y);
    	}
    Last edited by PhQ; 12-26-2012 at 04:35 AM.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Making text look like it belongs in the input box

    Done it. However, I don't think it's efficient. I feel like it could be done a lot better.
    Java Code:
    public void renderTextForInput(String text, int width, int x, int y, int color){
    
    		AttributedString as1 = new AttributedString(text);
    		if(text.length() == 0){
    			return;
    		}
    			
    		as1.addAttribute(TextAttribute.FONT, font);
    		BufferedImage img = new BufferedImage(width, 50, BufferedImage.TYPE_INT_ARGB);
    		Graphics2D g = img.createGraphics();
    		
    		int textWidth = g.getFontMetrics(font).stringWidth(text);
    		int difference = width - textWidth;
    		
    		int toAdd = 0;
    		if(difference < 0){
    			toAdd = difference;
    		}
    		
    		g.setColor(Misc.intToColor(color));
    		g.drawString(as1.getIterator(), 0+toAdd, 11);
    		
    		
    		g.dispose();
    		screen.g2d.drawImage(img, x, y-10, null);
    	}

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Making text look like it belongs in the input box

    Just playing with opacity and fonts you should be able to emulate what you're doing using JTextFields. If not reproduce it exactly, you should be able to get pretty close.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Making text look like it belongs in the input box

    Heck.

    Java Code:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.*;
    import javax.swing.text.JTextComponent;
    
    @SuppressWarnings("serial")
    public class StrangeLogin extends JPanel {
       private static final int PREF_W = 700;
       private static final int PREF_H = 450;
       private static final int TF_COLS = 15;
       private static final float FONT_SIZE = 18F;
       public static final Color FIELD_TOP_COLOR = new Color(100, 0, 0);
       public static final Color FIELD_BOTTOM_COLOR = Color.BLACK;
       public static final Color FIELD_FG_COLOR = Color.WHITE;
       public static final Color FIELD_BG_COLOR = new Color(0, 0, 0, 0);
       public static final Color FIELD_BORDER_COLOR = Color.DARK_GRAY.darker();
       private JTextField userNameField = new UserNameField(TF_COLS);
       private JPasswordField passField = new PassField(TF_COLS);
       private JPanel innerPanel = new JPanel(new GridBagLayout());
    
       public StrangeLogin() {
          setBackground(Color.black);
          innerPanel.setOpaque(false);
    
          JLabel waitingForLabel = new JLabel("Waiting for login ");
          waitingForLabel.setForeground(Color.orange);
          waitingForLabel.setFont(waitingForLabel.getFont().deriveFont(FONT_SIZE));
          JLabel testLabel = new JLabel("test");
          testLabel.setForeground(Color.red);
          testLabel.setFont(testLabel.getFont().deriveFont(FONT_SIZE));
          JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
          titlePanel.setOpaque(false);
          titlePanel.add(waitingForLabel);
          titlePanel.add(testLabel);
    
          JLabel userNameLabel = new JLabel("Username: ");
          userNameLabel.setForeground(Color.white);
          userNameLabel.setFont(waitingForLabel.getFont());
          JLabel passLabel = new JLabel("Password: ");
          passLabel.setForeground(Color.white);
          passLabel.setFont(waitingForLabel.getFont());
    
          addToInnerPanel(innerPanel, new JLabel(), 0, 0);
          addToInnerPanel(innerPanel, titlePanel, 1, 0);
          addToInnerPanel(innerPanel, userNameLabel, 0, 1);
          addToInnerPanel(innerPanel, userNameField, 1, 1);
          addToInnerPanel(innerPanel, passLabel, 0, 2);
          addToInnerPanel(innerPanel, passField, 1, 2);
    
          int eb = 25;
          innerPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
          add(innerPanel);
       }
    
       private void addToInnerPanel(Container container, Component comp, int x,
             int y) {
          int anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST;
          int fill = GridBagConstraints.HORIZONTAL;
          int ins = 10;
          Insets insets = new Insets(ins, ins, ins, ins);
          GridBagConstraints gbc = new GridBagConstraints(x, y, 1, 1, 1.0, 1.0,
                anchor, fill, insets, 0, 0);
          container.add(comp, gbc);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private class UserNameField extends JTextField {
    
          public UserNameField(int columns) {
             super(columns);
             initField(this);
          }
    
          @Override
          protected void paintComponent(Graphics g) {
             myPaint((Graphics2D) g, getWidth(), getHeight());
             super.paintComponent(g);
          }
       }
    
       private class PassField extends JPasswordField {
    
          public PassField(int columns) {
             super(columns);
             initField(this);
          }
    
          @Override
          protected void paintComponent(Graphics g) {
             myPaint((Graphics2D) g, getWidth(), getHeight());
             super.paintComponent(g);
          }
       }
       
       private void initField(JTextComponent textComp) {
          textComp.setForeground(FIELD_FG_COLOR);
          textComp.setBackground(FIELD_BG_COLOR);
          textComp.setBorder(BorderFactory.createLineBorder(FIELD_BORDER_COLOR));
          textComp.setFont(textComp.getFont().deriveFont(FONT_SIZE));
    
       }
       
       private void myPaint(Graphics2D g2, int width, int height) {
          g2.setPaint(new GradientPaint(0f, 0f, FIELD_TOP_COLOR, 0f,
                (float) height, FIELD_BOTTOM_COLOR));
          g2.fillRect(0, 0, width, height);
          
       }
    
       private static void createAndShowGui() {
          StrangeLogin mainPanel = new StrangeLogin();
    
          JFrame frame = new JFrame("Strange Login");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    DarrylBurke and PhQ like this.

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Making text look like it belongs in the input box

    Woah!!!!
    I love you.

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2011, 01:03 PM
  2. Replies: 9
    Last Post: 05-11-2011, 06:12 PM
  3. Replies: 2
    Last Post: 01-13-2011, 05:43 AM
  4. length method belongs to which class
    By nagesh1811 in forum New To Java
    Replies: 6
    Last Post: 05-27-2010, 12:03 PM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 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
  •