Can I (expand || shrink) the Text only vertically or horizontally using drawText

[b]Hi all,

I have an applet where I draw text in the middle of a rectangle.

The text should not go out of the rectangle bounds

[color=blue]is there a way to expand text size vertically or horizontally only ?

because increasing the font size will increase both side which might result in the text going out of one of the Rectangles side.

I thought of displaying text as images and change its width or height, but I am sure this is not the best solution

[/color][/b]

yes, apply a scaling transform to the graphics context onto which you are rendering.

@see java.awt.geom.AffineTransform

thanks Abuse

but when i apply a scaling transform to the graphics context , whouldn’t that affact also other shapes like Rectangles?

i only want expand the text not the rectangle.

i hope i’v understood what u’v said.

if not, please put a simple example .

  1. store current transform - @see java.awt.Graphics2D#getTransform())
  2. create appropriate scaling transform.
  3. set the new transform - @see java.awt.Graphics2D#setTransform(Transform)
  4. draw the text
  5. reset the transform that you stored in 1)

AffineTransform at = new AffineTransform();
at.scale(double sx,double sy)

what should provide as parameter for this method? so that the text will be displayed with (hight > old Hight)

if there is a good site explaining how to use this class , please share it.

So long as you understand the use of matrices in mathematics, the javadoc should give adequate explanation on it’s use.

http://java.sun.com/javase/6/docs/api/java/awt/geom/AffineTransform.html

i have used scale

but i think what it does is “zooming”

that is now what i want !

i want to change the hight and width of text without any zooming !

check snapshoot before useing scale and after scale !

i am not getting the result i want.

the text getting out of the Rectangle !

Hi, you can compose your scaling with a translation to keep the same top-left point can’t you?
Hope it helps…

turquoise3232

can you please , provide a simple example ?

i did not get your point.

thanks guys for the Help

i found what i exactly want

check it here

http://java.sun.com/j2se/1.5.0/docs/guide/2d/spec/j2d-fonts.html#wp74065

u can apply scale on a font object and get your own derived font

:slight_smile: