Error with nifty and awt

Hello,



I build my game for android, 2 weeks ago it did works, but after I updated with the latest nightly build, I get an error when running the apps :



but java.awt is not part of android, did I do something wrong ?



E/dalvikvm(18150): Could not find class 'java.awt.datatransfer.Clipboard', referenced from method de.lessvoid.nifty.Nifty.initializeClipboard
W/dalvikvm(18150): VFY: unable to resolve const-class 1993 (Ljava/awt/datatransfer/Clipboard;) in Lde/lessvoid/nifty/Nifty;
D/dalvikvm(18150): VFY: replacing opcode 0x1c at 0x0000
D/dalvikvm(18150): VFY: dead code 0x0002-000f in Lde/lessvoid/nifty/Nifty;.initializeClipboard ()V
D/dalvikvm(18150): GC_CONCURRENT freed 1398K, 55% free 3652K/8007K, external 2138K/2394K, paused 2ms+5ms
D/dalvikvm(18150): GC_CONCURRENT freed 951K, 54% free 3734K/8007K, external 2138K/2394K, paused 2ms+3ms
D/dalvikvm(18150): GC_FOR_MALLOC freed 546K, 50% free 4032K/8007K, external 2138K/2394K, paused 27ms
D/dalvikvm( 9409): GC_EXPLICIT freed 50K, 46% free 6452K/11783K, external 2045K/2557K, paused 81ms
D/dalvikvm(18150): GC_CONCURRENT freed 1120K, 50% free 4061K/8007K, external 2138K/2394K, paused 2ms+7ms
D/dalvikvm(18150): GC_FOR_MALLOC freed 1193K, 53% free 3788K/8007K, external 2138K/2394K, paused 32ms
W/dalvikvm(18150): threadid=8: thread exiting with uncaught exception (group=0x40015560)
E/AndroidHarness(18150): java.lang.NoClassDefFoundError: java.awt.datatransfer.Clipboard
E/AndroidHarness(18150): SEVERE AndroidHarness 13:43:23 Exception thrown in Thread[GLThread 10,5,main]: at de.lessvoid.nifty.Nifty.initializeClipboard(166)
E/AndroidHarness(18150): at de.lessvoid.nifty.Nifty.(125)
E/AndroidHarness(18150): at com.jme3.niftygui.NiftyJmeDisplay.(110)
E/AndroidHarness(18150): at mygame.Main.simpleInitApp(323)
E/AndroidHarness(18150): at com.jme3.app.SimpleApplication.initialize(230)
E/AndroidHarness(18150): at com.jme3.system.android.OGLESContext.initInThread(266)
E/AndroidHarness(18150): at com.jme3.system.android.OGLESContext.onSurfaceCreated(217)
E/AndroidHarness(18150): at android.opengl.GLSurfaceView$GLThread.guardedRun(1348)
E/AndroidHarness(18150): at android.opengl.GLSurfaceView$GLThread.run(1118)

oO It should be the other way round, two weeks ago nifty had references to AWT, now not anymoreā€¦ Try deleting all files from the ā€œlibā€ directory of the android project if you use the SDK, maybe the old nifty files are still there (if you use the SDK).

I had the same issue today. I updated the SDK this morning and then created a new test project tonight. The project works on the PC, but when run on Android I get the same type of error. This was my first attempt at a Nifty screen on Android, so I donā€™t have any previous experience with Nifty.



[java]

java.lang.NoClassDefFoundError: java.awt.datatransfer.Clipboard

Exception thrown in Thread[GLThread 10,5,main]: at de.lessvoid.nifty.Nifty.initializeClipboard(166)

at de.lessvoid.nifty.Nifty.(125)

at com.jme3.niftygui.NiftyJmeDisplay.(110)

at mygame.Main.simpleInitApp(40)

[/java]



The code in app is the follwing:

[java]

package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.niftygui.NiftyJmeDisplay;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.builder.LayerBuilder;

import de.lessvoid.nifty.builder.PanelBuilder;

import de.lessvoid.nifty.builder.ScreenBuilder;

import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;

import de.lessvoid.nifty.screen.DefaultScreenController;



/**

  • test
  • @author normenhansen

    */

    public class Main extends SimpleApplication {



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    Box b = new Box(Vector3f.ZERO, 1, 1, 1);

    Geometry geom = new Geometry("Box", b);



    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    mat.setColor("Color", ColorRGBA.Blue);

    geom.setMaterial(mat);



    rootNode.attachChild(geom);



    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

    assetManager, inputManager, audioRenderer, guiViewPort);

    Nifty nifty = niftyDisplay.getNifty();

    guiViewPort.addProcessor(niftyDisplay);

    flyCam.setDragToRotate(true);



    nifty.loadStyleFile("nifty-default-styles.xml");

    nifty.loadControlFile("nifty-default-controls.xml");



    // <screen>

    nifty.addScreen("Screen_ID", new ScreenBuilder("Hello Nifty Screen"){{

    controller(new DefaultScreenController()); // Screen properties



    // <layer>

    layer(new LayerBuilder("Layer_ID") {{

    childLayoutVertical(); // layer properties, add moreā€¦



    // <panel>

    panel(new PanelBuilder("Panel_ID") {{

    childLayoutCenter(); // panel properties, add moreā€¦



    // GUI elements

    control(new ButtonBuilder("Button_ID", "Hello Nifty"){{

    alignCenter();

    valignCenter();

    height("5%");

    width("15%");

    }});



    //ā€¦ add more GUI elements here



    }});

    // </panel>

    }});

    // </layer>

    }}.build(nifty));

    // </screen>



    nifty.gotoScreen("Screen_ID"); // start the screen



    }



    @Override

    public void simpleUpdate(float tpf) {

    //TODO: add update code

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }



    [/java]

The nifty jar libraries that are being imported should have no version numbers in their name, can you make sure that is the case? Also, did you try deleting all files from the ā€œlibā€ directory of the android project (ā€œmobileā€ folder)?

I originally created a new project tonight after updating the SDK this morning. I also just deleted all the files in mobile=>libs and rebuilt. Same result.



I checked the jar filenames in mobile=>libs and did not see any version numbers in the filenames.

assets.jar

eventbus.jar

jbullet.jar

jME3-jbullet.jar

jMonkeyEngine3.jar

j-ogg-oggd.jar

j-ogg-vorbisd.jar

MyGame.jar

nifty.jar

nifty-default-controls.jar

nifty-style-black.jar

noise-0.0.1-SNAPSHOT.jar

stack-alloc.jar

vecmath.jar

xmlpull-xpp3.jar

@void256: What is this sorcery? oO

Normen is right it should be the other way around and it should work now.



But I think Iā€™ve messed up this new code that was added to fix this:



[java]private void initializeClipboard() {

try {

Class.forName(java.awt.datatransfer.Clipboard.class.getName());

clipboard = new ClipboardAWT();

} catch (ClassNotFoundException e) {

log.info(ā€œunable to access class ā€˜java.awt.datatransfer.Clipboardā€™. clipboard will be disabled.ā€);

clipboard = new ClipboardNull();

}

}[/java]



ClassNotFoundException is not the same as NoClassDefFoundError! This catches the wrong exception! Damn it!



Sorry about that :frowning: I will fix this asapā€¦

2 Likes

No ā€¦ Its because you load it the way you do. Using ā€œjava.awt.datatransfer.Clipboard.classā€ essentially forces the class to load so when you call Class.forName() the class is already loaded. You have to pass a string, not a class object, to that method and then it will load it in the right way. E.g.:

[java]Class.forName(ā€œjava.awt.datatransfer.Clipboardā€);[/java]

1 Like

I updated the platform today and tried this again. I was plenty surprised that my Hello Nifty box showed up on the phone. Thanks for making the changes!

Yay! Nifty on android is now reality :smiley:

Maybe you can run the nifty examples and show us some screenshots? Video even better

Will do. I should be able to get through some of them in the next day of two. Iā€™ve never attached pics on a post before. Do you have to upload them to imgur with the upload button?

yeah

Iā€™ve got one of the tutorials running on the phone (https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_xml_layout), but when I touch one of the buttons, nothing happens. When I click on the button on the PC, the gui switches screens like itā€™s supposed to.



in the ADB Logger, I get the message

[processMouseEvent] [0,0,0,0,true] processed [false]

when I touch the screen so I think the ā€œclickā€ is seen by nifty, but the event doesnā€™t fire. Iā€™m going to try a bigger button to see if itā€™s just too small and the touch isnā€™t actually on the button.

No dice. Nice big fat button and still nothing. When I drag my finger within the button I get the same message in the ADB log and the flyCam moves like I was not touching the button.

The first two coordinates in that message is the mouse position. When you run in desktop you see something like:

[java]INFO: [processMouseEvent] [1215, 671, 0, 0, false] processed [true][/java]

The question is, why are the coordinates 0,0 being passed down

In the file com.jme3.niftygui.InputSystemJme the x and y values being passed to nifty are only getting updated in onMouseMotionEventQueued. Since the phone doesnā€™t have mouse motion, the x and y stay 0. I added some code to set the x and y values on the touch event and it is working now. Iā€™m not sure this is the right way to fix the issue. Maybe someone can review and determine if there should be a different way to fix it.



[java]

public void onTouchEvent(TouchEvent evt) {

x = (int) evt.getX();

y = (int) (height - evt.getY());

}

[/java]



[EDIT] This isnā€™t the right way to fix this. I have to hit the button twice to get the action to fire. The first time I hit the button, the button becomes ā€œselectedā€ and a border shows up around the button. If I touch the button a second time, the event fires.



Can someone look at this and see what the right way to fix it is?

Perhaps it should be set on a button press/release instead?

Here is my patch file for getting the touch events to nifty. Itā€™s now working on the phone and I donā€™t think I broke anything on the PC version.



[java]

This patch file was generated by NetBeans IDE

Following Index: paths are relative to: D:UserspotterecDocumentsjMonkeyProjectsjME3srcniftyguicomjme3niftygui

