2 Attachment(s)
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:
Attachment 4312
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:
Attachment 4313
Sorry if my title and my wording doesn't make any sense.
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.
Re: Making text look like it belongs in the input box
Quote:
Originally Posted by
Fubarable
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:
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:
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);
}
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.
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);
}
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.
Re: Making text look like it belongs in the input box
Heck.
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();
}
});
}
}
Re: Making text look like it belongs in the input box