Thursday, 18 July 2013

When to create an interface

Consider you are making a first person shooting game. The player has multiple guns to choose from.
We can have an interface Gun which defines a function shoot().
We need different subclasses of Gun class namely ShotGun Sniper and so on.
class ShotGun implements Gun{
    public void shoot(){
       \\shotgun implementation of shoot.
    } 
}

class Sniper implements Gun{
    public void shoot(){
       \\sniper implementation of shoot.
    } 
}

Shooter Class

The shooter has all the guns in his Armour. Lets create a List to represent it.
List<Gun> listOfGuns = new ArrayList<Gun>();
The shooter cycles through his guns,as and when needed, using the function switchGun()
public void switchGun(){
    //code to cycle through the guns from the list of guns.
    currentGun = //the next gun in the list.
}
We can set the current Gun , using the above function and simply call shoot() function, whenfire() is called.
public void fire(){
    currentGun.shoot();
}
The behavior of the shoot function will vary according to different implementations of the Gun interface.

Conclusion

Create an interface, when a class function is dependent on a function from another class, which is subjected to change its behavior, based on instance(object) of the class implemented.
for e.g. fire() function from Shooter class expects guns(Sniper, ShotGun) to implement theshoot() function. So if we switch the gun and fire.
shooter.switchGun();
shooter.fire();
We have changed the behaviour of fire() function

Thursday, 11 July 2013

Moving undecorated window by clicking on JPanel

You can place another panel over the panel with the border, leaving the border visible.Use the following code to move your window.
public class MotionPanel extends JPanel{
    private Point initialClick;
    private JFrame parent;

    public MotionPanel(final JFrame parent){
    this.parent = parent;

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            initialClick = e.getPoint();
            getComponentAt(initialClick);
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {

            // get location of Window
            int thisX = parent.getLocation().x;
            int thisY = parent.getLocation().y;

            // Determine how much the mouse moved since the initial click
            int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
            int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);

            // Move window to this position
            int X = thisX + xMoved;
            int Y = thisY + yMoved;
            parent.setLocation(X, Y);
        }
    });
    }
}
I've been working with this code for a while now to make a custom titlebar for undecorated windows. P.S.:You can generalize this example by extending JComponent instead of JPanel.

Wednesday, 26 June 2013

Java method in a C++ application

The following function allows you to create the VM.
JNIEnv* create_vm(JavaVM ** jvm)
{
    JNIEnv *env;
    JavaVMInitArgs vm_args;
    JavaVMOption options[2];

    options[0].optionString = "-Djava.class.path=.";
    options[1].optionString = "-DXcheck:jni:pedantic";  

    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 2;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = JNI_TRUE; // remove unrecognized options

    int ret = JNI_CreateJavaVM(jvm, (void**) &env, &vm_args);
    if (ret < 0) printf("\n<<<<< Unable to Launch JVM >>>>>\n");
    return env;
}
Compile the famous Hello World program. The following function attempts to call the main method of the HelloWorld Program.
int main(int argc, char* argv[])
{
    JNIEnv* env;
    JavaVM* jvm;

    env = create_vm(&jvm);

    if (env == NULL) return 1;

    jclass myClass = NULL;
    jmethodID main = NULL;


    myClass = env->FindClass("HelloWorld");


    if (myClass != NULL)
        main = env->GetStaticMethodID(myClass, "main", "([Ljava/lang/String;)V");
    else
        printf("Unable to find the requested class\n");


    if (main != NULL)
    {
       env->CallStaticVoidMethod( myClass, main, " ");

    }else printf("main method not found") ;


    jvm->DestroyJavaVM();
    return 0;
}
Now put create_vm function and main function into a single cpp file, include jni.h and compile it. I used MinGW on windows.
g++ -D_JNI_IMPLEMENTATION_ -I"C:\Program Files\Java\jdk1.6.0_32\include" -I"C:\Program Files\Java\jdk1.6.0_32\include\win32" hello.cpp -L"C:\Program Files\Java\jre6\bin\client" -ljvm -o hello.exe
Exection Now if you run the created exe, you will get an error. jvm.dll not found . Put C:\Program Files\Java\jre6\bin\client in your PATH environment variable. Now you can run the exe file.
Note: Don't displace the jvm.dll file.

Popup Window in HTML

HTML
<div class="popup">

    <div class="blur-bg"></div>

    <div class="popup-win"><img src="foo.jpg"/></div>
</div>
CSS
    .blur-bg{
            position: absolute;
            top: 0px;
            z-index: 1000;
            opacity: 0.5;
            height: 2323px;
            left: 0px;
            width: 100%;
            background-color: black;
        }
    .popup-win{
            position: fixed;
            width: 400px;
            z-index: 1001;
            top: 50%;
            left: 50%;
            display: block;
            margin-top: -85px;
            margin-left: -215px;
            box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3);
            background-color: #fff;
            padding: 15px!important;
        }