Re: Drawing a String in Java
Re: Drawing a String in Java
- Subject: Re: Drawing a String in Java
- From: email@hidden
- Date: Thu, 25 Oct 2001 23:14:19 -0400
Joe,
It's in there. You just have to know where to look. I actually asked
this question when I got the Learning Cocoa book and tried to run the
Appendix Example where it draws a string to the screen. Nobody knew
then. But I know now!
The method you are looking for happens to be in the NSGraphics class in
the AppKit. I found out a little about this and some other interesting
Java/Cocoa facts at this URL:
http://developer.apple.com/techpubs/macosx/ReleaseNotes/JavaCocoaAPIs.html
It was hidden off of this one:
http://developer.apple.com/techpubs/macosx/ReleaseNotes/AppKit.html
This is similar to a sample I did a few days ago. Use this method as
the drawRect of your subclass of NSView. It should work, but I am
typing this into my mail.app so watch for typos and bugs...
public void drawRect(NSRect rect)
{
String string = "Rock On";
NSFont font = NSFont.systemFontOfSize(16);
NSRect myBounds = this.bounds();
NSMutableDictionary attrs = new NSMutableDictionary();
attrs.setObjectForKey(font,font.fontName());
NSAttributedString attrString = new
NSAttributedString(string,(NSDictionary)attrs);
// this is the method that draws the string in your subclass of
NSView....
NSGraphics.drawAttributedString(attrString,new
NSPoint((myBounds.width())/4,(float)25.0));
}
That's probably not the greatest of code but it should get you started...
As for using Java in Cocoa - it's not really THAT bad. The
documentation IS there if you look - there's just not as much. Most of
the Programming Topics listed here have some pointers to the Java
classes as well as the ObjC ones:
http://developer.apple.com/techpubs/macosx/Cocoa/CocoaTopics.html
If you have Java experience - and more importantly java code or java
examples that you want to re-use - it's the way to go. Objective-C
isn't that hard to learn - and there's some neat things in it - so I
encourage you to actually learn a little of that as well. BUT - Cocoa
is really just a great set of API's. Java Cocoa is good and gives you
everything you need to build your apps. Sure it could use some work,
but I am sure apple is working on making it better...
Good Luck,
Tyler
On Thursday, October 25, 2001, at 10:41 PM, cocoa-dev-
email@hidden wrote:
>
Hey all,
>
>
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?
>
>
Thanks,
>
>
Joe