Re: Drawing a String in Java
Re: Drawing a String in Java
- Subject: Re: Drawing a String in Java
- From: Joe <email@hidden>
- Date: Thu, 25 Oct 2001 22:31:21 -0500
Thanks for your help! This looks like it's exactly what I'm looking for.
No wonder I didn't find it. I think I may have breezed right past that
document, actually.
As far as the whole Objective-C vs. Java thing: I find Objective C to be
funny looking, but I understand that Cocoa is really designed to work with
Objective C, and using Java is kind of a big hack. But I have years of
Java experience, and huge quantities of Java code that I want to reuse,
not port to Objective C. I want to learn Objective C as soon as possible,
but right now, I've got to pay the bills and make my existing Java code
work.
And I think you are right: from my experience so far, Java Cocoa seems to
have a few shortcomings, but it's not that bad. Some people really dump on
it, calling it well-nigh unusable, but it works fine for me so far. The
docs are a little confusing from time to time, and there isn't much sample
code. I just keep the JavaBrowser app open, and constantly refer to it.
Thanks again for your help, I really do appreciate it.
-Joe
On Thursday, October 25, 2001, at 10:14 PM, email@hidden wrote:
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