 |
 |
New to Obj-C
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Murray, Utah, USA
Status:
Offline
|
|
Hello, everyone!
I'm a very experienced C++ programmer, but new to Cocoa and Obj-C. I love what I see so far, just have some problems I'm hoping someone can help me with
1) When declaring a method, what is the difference between: ?
- (void)SomeMethod;
+ (void)SomeMethod;
2) I can't seem to create RGB colors with NSColor. Do I use colorWithDeviceRed? or colorWithCalibratedRed?
3) My window won't refresh. If I have a custom NSView in which I do the following code:
- (void)mouseUp  id)sender {
[[NSColor greenColor] set];
NSRectFill(NSMakeRect(0,0,100,100));
[[self window] flushWindow];
}
After clicking the mouse the box draws just fine, but if I minimize to the dock then restore it the box is gone. And clicking will not redraw the rectangle. What is wrong?
Thanks! I'm sure these are simple problems...
Jeff
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Feb 2001
Status:
Offline
|
|
1) - is for instance methods, + for class methods (called "static" in C++ and Java)
2) I had problems making colors, too. colorWithDeviceRed ended up working, though.
3) Don't know about this. I think you need to lock focus. Here's an example with double buffering that works:
Code:
[bufferImage lockFocus];
[bufferImage compositeToPoint:NSMakePoint(0,0) operation:NSCompositeClear]; // clear buffer image
[[NSColor greenColor] set];
NSRectFill(NSMakeRect(0,0,100,100));
[bufferImage unlockFocus];
tempImage = bufferImage;
bufferImage = displayImage;
displayImage = tempImage;
[myWindow disableFlushWindow];
[myWindow setImage:displayImage];
[myWindow display];
[myWindow enableFlushWindow];
[myWindow flushWindow];
|
|
The 4 o'clock train will be a bus.
It will depart at 20 minutes to 5.
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
2) I can't seem to create RGB colors with NSColor. Do I use colorWithDeviceRed? or colorWithCalibratedRed?
"Device" colors are are colors which will always use those values, on any device. "Calibrated" colors might attempt color matching. Typically latter is the right thing to use, although on the screen these two might always be the same. Use the first if for some reason you absolutely need those RGB values.
I don't understand what problems you had with creating NSColors --- what wasn't working?
3) My window won't refresh. If I have a custom NSView in which I do the following code:
- (void)mouseUp id)sender {
[[NSColor greenColor] set];
NSRectFill(NSMakeRect(0,0,100,100));
[[self window] flushWindow];
}
After clicking the mouse the box draws just fine, but if I minimize to the dock then restore it the box is gone. And clicking will not redraw the rectangle. What is wrong?
Normally all your drawing should be done in the drawRect: method, which will automatically be called anytime drawing or printing needs to happen. In your case, it would look like:
- (void)drawRect  NSRect)rect {
[[NSColor greenColor] set];
NSRectFill(NSMakeRect(0,0,100,100));
}
Then, to indicate that the view needs to be redrawn (say because you modified some state), simply call [view setNeedsDisplay:YES]. This will mark that the view is dirty, and eventually call drawRect:. You would do this in the mouseUp: method for instance, after changing some state...
Note that if you want to draw your view right then and there, don't call drawRect: directly, instead call display, which sets up the drawing state then calls drawRect:.
The "DotView" example on the system is a good simple example of this.
Ali
[This message has been edited by ali (edited 04-26-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Murray, Utah, USA
Status:
Offline
|
|
Originally posted by ali:
"Device" colors are are colors which will always use those values, on any device. "Calibrated" colors might attempt color matching. Typically latter is the right thing to use, although on the screen these two might always be the same. Use the first if for some reason you absolutely need those RGB values.
I don't understand what problems you had with creating NSColors --- what wasn't working?
Thanks, that explains a lot  I think the problem I was having (now that I look at other examples was a stupid one...
[[NSColor colorWithDeviceRed:0.1:0.8:0.1:1.0] set];
instead of
[[NSColor colorWithDeviceRed:0.1 green:0.8 blue:0.1 alpha:0.1] set];
or something like that (I'm not at my machine right now to test this). BTW, are colors 0.0-1.0 or 0-255?
Normally all your drawing should be done in the drawRect: method, which will automatically be called anytime drawing or printing needs to happen.
Okay, like OnDraw() event in VC++ (sorry to bring up Win32 here).
Then, to indicate that the view needs to be redrawn, simply call [view setNeedsDisplay:YES].
Thank you, this is exactly what I needed! It seems as though so many things are wrapped and other things aren't... My first attempt was subclassing NSQuickDrawView and getting qdPort and using toolbox calls (which worked, but was more tedious).
Is there a tutorial on using the NSOpenGLView? I assume (from what you posted) that I could just in drawRect put all my gl* statements? Example:
- (void)drawRect  NSRect)rect {
glClear
glBegin
...
glEnd
glutSwapBuffers
}
Would this work? or is there more to it?
Jeff
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
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
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|