I have a screen with a RitchTextField added, which has a custom font applied. I want to find the total number of lines that the RitchTextField can hold to avoid vertical scrolling.
I tried to do this by getting the height of the RichTextField then dividing it by the the height of the font, the problem however is using rtfField.getHeight() always returns the current height of field, and using the screens getHeight() returns the screen size not taking other fields into consideration.
For example:
Screen Size using getHeight() = 360.
Font Size using: this.rtfField.getFont().getHeight() = 25
Total Lines ~ 12
However doing a manual count the screen only comfortably displays 8 lines.
Not sure if this is what you're looking for, but I think you want the current height of the field divided by the font height:
int lines = this.rtfField.getHeight() / this.rtfField.getFont().getHeight();
Related
I'm trying to draw 2048 boxes onto a screen with a width and a height. (I am making this in Java)
I have the following variables:
width = 1024
height = 768
population = 2048
Based on these variables, how can I draw a grid with x amount of columns and y amount of rows allowing the whole layout to perfectly fit the desired width and height?
For example:
I have a width and height of 4 x 4 and a total population of 4, I therefore would have 2 columns and 2 rows.
Example 2:
I want to reproduce the exact same thing in the screenshot above, but I do not know how many rows and columns I need to loop through as well as the size of each square.
All I know is the total width/height of the window, along with the total number of boxes I want to draw.
The best thing you can do to answer questions like this for yourself is to get out a piece of paper and a pencil, and draw out a bunch of examples. Label the width and height of the window, as well as the position and width and height of each individual box. Keep drawing examples until you notice a pattern.
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. You need to break your problem down into smaller pieces and take those pieces on one at a time. That being said, I can try to help in a general sense.
First, you need to figure out how many rows and columns your grid should have. There isn't just a single correct way to go from a population to a row and column count. In fact, it might not even be possible- how would you split up a population of 3? There are also multiple solutions: if your population count is 20, then you could have a grid that's 1x20, 2x10, 4x5, 5x4, 10x2, or 20x1. I recommend you treat this as a separate problem and post a separate question about that.
But once you have the row and column count, you can use some basic math to figure out the position and size of each box. Again, drawing out some examples would help, but the basics would look like this:
float cellWidth = width/columns;
float cellHeight = height/rows;
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
float cellX = cellWidth*column;
float cellY = cellHeight*row;
rect(cellX, cellY, cellWidth, cellHeight);
}
}
Note that I got this code from this tutorial, but you'll find examples all over the internet if you do some googling. If you still can't get it working, please post a MCVE showing exactly what you tried, and we'll go from there. Good luck.
If the boxes need to be rectangular, there are multiple possible solutions. eg. for area 12, box can be of 12x1, 1x12, 4x3, 3x4, 6x2, 2x6, etc.. let alone the floating values.
If square boxes are OK:
area = available_width * available_height
area_per_box = area / no_of_boxes
length = sqrt(area_per_box)
fill grid with this obtained length as width and height of small boxes.
Another approach would be to fill the grid as you like and scale the grid to your desired dimension.
I had posted the same question in oracle javafx forum too but haven't got a response. So trying my luck here.
I have a requirement where in the content of the text area is dynamically populated from the database. I am able to successfully retrieve and display the data on the text area.
However when the content is too large, I am not able to dynamically set the height of the text area. When I try to display the same as a label, the display is flawless, dynamically sets the height as per the content. So, I tried to create a label, with same content and dynamically bind the height to the preferred height as below, but it doesn't work.
// Generate User Note Description
TextArea textArea = new TextArea();
Label text = new Label();
// SETTING THE TEXT TO A LABEL TO RETRIEVE THE HEIGHT
text.setText(usrNotes.getNote().trim());
// ALWAYS DISPLAYS 0.0
System.out.println("height::"+text.getHeight());
if (isMyNote) {
// ALWAYS SETS TO THE MINIMUM HEIGHT OF 60.0
textArea.setText(usrNotes.getNote().trim());
textArea.setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth() - 500.0);
textArea.setWrapText(true);
textArea.setMinHeight(60.0);
// WITHOUT THIS BINDING, DISPLAYS LOT OF EXTRA SPACE AFTER THE TEXT
textArea.prefHeightProperty().bind(text.heightProperty());
textArea.setStyle("-fx-padding:0 5 2 1; -fx-font-size: 1.1em;-fx-background-color:white");
} else if (!isMyNote) {
// THIS IS PERFECT, AS EXPECTED SETS THE HEIGHT DYNAMICALLY
text.setText(usrNotes.getNote().trim());
text.setStyle("-fx-padding:0 5 2 1;");
text.setStyle("-fx-border-color: white;-fx-font-size: 1.1em;-fx-background-color:#F5F5F5;");
text.setWrapText(true);
text.setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth() - 500.0);
text.setMinHeight(60.0);
}
I would highly appreciate if someone can provide a hint on how to resolve this issue.
Thanks -SV
The reason text.getHeight() returns 0, (and therefore have to bind to text.heightProperty()) is because the height isn't calculated when the component is constructed. It is calculated when the component is rendered to the screen.
If you want to calculate a height ahead of time, I believe you will have to use something like FontMetrics (http://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html) to calculate the width and line height of your string, break up the string into tokens to figure out where line breaks will fall (based on your width), and then figure out how many lines you will need (and therefore, how high your TextArea needs to be).
I'm trying to set the height of a view in my application to a fraction of the total height of the user's screen. However, I can never seem to get it to quite work. Other solutions I've looked at for getting the height of the screen in pixels give a good approximate position, but the view position and height seem to vary from screen to screen.
How do I go about this?
Thanks for the help
to get the screen dimensions you can use:
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
You will want your view to preserve its aspect ratio, when resizing it, regardless of the screen size.
If you have a view, when it looks ok is 400 px wide and 200 px high, you will want to preserve this ratio (400/200) = 2 after you rescale the layout.
Lets say the screen is 480 w x 800 h and you want your view to be 50% of the width of the screen.
Then 480 * 50% = 240 will be target width of your view.
But we want to preserve aspect ratio:
400/200 = 2 = target width / target heigh = 240 / X
then X = 240*200/400 = 120
Your new Layout for the view should be 240w x 120 h (240 / 120 = 2 -aspect preserved-)
Here, as Android devices have different sizes in terms of aspect ratio (width / height), you may want to check how the new hight fits within the screen hight.
120 / 800 = 15% in this case.
If the hight % doesn't meet what you need, or if the hight happens to be out of bounds of the screen, or if the new hight doesn't meet a minimun height you want, then, you do the inverse calculation by setting a height and calculating the new width allways respecting the aspect ratio.
In this way the view won't deform on any screen.
well i think you might already have tried this trick below.
get width and height in pixels by using displayMetrics.widthPixels and displayMetrics.heightPixels respectively
then (assuming you have relative layout, whose position you want to set) set params as-
RelativeLayout.LayoutParams layoutParam;
set margin as below
//margin (left, top, right, bottom) in pixel CO-Ordinates
layoutParam.setMargins(screenWidth/108+screenWidth*seekTime/29, screenHeight/13, (screenWidth-screenWidth*seekTime/29)+screenWidth/108, (int) (screenHeight/0.9));
here the numbers in denominator (108, 29, 13, 0.9), i got from trial and error method, that suited my project best
so basic idea is to set it at a fraction of screen height in pixel.
for my project it worked for all the devices we tested on an also we had orientation locked to 'portrait'. might not work properly if screen orientation is changed
Another way might be, if you would like go after it...
Make margins a function of screen width.
eg:
if(height>4){
set at half height
}
else{
set at one-third height
}
(and i do hope that you do call display matrix only once in your whole app and store, say at launch of first activity, and just manipulate the margin from the stored place)
I am implementing PdfPageEventHelper event and footer stuff is as below:
ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase(String.format(" %d ",
writer.getPageNumber()),footerFont),
document.right() - 2 , document.bottom() - 20, 0);
Now, i have 3 lines which needs to be added into footer but i don't find a best to set its vertical margin. (Each 3 LINES has different font SIZE).
what should keep for - document.bottom() - XXX ??
The difference between two lines is the leading. You can pick your own leading, but it is custom to use 1.5 times the font size. You are drawing line by line yourself, using different font sizes, so you'll have to adjust the Y value based on that font size. Note that ColumnText.showTextAligned() uses the Y value as the baseline of the text you're adding, so if you have some text with font size of 12pt, you'd need to take into account a leading of 18pt. If you have a font size 8pt, you make sure you have 12pt.
That's the easy solution: based on "convention". If you really want to know how much horizontal space some specific takes, you need to calculate the ascender and the descender, as is done in figure 3.7 of my book. You'll find the code here. If bf is your font (a BaseFont object), text is your text (a String) and size is your font size (a float), then the height of your text is equal to height:
float aboveBaseline = bf.getAscentPoint(text, size);
float underBaseline = bf.getDescentPoint(text, size);
float height = aboveBaseline - underBaseline;
When y is the Y-coordinate used in showTextAligned() make sure you keep the space between y + aboveBaseline and y + underBaseline free. This is the accurate solution.
Note that document.bottom() - 20 looks somewhat strange. I would expect document.bottom() + 20 as the Y-axis of the PDF coordinate system points upwards, not downwards.
From an xml file, I'm given a width, height and id. All of them can and do vary very quickly. Now, I'm asked to draw a rectangle using the width and height (an easy task), and place the id at its center. The id must not overflow out of the rectangle it's contained it.
For single-character strings, this is also easy - set the font size to the height, play a bit with the x position maybe, and it's centered. The problem is when it's multi-character strings.
So given a width and height and a string, how can you determine what font-size the string should appear in? Assume you have every bit of information you need on the rectangle you're drawing the string in.
[Edit]: I'm using the Graphics 2D class to draw everything.
Start with selecting a Font at your preferred (i.e. maximum) size.
Grab the FontRenderContext from your Graphics2D object using getFontRenderContext.
Use getStringBounds() on the Font to be rendered to get a Rectangle2D object for the specific String to be rendered. That object describes the final size of the String using that Font
Check if the size specified by that Rectangle2D is small enough.
4a. If it is small enough, you're done. Use the last Font you've checked.
4b. If it is too big, use Font.derive() to produce a smaller version of the Font and continue to use that and loop back to 3.
Don't quite have the time to give you a full working example, but here are a couple pointers that should get you going in the right direction. The graphics object you are using to draw with has a getFontMetrics() method, one of the methods on FontMetrics is stringWidth(String str) which gives you the width of a string in the current Font.
If the width is too big for your rectangle set the Font on the Graphics object to the same font just with a smaller size until it fits.
To horizontally center a string in a container (learned long ago in typing class in high school):
(rectangleWidth / 2) - (stringWidth / 2)
http://download.oracle.com/javase/1.5.0/docs/api/java/awt/FontMetrics.html
To create a Font with a smaller size, something like:
Font font = graphics.getFont();
Font smallerFont = font.derive(font.getSize() - 1);
graphics.setFont(smallerFont);
Hope this gets you going in the right direction.
I would recommend for this problem to remove as many unknowns as possible. In this case, the problem chiefly is that font characters can vary in width... well most. That's why I would use a good monospace font like courier new for the ID, that way you know what the width of each character is, you know the width of your rectangle and you know the number of characters in your string. You can simply reduce the pixel size of each character will till your string fits the available width.
Example, if the width of each character is 12px and you have 10 characters in your ID, then you need 120px to fit everything in. If you only have 80px available, it's simple math 80/10 = 8px font-size (reduce half a pixel for padding if you want.
Just my suggestion.