Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dbrayford

macrumors member
Original poster
Feb 22, 2010
41
0
I've got a Java application that contains a panel within a frame and
I want to draw into the panel and the method works on Windows (with OS
specific modification). However, when I execute the program on a Mac it would appear that NSView *view = dsi_mac->cocoaViewRef; returns the parent window, which in this case is the frame.

Is there anyway I can modify the code below to ensure that I always
have the panel as my windows context rather the frame?

David


// Get the corresponding peer from the caller canvas
NSView *view = dsi_mac->cocoaViewRef;

NSWindow *window = [view window];
NSGraphicsContext *ctxt = [NSGraphicsContext
graphicsContextWithWindow:window];
CGContextRef cg = [ctxt graphicsPort];
 
What are you doing that requires drawing into the window directly? You should only draw into its content view. If you want some funky drawing behavior, make a borderless window and then your content view will draw into the entire window. Anything else is undocumented and should be avoided.
 
I have a JPanel within a JFrame and want to draw into the panel. However, the NSView *view = dsi_mac->cocoaViewRef; only returns the JFrame and draws into that.
 
I've examined the java object passed into Objective-C function and it is the panel I want to draw in however, cocoa decides to get the panels parent, which is the frame and draws into that.

It would appear that the call to the cocoaViewRef is returning the JPanels parent window, which is the JFrame.

Is there a way to specify that I don't want the parent window?
 
I've examined the java object passed into Objective-C function and it is the panel I want to draw in however, cocoa decides to get the panels parent, which is the frame and draws into that.

It would appear that the call to the cocoaViewRef is returning the JPanels parent window, which is the JFrame.

Is there a way to specify that I don't want the parent window?

What window do you want?

Are you using the term "window" in a Windows sense?

Do you mean "view" or "contained element" instead of "window"?

I'm guessing here, because it's unclear what you're trying to accomplish.

Your originally posted code shows getting an NSView for a component, and that NSView presumably corresponds to the JPanel. You then get an NSWindow that contains the NSView, and that presumably corresponds to the JFrame. If you want the NSView, then use the NSView. If you don't want the NSWindow, then don't use it.

NSView and NSWindow are distinct classes. If you're trying to do something that involves both the NSView and the NSWindow, then you'll have to use both objects, each one being used as appropriate.
 
Sorry about the confusion with the window and view classes, I am very new to cocoa and found an example and tried to modify it to do what I wanted as a learning exercise.

I've modified the code to just use the view but the rectangle isn't being drawn at all.

Code:
    dsi = ds->GetDrawingSurfaceInfo(ds);
    
    if (dsi != NULL) 
    {
        dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
        if (env->ExceptionOccurred()) 
	{
            env->ExceptionDescribe();
        }
        
        NSView *view = dsi_mac->cocoaViewRef;
 
        NSGraphicsContext *ctxt = [NSGraphicsContext currentContext];
        CGContextRef cg = (CGContextRef)[ctxt graphicsPort];
        
        NSRect viewRect = [view frame];
        
        CGContextSetRGBFillColor(cg, 1.0f, 0.0f, 0.0f, 1.0f);
        CGContextFillRect(cg, CGRectMake(10, 10, 10, 10));
        
        ds->FreeDrawingSurfaceInfo(dsi);
        if (env->ExceptionOccurred())
	{
            env->ExceptionDescribe();
        }
    }
 
Sorry about the confusion with the window and view classes, I am very new to cocoa and found an example and tried to modify it to do what I wanted as a learning exercise.

What was the original example? Post the URL.

Does the original example work as given?


Code:
        NSRect viewRect = [view frame];
        
        CGContextSetRGBFillColor(cg, 1.0f, 0.0f, 0.0f, 1.0f);
        CGContextFillRect(cg, CGRectMake(10, 10, 10, 10));
What purpose does viewRect serve? It's not used by any of the subsequent code.

Exactly when is your native code being called? Is it at a point where a call to CGContextFillRect() is appropriate?
 
Unfortunately, I can't remember the url, but have the code in a zip file. I've pasted the relevant section of code below, if that helps?

Code:
    dsi = ds->GetDrawingSurfaceInfo(ds);
    
    if (dsi != NULL) 
   {
        dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
        if ((*env)->ExceptionOccurred(env)) 
        {
            (*env)->ExceptionDescribe(env);
        }
        
        NSView *view = dsi_mac->cocoaViewRef;
        		
        NSWindow *window = [view window];
        NSGraphicsContext *ctxt = [NSGraphicsContext graphicsContextWithWindow:window];
        CGContextRef cg = [ctxt graphicsPort];

        
        NSRect windowRect = [window frame];
        CGContextConcatCTM(cg, CGAffineTransformMake(1, 0, 0, -1, dsi->bounds.x, windowRect.size.height-dsi->bounds.y));
        
        CGContextSetRGBFillColor(cg, 1.0f, 0.0f, 0.0f, 1.0f);
        CGContextFillRect(cg, CGRectMake(15, 15, dsi->bounds.width-30, dsi->bounds.height-30));
 
Does the original code work as given?

If so, then the obvious thing to do is start with the original code. Then remove, comment-out, or change things one by one. Recompile and rerun after every change, no matter how small or insignificant you think it is. If the change fails, i.e. the behavior is not what you expect, then the last change you made is significant, so go back and reverse it or fix it or whatever is needed to restore proper behavior.

As posted, your modified code is doing several things different, and has omitted some things I would expect to be important, such as the CGContextConcatCTM().

However, if the original code doesn't work as given, then you should probably find an example that does.


BTW, a little googling of words from the source suggests the following is the original:
http://developer.apple.com/mac/library/samplecode/JAWTExample/Introduction/Intro.html

I didn't read it extensively, but I did notice that it extends Canvas, not JPanel or any other lightweight JComponent.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.