 |
 |
Java, Cocoa, Drawing: One confused novi programmer
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Just for reference, I am some what of a novice to coding (not counting that stuff I did in ProDOs when I was 8):
So here I am, thinking that graphical/moving programs are little more exciting than static ones that just sort of display numbers. But I'm having trouble figuring out how to do that kind of thing. All I really need to know is: how can I make a NS* graphical display window thang (I have no idea) and then draw in it. You know, connect two points (103, 5) (102, 100) with a line color red, or just make a pixel change color. Apple's cocoa documentation needs some serious finishing up. I find it hard to find anything. Half their docs aren't even written yet!
Thanks in advance,
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Dec 2000
Status:
Offline
|
|
I really think the example GL programs are a good idea.
/Developer/Examples/GLUTExamples/
Look at all the stuff in there. Most of it's 3d, but gameglut should be what you want: a window with vector-style graphics being drawn in it.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Dec 2000
Status:
Offline
|
|
Sorry, I'm not sure I read your request right... The GLUT stuff is super-easy drawing commands, but it handles all the windowing itself. If you want to control the window, you'll have to do some mix and match stuff... Perhaps someone else can explain how you integrate GL calls into a Cocoa app...?
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Code:
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class test {
NSView test ;
NSPoint point1 = new NSPoint(200, 100);
NSPoint point2 = new NSPoint(250, 150);
test.lineToPoint(point1);
test.lineToPoint(point2);
...
Okay, so this looks all good to me. But I keep getting:
Code:
xRPN.java:11: Identifier expected.
test.lineToPoint(point1);
^
1 error
BuildFilter: inferior command exited with code 1
Does anyone know why this is?
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
You do not need to use GL to do animations, although GL will make it possible to get the best and most efficient animations. If you're just starting out, definitely start off with basic Cocoa stuff, without GL...
You usually subclass NSView to do custom drawing, event handling, etc. You implement the drawRect: method to do the drawing. You can use classes such as NSBezierPath, NSImage, NSColor to do the actual drawing in drawRect:. You can also implement methods such as mouseDown: to handle events.
Below is a very simple subclass of NSView demonstrating some of this. To use it, create a new Cocoa Application in ProjectBuilder; add two files (BouncingDot.m and BouncingDot.h) with the below contents. This way you'll have created a subclass of NSView named BouncingDot.
You now have to tell IB about this so you can use it in your UI. Open the nib file by double-clicking on MainMenu.nib. Go to "Classes" pane, select "NSView", do Classes->Subclass and rename the subclass as "BouncingDot". Now drag a CustomView from the palette to your window. Double-click on it to bring up the class inspector, and change the class of the custom view to "BouncingDot." Save, build, and run... Clicking in the view should get the animation to start/stop.
Note that this code is very simple minded and does very inefficient animation. However, it should get you going!
Ali
Code:
/* BouncingDot.h */
#import <AppKit/AppKit.h>
@interface BouncingDot : NSView {
NSSize vel; // velocify of dot, in pixels
NSRect dotRect; // location and size of dot
NSTimer *timer; // timer; if not-nil, we're animating
}
// (should declare methods here, but can be lazy for now)
@end
/* BouncingDot.m */
#import "BouncingDot.h"
@implementation BouncingDot
#define VEL 10.0
// Initialize the super (NSView); also initialize the location and speed of the dot
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
dotRect = NSMakeRect(0, 0, 50, 50);
vel.width = vel.height = VEL;
}
return self;
}
// This is called when deallocating the view.
// If the timer is still active, stop it...
- (void)dealloc {
if (timer) {
[self toggle:nil];
}
[super dealloc];
}
// This method redraws the whole view at its current state
// Note that it does not change any state! It can even be called when printing...
- (void)drawRect:(NSRect)rect {
// Clear the whole view to white
[[NSColor whiteColor] set];
NSRectFill([self bounds]); // Equiv to [[NSBezierPath bezierPathWithRect:[self bounds]] fill]
// And draw a red dot
[[NSColor redColor] set];
[[NSBezierPath bezierPathWithOvalInRect:dotRect] fill];
}
// This method is called periodically to update the location of the dot. It also indicates that the view needs to be redisplayed.
- (void)step:(NSTimer *)timer {
NSRect bounds = [self bounds];
dotRect.origin.x += vel.width;
dotRect.origin.y += vel.height;
if (NSMinX(dotRect) < NSMinX(bounds)) { dotRect.origin.x = NSMinX(bounds); vel.width = VEL; }
if (NSMinY(dotRect) < NSMinY(bounds)) { dotRect.origin.y = NSMinY(bounds); vel.height =VEL; }
if (NSMaxX(dotRect) > NSMaxX(bounds)) { dotRect.origin.x = NSMaxX(bounds) - NSWidth(dotRect); vel.width = -VEL; }
if (NSMaxY(dotRect) > NSMaxY(bounds)) { dotRect.origin.y = NSMaxY(bounds) - NSHeight(dotRect); vel.height = -VEL; }
[self setNeedsDisplay:YES];
}
// This can be connected to a button or some other widget; it will stop/start the dot each time invoked. Animation is set to occur at 20 frames/second (0.05). The method "step:" is called.
- (void)toggle:(id)sender {
if (timer == nil) {
timer = [[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(step:) userInfo:nil repeats:YES] retain];
} else {
[timer invalidate];
[timer release];
timer = nil;
}
}
// Clicking in the view will cause animation to start/stop
- (void)mouseDown:(NSEvent *)event {
[self toggle:nil];
}
@end
[This message has been edited by ali (edited 02-15-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Dec 2000
Location: Rehoboth Beach,DE USA
Status:
Offline
|
|
I have a related question,I would like to know where I can find a tutorial on basic raster graphics in Objective C.The first thing I would like to do is load a graphic,say a JPEG and draw it to a view,and then try more complex things after that.Any advice anyone can give would be appreciated.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Use NSImage or NSImageRep to load and draw pictures in views. It's easy. If you use NSImage I know that you can have transparency in the image. Using NSImageRep you should be able to have transparency also but I could never get it to work even when I set hasAlpha to YES and isOpaque to NO. I don't know why it doesn't seem to work.
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
Check out my site at
http://gilgalad.dyndns.org
When I boot into OS X.
There's an example of how to use an NSOpenGLView within a window.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Okay, all th above is looking like good stuff (although I'm going to have to translate Obj-C to java). But, u see my above code (my 2nd post, I think)? Does anyone see what's wrong with it. I mean, I make a new NSView object. I make two NSPoint objects, and then I use an NSView instance method on the NSView object using the NSPoint objects... which is what the Apple Kit said is the correct method construction... but why the error messgae?
Thanks,
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
But, u see my above code (my 2nd post, I think)? Does anyone see what's wrong with it. I mean, I make a new NSView object. I make two NSPoint objects, and then I use an NSView instance method on the NSView object using the NSPoint objects... which is what the Apple Kit said is the correct method construction... but why the error messgae?
Well, as far as I know, NSView does not have a "lineToPoint()" method. Secondly, I don't see you creating an NSView, unless it's an outlet that is being bound in IB? Third, I don't see from the top of my head how these problems could cause the error message you're seeing.
I didn't realize you wanted Java examples. /Developer/Examples/Java/AppKit/BlastApp is a Cocoa/Java example showing animation and such. The Game class is a subclass of NSView which implements most of the drawing stuff. It has the same sort of timer, step(), and drawRect() methods that the above example has (and bunch more stuff).
Ali
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Originally posted by ali:
Well, as far as I know, NSView does not have a "lineToPoint()" method. Secondly, I don't see you creating an NSView, unless it's an outlet that is being bound in IB? Third, I don't see from the top of my head how these problems could cause the error message you're seeing.
I didn't realize you wanted Java examples. /Developer/Examples/Java/AppKit/BlastApp is a Cocoa/Java example showing animation and such. The Game class is a subclass of NSView which implements most of the drawing stuff. It has the same sort of timer, step(), and drawRect() methods that the above example has (and bunch more stuff).
Ali
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Sorry about that last post. I would hav eedited it, but I kept getting password errors.
Originally posted by ali:
Well, as far as I know, NSView does not have a "lineToPoint()" method.
Oh DUH! That methods in NSBezierPath. Okay, here's what my code looks like:
Code:
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class tester {
NSView test;
NSBezierPath path = new NSBezierPath();
NSPoint point1 = new NSPoint(200, 100);
NSPoint point2 = new NSPoint(250, 150);
path.moveToPoint(point1);
path.lineToPoint(point2);
NSRect hello = test.bounds();
test.drawRect(hello);
I'm not at home right now, but I'm predicting what my next problem is going to be: How do I get the NSBezierPath path to show up in my NSView test (if I'm understanding this right)
Thanks,
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Dec 2000
Location: Rehoboth Beach,DE USA
Status:
Offline
|
|
Thanks for the advice everyone.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Code:
NSView test;
NSBezierPath path = new NSBezierPath();
NSPoint point1 = new NSPoint(200, 100);
NSPoint point2 = new NSPoint(250, 150);
path.moveToPoint(point1);
path.lineToPoint(point2);
NSRect hello = test.bounds();
test.drawRect(hello);
You should not call drawRect(). You can call display(), which will do the right thing as far as setting up the drawing context, and eventually invoke drawRect(); or, better yet, call setNeedsDisplay(), which will cause the system to coalesce various drawing requests. The code snippet above does this --- see the step: method.
Another confusion about this code is where your NSView is initialized.
Ali
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status:
Offline
|
|
Rick1138:
I think you went down in the storm so to speak.
I'm a beginning java programmer and also have no clue as to proggramming java or objectiveC in osx. However on the ADC http://developer.apple.com I found a very good tuorial for setting up a basic java programme. Maybe there's something similar on objectiveC over there
------------------
#ex-dotcom#
|
|
weird wabbit
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Yea, they have an Objective C tutorial called Currency Converter.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|