alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (IBDemo.java)

This example Java source code file (IBDemo.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

app, appkitframework, awt, failed, foundationframework, ibdemo, jobjcruntime, mainmenu, myview, myviewclass, nsapplication, nsstring, nsview, override, runnable

The IBDemo.java Java example source code

/*
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
/**
 * To locate the nib correctly, this demo must run from
 * a .app (created with Jar Bundler...).
 *
 * TODO Add .app/Jar Bundler as ant task
 */

package com.apple.jobjc;

import java.awt.Toolkit;

import com.apple.jobjc.appkit.AppKitFramework;
import com.apple.jobjc.appkit.NSApplication;
import com.apple.jobjc.appkit.NSView;
import com.apple.jobjc.appkit.NSViewClass;
import com.apple.jobjc.foundation.FoundationFramework;
import com.apple.jobjc.foundation.NSRect;
import com.apple.jobjc.foundation.NSString;

class MyView extends NSView{
    static final AppKitFramework APPKIT = JObjC.getInstance().AppKit();

    public MyView(long objPtr, JObjCRuntime runtime) { super(objPtr, runtime); }

    @Override public void drawRect(NSRect r){
        APPKIT.NSColor().redColor().set();
        APPKIT.NSBezierPath().fillRect(r);
    }
}

class MyViewClass extends NSViewClass{
    protected MyViewClass(String name, JObjCRuntime runtime) { super(name, runtime); }
    public MyViewClass(JObjCRuntime runtime){ this(MyView.class.getSimpleName(), runtime); }
}

public class IBDemo{
    final static FoundationFramework FOUNDATION = JObjC.getInstance().Foundation();
    final static AppKitFramework APPKIT = JObjC.getInstance().AppKit();

    // Works if the JVM is launched on the main thread,
    // but JavaApplicationStub does not understand -XstartOnFirstThread
    public static void mainWithAppMain(String[] args){
        APPKIT.NSApplicationMain(0, null);
    }

    // Work around: let someone else init, and then
    // get on the main thread to load the nib.
    public static void mainWithoutAppMain(String[] args){
        Toolkit.getDefaultToolkit();

        Utils.get().threads().performOnMainThread(new Runnable(){
            public void run() {
                APPKIT.NSApplication().sharedApplication();
                NSApplication APP = APPKIT.NSApp();

                NSString nibName = Utils.get().strings().nsString("MainMenu");
                boolean loadedNib = APPKIT.NSBundleCategory().loadNibNamed_owner(nibName, APP);
                if(!loadedNib) throw new RuntimeException("Failed to load nib.");
            }}, false);
    }

    public static void main(String[] args){
        JObjCRuntime.getInstance().registerUserClass(MyView.class, MyViewClass.class);
        mainWithoutAppMain(args);
        //mainWithAppMain(args);
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java IBDemo.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.