Re: Drawing a String in Java
Re: Drawing a String in Java
- Subject: Re: Drawing a String in Java
- From: Andrew Platzer <email@hidden>
- Date: Fri, 26 Oct 2001 11:28:16 -0700
On Thursday, October 25, 2001, at 05:02 , Joe wrote:
I am trying to draw a simple text string, using Java, and finding it
not-too-obvious. I just want to apply text to the screen -- not within a
text view.
Examining sample code, I notice that "SonOfSillyBalls" draws text with a
"drawToPoint" method in NSString. Unfortunately, Cocoa Java has no such
method (or NSString class, for that matter). How do I perform this
seemingly basic and simple task from within Java?
NSString is just bridged to a regular Java String. The problem is that you
can't add methods such as drawAtPoint to String. Instead, you need to use
NSAttributedString and use one of the two methods in
com.apple.cocoa.application.NSGraphics:
public static void
drawAttributedString (
com.apple.cocoa.foundation.NSAttributedString,
com.apple.cocoa.foundation.NSPoint )
public static void
drawAttributedString (
com.apple.cocoa.foundation.NSAttributedString,
com.apple.cocoa.foundation.NSRect )
For example (and my apologies if this is wwrong as my Java is a bit rusty)
if you wanted to draw a string in the default font but in red:
void DrawRedText(String str, NSPoint point)
{
NSDictionary attr = new NSDictionary(NSColor.redColor(),
ForegroundColorAttributeName);
NSAttributedString attrString = new NSAttributedString(string, attr);
NSGraphics.drawAttributedString(attrString, point);
}
Andrew
__________________________________________________________________
A n d r e w P l a t z e r
A p p l i c a t i o n F r a m e w o r k s
A p p l e