String width in pixels
Hi All,
I'm making a small bar chart program that needs to print the bar values at the very end of the bar.
Is there a way to determine the width of a string in pixels given it's font and length in characters?
Basically, I'm trying to get the very last character of the value string to be right aligned with the bar itself without having it run off the edge or be too far towards the center of the bar.
Also, I don't suppose there is a way to print strings leftward instead of rightward?? No no, of course not, I'm just being silly ;) right?
Thanks for the help
Marcos
Anybody ever hear of StringBuffer???
Fair enough and yes, yes I have thank you -though I feel it is often better for the newbie to see the process manually and if they study this kinfd of thing then they may learn to work stuff out for themselves when they need to implement their own algorithm.
This - Snr whoIsFred - is what makes a programmer,
- knowing how to manipulate your data types to make things work
- it is not about knowing the API's off by heart (though that can help)
- knowing how to read and utilise the java API docs is the real key
I think what was posted therefore is moare beneficial than StringBuffer(aString).reverse()
Similar Messages
-
Hi
Is there anyway to determine width and height(in pixels or whatever its possible) a String is? I dont want the number of characters in it, I want the "width" and the "height" of some given String with a particular font name,style and size???
i tried using getStringWidth() in FontMetrics. its giving the width but its not considering the string value. i wanted to get the width for string width also considering the value of string.
can i use getStringBounds(String str, Graphics context)???
but what is this Graphics context??
i wanted to use it in a core java pgm.not using any awt or swing etc???
Does anyone know how to do this?
help me out plz??can i use getStringBounds(String str, Graphics context)???
but what is this Graphics context??The context is the graphics object to which you wish to draw this string. As strings sizes are done in a resolution independent format, the size in pixels changes depending on the resolution of the device. You would want a 12pt String to be about the same size if displayed either on 72dpi monitor, or a 300dpi printout. -
Determining the actual length of a string based on pixels?
How would you determine the actual length of a string based on pixels? Reason for is because a length of a string containing all " l " chars would be a lot smaller then the length of a string containing all "H" chars based on pixel width.
thanks,
newbieYes, look at the FontMetrics class which has methods to do just that. To get a relevant FontMetrics object, try "x.getFontMetrics(f)" where x is a Component and f is the Font your string is represented in.
-
Default column width in pixels? - Numbers app
I find the pixel count hard to see until i've moved my finger, does anyone know the default column width in pixels in the Numbers iOS app?
Thanks!Welcome to the Apple Community.
72 -
How do I get a strings width w/o getFontMetrics()?
Now that getFontMetrics() is deprecated - how am I supposed to get the width of a string? The FontMetrics class had a stringWidth() method.
I dont have the API docs here so I cant give u the exact method names and classes but heres generally how you can do it: You have the Graphics object for the screen/whatever that you want to calculate the bounds for so do this
Graphics2D g = ...
FontRenderContext frc = g.getFontRenderContext();
Font font = g.getFont();
Rectangle2D bounds = font.getStringBounds("foo", frc);
or something like that, and if you dont have the Graphics object you can always pass that part by calling the FontRenderContext constructor and passing some null values to it and so creating your own context that does some basic calculations correctly anyways.. Enuff for any font bounds anyway. -
How do I find the current browser window width in pixels?
I'm writing a PHP script that will use the current browser
window width
and height, but I don't know how to find that. Is there some
way to
capture the width and height of the current window in pixels?
TIAThanks Micha, you certainly may ask why.
I posted a message some time ago in the appdev forum about
the Recordset
Paging server behavior being geared toward use with tables. I
was
frustrated because I wanted to do something with repeated
div's in a
liquid site. But the Recordset Paging s.b. locks the number
of records
per page to a fixed number. I'm trying to figure out how to
make the
records per page a variable that's determined by the window
width. I've
created a liquid site with a products page that contains both
text and
thumbnail product images. The text occupies 2/3 of the width
of the
window, and the div container that holds the floated div
thumbnails I
want to account for the other third. The thumbnail div's are
produced by
a WHILE statement and I want them to be paged based on the
number of
div's that will fit within a container that occupies a
percent of the
browser window width. Here's what I've got so:
$window_size_wide = ; // Current browser window width in px
$window_size_tall = ; // Current browser window height in px
$thumb_cont_wide = $window_size_wide * .3; // 30% of window
width incl.
padding in px
$thumb_cont_tall = $window_size_tall * .4; // 40% of window
height incl.
padding in px
$thumb_size_wide = 100; // thumbnail div width incl. margins
in px
$thumb_size_tall = 100; // thumbnail div height incl. margins
in px
$no_thumbs_wide =
ceil(($thumb_cont_wide/$thumb_size_wide)-1);
$no_thumbs_tall =
ceil(($thumb_cont_tall/$thumb_size_tall)-1);
$records_per_page = ($no_thumbs_wide * $no_thumbs_tall);
$offset = ($thispage - 1) * $records_per_page;
$total_pages = ceil($total_records/$records_per_page);
etc.
What I don't know is how to find the current window width or
height. I
would also like to know if the $thumb_cont_wide and the
$thumb_cont_tall
can be determined in some way based on the div size generated
through my
CSS. In other words, I don't want to declare the percent of
height and
width in this script, I want to declare it only in my CSS and
have it
determined by the browser window width and height. The only
element I
want to hard code is the size of the thumbnails as they will
always be
the same. It seems to me that something like this could be
done to make
recordset paging scalable to match a scalable web page.
So the question is, can I define a PHP variable based on a
Javascript
variable? If so, how? If not, how can I accomplish this? Is
this just
not possible?
Thanks for your help. -
Illustrator has the 'Transform Each' dialog, but it only allows you to scale multiple objects by specifying a percentage. Is there any way to specify the size in pixels?
More specifically, imagine I have 10 rectangles on my canvas, and I want them all to be of a specific height. The rectangle position on the canvas matters, so I can't do the 'vertical align to top and then resize group height' trick - they need to be resized in place.@Mike: Thank you very much for the quick reply.
Your suggestion does help, but there is still a gap. To be more specific, my rectangles are actually a bar chart, so the heigh of the ractangles is different and should remain unchanged. What I want to do is make each bar narrower to a specific pixel width. The Shape Options feature allows you to specify absolute dimensions, but it seems like you have to specify this for both the Width and Height - can't just do one unless I'm missing something. You can specify relative increase/decrease in pixels (which mostly solves my problem), but I have to calculate the pixel difference, usually fine unless I'm dealing with decimal pixels which I'd like to clean up and turn to clean pixels.
A bit confused about your last point. What does 'select individually' mean? Is Ctrl Enter do anything different from just Enter? -
How to find string length in pixels
Hi,
I have a string "lucida bright regular". How can I get size of text in pixels.
Can java.awt.Font work.
An example wil really help.
Thanks
with regardsNot exactly.
I am applying font on text at web server using java
and sending the resulting html to client end where
is is displayed.
Client can not change the
font of that text.
In that case you don't know the capabilities of modern browsers...
Just because you want something bad enough doesn't make it happen.
This is NOT Oz, this is the real world (or what passes for it for lack of alternatives). -
I've had my black Macbook since late 2007.
Recently a string of pixels about 1/4 of the screen in from the left hand side (at about F3) has become stuck on white. The machine continues to function fine, but as you might imagine it's terribly inconvenient for movies and the like.
I've tried various "pixel unsticker" programs to no avail.
Any suggestions?The way to do your request...
Duplicate the clip on an upper track. Crop it down to that one pixel line just to the right or left of your troubled area and then shift it over one pixel. If you have image+wireframe using your arrow key will move it one pixel.
But why isn't it on ALL the clips? Or is it?
CaptM -
Hallo, I'd like to know how to compute
pixel length of a string without any context using Java 1.3.
In Java 1.2 I used Toolkit, but not more supported.
Who can help me?
Thanx.Look at the FontMetrics
-
Photoshop transform height or width as pixels instead of percentage
Hello,
As the title suggests I'm wondering if it's possible in Photoshop to transform an object and input the height or width of it as pixels instead of a percentage?
Thanks.I feel really stupid now, I was looking in the preferences for the option to change it, thanks for pointing me in the right direction anyway
-
Hello all,
Is it possible to get the width of a String? By this I mean that actual space that the String takes up not .length().
The reason I need this is because I have a JPS that populates an HTML menu with 2 pieces of text on each row. To align the second piece of text properly I need to find out the width of the first piece and apply the neccessary spacing.
Any ideas?Well, you can try and work with Font and FontRenderContext ...
But you should consider the fact, that you can't really control which font a user specifies in its browser preferences. Therefore it is usually preferable to use HTML elements to do the spacing for you (e.g. with tables). -
Is it possible to calculate the width of a String in a certain font without needing a Graphics object? I need to calculate this before a component is displayed in order to adjust the sizes of the components. getGraphics() is returning null at this point.
Didn't my original post answer that question?
My control is being wrapped in a JScrollPane. I need to set the preferred size of the scroll pane based on the width of a String. If I try to do that during initialization before it is displayed, getGraphics() returns null so I get a NullPointerException. -
Width / Height pixel information on images in finder no longer there?
I rely heavily on this feature for my job, and it seems to have stopped working! When finder is in the 'multi-column' mode, when clicking on an image, amongst the information shown to the right of it (including name, kind, size etc) used to be the pixel dimensions.
This is really annoying, anyone have any clues?Jeffrey Jones2 wrote:
Metadata is exactly what Spotlight indexes.
I understand that and agree with it.
Metadata is not in the file itself
I disagree with that.
It is data about the file
I agree with that.
but not in the file.
Once again, I disagree.
Some metadata, such as the file's name, its size, its creation date, are in the directory.
I agree.
Most of the rest is added by Spotlight.
According to your view of things, where does Spotlight get metadata for a file that's not in a directory? For the most part, it has to get it from inside the file.
My photo files contain a lot of metadata that describe the photo itself, such as camera model. I know this because I can see them in the file with Terminal commands.
There are other mentions of metadata in files on the Web. For an example, check this Web page:
http://downloads.zdnet.com/abstract.aspx?docid=980405
The page contains this quote: "Metadata Hootenanny is a viewer/editor for all the spiffy metadata you can put into Quicktime movies. You know how you can view, edit and sort by your mp3s' ID3 metadata in iTunes? Well, the same metadata system exists for all your quicktime movies, too..." -
Length of a string (text) in pixels.
I'm trying to locate a String's center on an exact position, drawing it to a JPanel. But since i don't know the exact length of the string, it's not that easy. Can anybody help me out there?
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class CenteringStrings
public static void main(String[] args)
StringDemoPanel demo = new StringDemoPanel();
DemoActionPanel action = new DemoActionPanel(demo);
JFrame f = new JFrame("Centering Strings");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(action, "East");
f.getContentPane().add(demo);
f.setSize(500,300);
f.setLocation(200,200);
f.setVisible(true);
class StringDemoPanel extends JPanel
String text;
Font font;
final int
ASCENT = 0,
DESCENT = 1,
LEADING = 2,
BASELINE = 3;
int index = -1;
boolean sizeFlag, boundsFlag;
public StringDemoPanel()
text = "Centered text";
font = new Font("lucida sans oblique", Font.PLAIN, 36);
sizeFlag = false;
boundsFlag = false;
setBackground(Color.white);
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics(text, frc);
TextLayout tl = new TextLayout(text, font, frc);
int w = getWidth();
int h = getHeight();
float textWidth = (float)font.getStringBounds(text, frc).getWidth();
float ascent = lm.getAscent();
float descent = lm.getDescent();
float leading = lm.getLeading();
float height = lm.getHeight();
float x = (w - textWidth)/2;
float y = (h + height)/2 - descent;
g2.drawString(text, x, y);
// marker for string origin (x,y) on baseline
//g2.setPaint(Color.blue);
//g2.fill(new Ellipse2D.Double(x - 1, y - 1, 2, 2));
// physical space of text
Rectangle2D textSize = tl.getBounds();
textSize.setFrame(x, y - textSize.getHeight(),
textSize.getWidth(), textSize.getHeight());
// string bounds
Rectangle2D bounds = font.getStringBounds(text, frc);
bounds.setFrame(x, y - ascent - leading, bounds.getWidth(), bounds.getHeight());
switch(index)
case ASCENT:
Rectangle2D r1 = new Rectangle2D.Double(x, y - ascent, textWidth, ascent);
g2.setPaint(Color.magenta);
g2.draw(r1);
break;
case DESCENT:
Rectangle2D r2 = new Rectangle2D.Double(x , y, textWidth, descent);
g2.setPaint(Color.orange);
g2.draw(r2);
break;
case LEADING:
Rectangle2D r3 = new Rectangle2D.Double(x, y - ascent - leading,
textWidth, leading);
g2.setPaint(Color.yellow);
g2.draw(r3);
break;
case BASELINE:
Line2D line = new Line2D.Double(x, y, x + textWidth, y);
g2.setPaint(Color.blue);
g2.draw(line);
if(sizeFlag)
g2.setPaint(Color.green);
g2.draw(textSize);
System.out.println("above size = " + (int)textSize.getY() + "\n" +
"below size = " +
(int)(h - (textSize.getY() + textSize.getHeight())));
if(boundsFlag)
g2.setPaint(Color.red);
g2.draw(bounds);
System.out.println("above bounds = " + (int)bounds.getY() + "\n" +
"below bounds = " +
(int)(h - (bounds.getY() + bounds.getHeight())));
public void setIndex(int i)
index = i;
repaint();
public void setSizeFlag(boolean flag)
sizeFlag = flag;
repaint();
public void setBoundsFlag(boolean flag)
boundsFlag = flag;
repaint();
class DemoActionPanel extends JPanel
StringDemoPanel sdp;
JRadioButton
ascentButton, descentButton, leadingButton, baselineButton, clearButton;
JRadioButton[] buttons;
JCheckBox
sizeCheck, boundsCheck;
JCheckBox[] checks;
public DemoActionPanel(StringDemoPanel p)
sdp = p;
createComponents();
private void createComponents()
ascentButton = new JRadioButton("ascent");
descentButton = new JRadioButton("descent");
leadingButton = new JRadioButton("leading");
baselineButton = new JRadioButton("baseline");
clearButton = new JRadioButton("clear");
buttons = new JRadioButton[] {
ascentButton, descentButton, leadingButton, baselineButton, clearButton
ButtonGroup group = new ButtonGroup();
RadioButtonListener buttonListener = new RadioButtonListener();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1.0;
gbc.insets = new Insets(2,2,2,2);
gbc.gridwidth = gbc.REMAINDER;
gbc.anchor = gbc.WEST;
for(int i = 0; i < buttons.length; i++)
add(buttons, gbc);
group.add(buttons[i]);
buttons[i].addActionListener(buttonListener);
sizeCheck = new JCheckBox("text size");
boundsCheck = new JCheckBox("bounds");
checks = new JCheckBox[] { sizeCheck, boundsCheck };
CheckBoxListener checkBoxListener = new CheckBoxListener();
for(int i = 0; i < checks.length; i++)
add(checks[i], gbc);
checks[i].addActionListener(checkBoxListener);
private class RadioButtonListener implements ActionListener
public void actionPerformed(ActionEvent e)
JRadioButton button = (JRadioButton)e.getSource();
if(button == clearButton)
sdp.setIndex(-1);
return;
for(int i = 0; i < buttons.length; i++)
if(button == buttons[i])
sdp.setIndex(i);
break;
private class CheckBoxListener implements ActionListener
public void actionPerformed(ActionEvent e)
JCheckBox checkBox = (JCheckBox)e.getSource();
if(checkBox == sizeCheck)
sdp.setSizeFlag(sizeCheck.isSelected());
if(checkBox == boundsCheck)
sdp.setBoundsFlag(boundsCheck.isSelected());
Maybe you are looking for
-
How to get rid of bugs in MBP?
Greetings all, My daughter's boyfriend has a 2012 MacBook Pro with a bug (yes, real insects) infestation. They live in a tropical climate and the insects are more adaptable than they are in, like, California. Anyway, the insects have gotten to the po
-
Message "Accessory connected" when connecting to my car audio
Hi, I bought a new iPod Touch recently but haven't tried connecting it to my car audio through the car's USB interface. When I tried connecting it to my car audio, the iPod interface vanished and a screen with "Accessory connected" appeared and it is
-
Mouse for my MacBook Pro.
Hi I have a MacBook Pro and I want to buy a mouse for it. I've looked at the Apple Magic Mouse but it's a little expensive and also has quite a few bad reviews. I saw a HP mouse in PC World but they told me it wasn't compatible with a Mac. Has anyone
-
Hi, Is there any report of PO header details which gives the below details? 1. PO 2. PO creator(Planner) 3. Buyer 4. Customer Data in header details ( In our organisation we put email ID which should be available in the report). Regards, Rajaneesh
-
FW CS5 Tool Panels Not Showing
Hi, When I load Adobe Fireworks CS5 it starts error-free and I can load and work on documents no problem. When I tab across to the "Align" panel or the "Color Palette" or try to use the "Auto Vector Mask" function, it comes up with a blank panel. If