This patch can be applied using context Tools: Patch action on respective folder.

It uses platform neutral UTF-8 encoding and n newlines.

Above lines and this line are ignored by the patching process.

Index: InputSystemJme.java

ā€” InputSystemJme.java Base (BASE)

+++ InputSystemJme.java Locally Modified (Based On LOCAL)

@@ -92,6 +92,24 @@

boolean result = nifty.update();

}


  • private void onTouchEventQueued(TouchEvent evt, NiftyInputConsumer nic) {
  •    x = (int) evt.getX();<br />
    
  •    y = (int) (height - evt.getY());<br />
    

+

  •   switch (evt.getType()) {<br />
    
  •   	case DOWN:<br />
    
  •           nic.processMouseEvent(x, y, 0, 0, false);<br />
    
  •   		break;<br />
    
  •   	case UP:<br />
    
  •   		boolean consumed = false;<br />
    
  •   		consumed = nic.processMouseEvent(x, y, 0, 0, true);<br />
    
  •   		if (consumed){<br />
    
  •   			evt.setConsumed();<br />
    
  •   		}<br />
    
  •   		break;<br />
    
  •   }<br />
    
  • }

    +

    private void onMouseMotionEventQueued(MouseMotionEvent evt, NiftyInputConsumer nic) {

    x = evt.getX();

    y = height - evt.getY();

    @@ -179,6 +197,7 @@

    }



    public void onTouchEvent(TouchEvent evt) {
  •   inputQueue.add(evt);<br />
    

}



public void forwardEvents(NiftyInputConsumer nic) {

@@ -192,6 +211,8 @@

onMouseButtonEventQueued( (MouseButtonEvent)evt, nic);

}else if (evt instanceof KeyInputEvent){

onKeyEventQueued( (KeyInputEvent)evt, nic);

  •        }else if (evt instanceof TouchEvent){<br />
    
  •            onTouchEventQueued( (TouchEvent)evt, nic);<br />
    

}

}





[/java]

Sorry, discovered that with the patch above and you drag over the button and lift your finger, the button became active. The patch below removes that problem. I think Iā€™m done. Sorry for the multple patches.



[java]

This patch file was generated by NetBeans IDE

Following Index: paths are relative to: D:UserspotterecDocumentsjMonkeyProjectsjME3srcniftyguicomjme3niftygui

This patch can be applied using context Tools: Patch action on respective folder.

It uses platform neutral UTF-8 encoding and n newlines.

Above lines and this line are ignored by the patching process.

Index: InputSystemJme.java

ā€” InputSystemJme.java Base (BASE)

+++ InputSystemJme.java Locally Modified (Based On LOCAL)

@@ -92,6 +92,39 @@

boolean result = nifty.update();

}


  • private void onTouchEventQueued(TouchEvent evt, NiftyInputConsumer nic) {
  •    boolean wasPressed = pressed;<br />
    
  •    boolean forwardToNifty = true;<br />
    
  •    boolean consumed = false;<br />
    

+

  •   x = (int) evt.getX();<br />
    
  •    y = (int) (height - evt.getY());<br />
    

+

  •   switch (evt.getType()) {<br />
    
  •   	case DOWN:<br />
    
  •   		consumed = nic.processMouseEvent(x, y, 0, 0, false);<br />
    
  •   		isDragging = true;<br />
    
  •   		niftyOwnsDragging = consumed;<br />
    
  •   		if (consumed){<br />
    
  •   			evt.setConsumed();<br />
    
  •   		}<br />
    

+

  •   		break;<br />
    

+

  •   	case UP:<br />
    
  •   		if (niftyOwnsDragging){<br />
    
  •   			consumed = nic.processMouseEvent(x, y, 0, buttonIndex, pressed);<br />
    
  •   			if (consumed){<br />
    
  •   				evt.setConsumed();<br />
    
  •   			}<br />
    
  •   		}<br />
    

+

  •   		isDragging = false;<br />
    
  •   		niftyOwnsDragging = false;<br />
    
  •   		break;<br />
    
  •   }<br />
    
  • }

    +

    private void onMouseMotionEventQueued(MouseMotionEvent evt, NiftyInputConsumer nic) {

    x = evt.getX();

    y = height - evt.getY();

    @@ -179,6 +212,7 @@

    }



    public void onTouchEvent(TouchEvent evt) {
  •   inputQueue.add(evt);<br />
    

}



public void forwardEvents(NiftyInputConsumer nic) {

@@ -192,6 +226,8 @@

onMouseButtonEventQueued( (MouseButtonEvent)evt, nic);

}else if (evt instanceof KeyInputEvent){

onKeyEventQueued( (KeyInputEvent)evt, nic);

  •        }else if (evt instanceof TouchEvent){<br />
    
  •            onTouchEventQueued( (TouchEvent)evt, nic);<br />
    

}

}





[/java]

2 Likes

Shouldnā€™t dragging (using the touchscreen) be supported as well? Also why regular mouse events do not work? They are supposed to be ā€œemulatedā€ when running under Android