I have a table like this:
All what I want is to align easy, medium and hard texts in the middle of the cell, at least horizontally.
I searched a lot on how to do this, but I didn't find anything useful...
My code for the table is this:
table.setBounds(0, 0, Info.Width, Info.Height);
table.row();
table.add(label).expandX().colspan(3);
table.row().padTop(20).width(Info.Width);
table.add(easyText).width(Info.Width / 3).expandX();
table.add(mediumText).width(Info.Width / 3).expandX();
table.add(hardText).width(Info.Width / 3).expandX();
table.row().padTop(10);
table.add(easyHighscoreLabel).width(Info.Width / 3);
table.add(mediumHighscoreLabel).width(Info.Width / 3);
table.add(hardHighscoreLabel).width(Info.Width / 3);
table.row().padTop(50);
table.add(backButton).expandX().colspan(3);
Info.Width, Info.Height are the width and the height of the app.
label is the title: "HIGHSCORES".
easyText, mediumText, hardText are the texts I want to align.
easyHighscoreLabel, mediumHighscoreLabel, hardHighscoreLabel, are the numbers below the text.
And backButton is "Back" from bottom.
Use setAlignment (int alignment) on your label by default it is left center.
easyText.setAlignment(Align.center);
mediumText.setAlignment(Align.center);
hardText.setAlignment(Align.center);
This will align all the text by center within the label.
Related
I am trying to draw a string exactly in the center of a rectangle using JavaFX GraphicsContext2D.
I don't want to use JavaFX components so please do not recommend them.
For example, I stroke a rectangle with attributes: x = 10, y = 10, width = 100, height = 100. Now I want to stroke a text in a way that it comes exactly in the center(horizontally and vertically) of the rectangle. How can I do it?
As #James_D comments, you can use the GraphicsContext methods setTextAlign() and setTextBaseline() to center the fillText() in an arbitrary Rectangle. Starting from this example, I added the following lines in the tooltips loop in order to produce the image shown:
gc.setTextAlign(TextAlignment.CENTER);
gc.setTextBaseline(VPos.CENTER);
gc.setFill(Color.BLACK);
gc.fillText(color.toString(),
bounds.getX() + bounds.getWidth() / 2,
bounds.getY() + bounds.getHeight() / 2);
I have a Java Swing form and JLabel like this:
What I need to do is inserting a gap in the beginning of the JLabel:
So it will not stuck to the border line.
Note 1 : I already used jLabName.setIconTextGap(35); but it did the below:
I need to insert the gap before the icon not after it!
Note 2 : The Border Setting And Type And other setting:
You can use compound borders for that.
For Example.
//get border of your component which is button as you say
Border border = myButton.getBorder();
//create a new empty border with name it margin
Border margin = new EmptyBorder(0,10,0,0); //top 0, left 10 , bottom 0, right 0
//now set compound border to your button(component), with margin
myButton.setBorder(new CompoundBorder(border, margin));
//NOTE: CompoundBorder accepts two borders as arguments, first one is inner border and last one is outer border
Fixed it with the following code:
a1.setBorder(new CompoundBorder(new EtchedBorder(), BorderFactory.createEmptyBorder(1, 8, 1, 1)));
I have a Label object with a text in it...
Label label = new Label(text);
..and I get the height by...
label.pack();
GlyphLayout layout = label.getGlyphLayout();
float height = layout.height;
When I add them to a table I just said
Table t = new Table();
t.add(label).width(200).height(height);
Currently I work on a chat window and each entry of the
window is a Label containing a colored name and text, like...
Note: The debug lines are drawn. Please ignore the blue line, it's from another table in the scene
Now my question: The 1-liner has a glyphlayout height of 38 and the 3-liner 139. Does someone know why there's such a big gap between the 2 entries resp. why the 3-liner height doesn't seem to be correct? No extra spacing / padding.
I would appreciate any ideas to solve this.
SOLVED:
My post was incomplete, sorry. I set the width of the label before calling...
label.setWidth(x); // where x was < chatbox width
label.pack();
So the GlyphLayout assumed a width which was smaller than the actual chatbox width. The result is of course a bigger height...
Thanks a lot to the libgdx IRC, especially Tomski :)
How do I set the position of text so that it is centered vertically relative to its page size? I want to position it say for example x number of points from right and centered vertically. The text of course is rotated 90 degrees.
int n = reader.getNumberOfPages();
PdfImportedPage page;
PdfCopy.PageStamp stamp;
for (int j = 0; j < n; )
{
++j;
page = writer.getImportedPage(reader, j);
stamp = writer.createPageStamp(page);
Rectangle crop = reader.getCropBox(1);
// add overlay text
Phrase phrase = new Phrase("Overlay Text");
ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, phrase,
crop.getRight(72f), crop.getHeight() / 2, 90);
stamp.alterContents();
writer.addPage(page);
}
The code above gives me inconsistent position of text, and in some pages, only a portion of the "Overlay text" is visible. Please help, I don't know how to properly use mediabox and cropbox and I'm new to itext.
Thanks!
Regarding the inconsistent position: that should be fixed by adding the vertical offset:
crop.getRight(72f), crop.getBottom() + crop.getHeight() / 2
Do you see? You took the right border with a margin of 1 inch as x coordinate, but you forgot to take into account the y coordinate of the bottom of the page (it's not always 0). Normally, this should fix the positioning problem.
Regarding the fact that only a portion of the overlay text is visible: my first guess was that you're adding content under the existing content, but that guess is wrong (you're using getOverContent()). What exactly do you mean by that second question? Do yo mean the text is clipped by the CropBox? Are you looking for a way to measure the content of phrase to see if it fits the height before you add it?
We have an app that draws text, but then displays a JTextArea for the user to edit the text when they click on the text. However, the wrapping between these two text-handling components differs. They use the same width, text String, and Font.
For the text-drawing, I'm using the from the Java tutorial, which I've also seen used by others in related questions here and other forums. Here's that part of the code:
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout;
AttributedString attrString = new AttributedString(myText);
AttributedCharacterIterator charIterator;
int paragraphStart;
int paragraphEnd;
LineBreakMeasurer lineMeasurer;
float breakWidth;
float drawPosX;
float drawPosY;
attrString.addAttribute(TextAttribute.FONT, myFont);
charIterator = attrString.getIterator();
paragraphStart = charIterator.getBeginIndex();
paragraphEnd = charIterator.getEndIndex();
lineMeasurer = new LineBreakMeasurer(charIterator, frc);
// Set break width to width of Component.
breakWidth = myTextWidth;
drawPosY = startY
// Set position to the index of the first character in the paragraph.
lineMeasurer.setPosition(paragraphStart);
textBounds = new Rectangle(startX, startY(), 0, 0);
// Get lines from until the entire paragraph has been displayed.
while (lineMeasurer.getPosition() < paragraphEnd) {
layout = lineMeasurer.nextLayout(breakWidth);
// Compute pen x position. If the paragraph is right-to-left we
// will align the TextLayouts to the right edge of the panel.
drawPosX = layout.isLeftToRight()
? startX() : breakWidth - layout.getAdvance();
// Draw the TextLayout at (drawPosX, drawPosY).
layout.draw(g2d, drawPosX, drawPosY);
lineBounds = new Rectangle2D.Float(drawPosX, drawPosY - layout.getAscent(), layout.getAdvance(), (layout.getAscent() + layout.getDescent() + layout.getLeading()));
// Move y-coordinate in preparation for next layout.
drawPosY += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
The JTextArea is much simpler:
JTextArea textArea = new JTextArea(myText);
textArea.setSize(myTextWidth, myTextThing.getHeight());
textArea.setOpaque(true);
textArea.setVisible(true);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(myFont);
textArea.setBorder(null);
I set the border to null because I have another rectangle drawn outside the bounds of the text area with a dashed area to show where it is. Might seem silly now, but we use it to show the bounds of the text area when the user first selects the text they want to edit. At that point, the JTextArea isn't yet created. They have to click on it again to begin editing. The reason for this is that once a text area is selected, they may also drag and resize the text area, and that gets messy and more confusing if they had a live JTextArea when they started dragging and resizing.
Separately, both the drawn TextLayouts and the JTextArea appear to wrap words just fine. but when used together you can see the difference. The problem with this is that while the user is editing the text, the JTextArea is doing its thing to wrap the text. But when the user JTextArea loses focus, it is converted to the drawn text, and then the words may be wrapped differently.
Fill the text area with i or l characters. Grab a UI ruler or magnifying glass and count the size of your text area in pixels from the leftmost character of the longest line to the rightmost. Do the same with n, m, and a few other characters for a few more data points. I suspect that the text area has an invisible border of a few pixels it uses even when set to no border. If this is the case, add the same border around the TextLayout component and they should appear identical.
(Alternatively to counting pixels, you could set a background color for the text or the components, but I wouldn't necessarily trust it.)