I have been recently experimenting with JavaFXPorts and I have been trying to use it to build a native Android app. For the application I'm building I am trying to bake in NFC support but there doesn't seem to be much information out there about this. The only useful guide I found so far was some sample code written by johanvos on his BitBucket Repo here.
The problem I am facing now is using this code snippet, Netbeans keeps on reporting that the android libraries I'm trying to reference don't exist.
This is a some of my code:
package com.afropolymath.waitress;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.ReaderCallback;
import android.nfc.Tag;
import android.os.Bundle;
import javafxports.android.FXActivity;
public class Waitress extends Application implements ReaderCallback {
private Stage stage;
private StackPane rootLayout;
#Override
public void start(Stage stage) {
this.stage = stage;
this.initLayout();
Context ctx = FXActivity.getInstance();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(ctx);
nfcAdapter.enableReaderMode(FXActivity.getInstance(), this, NfcAdapter.FLAG_READER_NFC_A, Bundle.EMPTY);
}
public void initLayout() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/RootLayout.fxml"));
rootLayout = (StackPane) loader.load();
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(rootLayout, visualBounds.getWidth(), visualBounds.getHeight());
stage.getIcons().add(new Image(Waitress.class.getResourceAsStream("/assets/icon.png")));
stage.setScene(scene);
stage.show();
} catch (IOException e) {
}
}
#Override
public void onTagDiscovered(Tag tag) {
}
}
And these are the errors I'm getting:
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:12: error: package android.content does not exist
import android.content.Context;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:13: error: package android.nfc does not exist
import android.nfc.NfcAdapter;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:14: error: package android.nfc.NfcAdapter does not exist
import android.nfc.NfcAdapter.ReaderCallback;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:15: error: package android.nfc does not exist
import android.nfc.Tag;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:16: error: package android.os does not exist
import android.os.Bundle;
^
Any ideas on what might have been the problem?
While Johan Vos' NFC project works, it is based on the use of android and dalvik dependencies on the main package:
dependencies {
compile files("${ANDROID_HOME}/platforms/android-21/android.jar")
compile "org.javafxports:jfxdvk:8u60-b3"
}
But JavaFXPorts it's intended to create projects that can be deployed on different platforms, so now if you create a project using the Gluon's Plugin for NetBeans, you'll find four different folders to place the code:
Source Packages [Java], for all the common code, shared with all the platforms
Desktop/Java Packages, for Java code, only available running on Desktop
Android/Java Packages, for Java or Android code, only available running on Android
Ios/Java Packages, for Java code, only available running on iOS.
Also, if you check the project dependencies, you'll notice that the android.jar is available only for Android.
This means that you should create your regular project on the main package, and add the android part only in the Android package. In order to call the android class, you'll need to provide some mechanism on the main package to create an instance of it.
If you check the HelloPlatform sample, you'll notice that a PlatformService is used to load the classes depending on the platform, while PlatformProvider is an interface with the method/s that can be called from the main package, but will have the implementation given to each platform.
With this idea, but using Class.forName() instead of the service, Gluon Charm-Down library implements different native services.
You can have a look also at the GoNative sample and post that explains it in detail.
In case you want to implement a new service, like the NFC for Android,
these are the possible classes required.
Main package
NFCPlatform
public abstract class NFCPlatform {
public abstract NFCService getNFCService();
}
NFCService
public interface NFCService {
boolean isAvailable();
boolean isEnabled();
StringProperty tagIdProperty();
// other methods
}
Android Package
AndroidNFCPlatform
public class AndroidNFCPlatform extends NFCPlatform {
private AndroidNFCService nfcService;
#Override
public NFCService getNFCService() {
if (nfcService == null) {
nfcService = new AndroidNFCService();
}
return nfcService;
}
}
AndroidNFCService
Your implementation of the NFC service. Here we can use Android API. NetBeans won't complain.
This is a minimal implementation:
public class AndroidNFCService implements NFCService, ReaderCallback {
private final NfcAdapter nfcAdapter;
private StringProperty tagId;
public AndroidNFCService() {
NfcManager manager = (NfcManager) FXActivity.getInstance().getSystemService(FXActivity.NFC_SERVICE);
nfcAdapter = manager.getDefaultAdapter();
}
#Override
public boolean isAvailable() {
return nfcAdapter != null;
}
#Override
public boolean isEnabled() {
return isAvailable() && nfcAdapter.isEnabled();
}
#Override
public StringProperty tagIdProperty() {
if (tagId == null) {
tagId = new SimpleStringProperty();
}
return tagId;
}
#Override
public void onTagDiscovered(Tag tag) {
Platform.runLater(() ->
tagIdProperty().set(new String(tag.getId())));
}
}
Finally, you can use this service on your main class, once you get an instance of the NFCPlatform.
public static NFCPlatform getNFCPlatform() {
try {
if ("android".equals(System.getProperty("javafx.platform", "desktop"))) {
return (NFCPlatform) Class.forName("<your package>.AndroidNFCPlatform").newInstance();
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.out.println("Error " + ex);
}
return null;
}
#Override
public void start(Stage stage) {
NFCService nfcService = getNFCPlatform().getNFCService();
if (nfcService != null) {
System.out.println("Available: " + nfcService.isAvailable());
...
}
}
Related
I am trying to import javafx (.jar) files into a maven project. I have made a project with Netbeans (Java with Ant -> Java Application) and want to use the same code in Netbeans (Java with Maven -> Java Application). With Ant, I can easily import and use the (.jar) files, but this seems to be a blocked functionality with Maven.
Any solution to this?
I have looked up on several posts and guides online with no luck.
NOTE: Implementing .jar files within maven have been solved. The code does not work though.
Current Error: "Exception running application com.mycompany.ep_game_ref.Game Command execution failed."
package com.mycompany.ep_game_ref;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Game extends Application
{
public static String[] args;
public static Stage primarystage;
public static ObservableList<Items.Item> items = FXCollections.observableArrayList();
public static MainCentral startGame = new MainCentral();
public static Room.Introduction intro = new Room.Introduction();
public static Settings.Difficulty SETTINGS = new Settings.Difficulty();
public static Player player = new Player();
#Override
public void start(Stage stage)
{
this.primarystage = stage;
this.primarystage.setScene(new Scenes.Welcome().setWelcomeScene());
this.primarystage.getIcons().add(new Image("images/aq_logo.png"));
this.primarystage.setResizable(false);
this.primarystage.show();
}
public static void main(String[] args) {
Application.launch();
}
}
You can use JavaFX in a different way:
File>New Project>Maven>JavaFX Application.
My codenameone application crashes anything I use this native code
package com.mycompany.interfaces;
import android.app.Application;
import android.content.Context;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
public class InitialiseApp extends Application{
private static Context context;
public static Context getContext() {
return context;
}
#Override
public void onCreate()
{
super.onCreate();
context = getApplicationContext();
try
{
FirebaseApp.initializeApp(this, new FirebaseOptions.Builder().
setApiKey("XXXXXXXXXXXXXX").
setApplicationId("XXXXXXXX").
setGcmSenderId("XXXXXXXXXX")
.build());
FirebaseInstanceId.getInstance().deleteInstanceId();
FirebaseInstanceId.getInstance().getToken("XXXXXXXXXX",FirebaseMessaging.INSTANCE_ID_SCOPE);
FirebaseMessaging.getInstance().subscribeToTopic("test");
}
catch(Exception c)
{
c.printStackTrace();
}
}
}
I declare the class in the android.xapplication_attr android:name="com.mycompany.interfaces.InitialiseApp"
Need a assistance
Are you putting this in the native interface stub or in the CN1 part of the code?
Also, I don’t think that’s how you get a context in CN1. Look in the developer guide and video tutorials for Native Interfaces. I also recall a series of blog posts about native interfaces that dive into writing the Android code. You’ll need to use something from the AndroidNativeUtil class like: AndroidNativeUtil.getActivity().
I've tried to modify minecraft by adding a new item called "uranium". Therefore I created the class "Trauma.java" in the main package and a few other classes listed below.
All packages and classes:
Package Explorer
Trauma.java
package main;
import items.ItemUranium;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import proxy.ServerProxy;
#Mod(modid = Trauma.MODID)
public class Trauma {
public static final String MODID = "Trauma";
#SidedProxy(clientSide = "proxy.ClientProxy", serverSide = "proxy.ServerProxy")
public static ServerProxy proxy;
public static ItemUranium uranium = new ItemUranium();
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
GameRegistry.register(uranium);
}
#EventHandler
public void init(FMLInitializationEvent event) {
proxy.registerClientStuff();
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}
BasicItem.java
package items;
import net.minecraft.item.Item;
public class BasicItem extends Item {
public BasicItem(String name) {
setUnlocalizedName(name);
setRegistryName(name);
}
}
ItemUranium.java
package items;
public class ItemUranium extends BasicItem {
public ItemUranium() {
super("uranium");
}
}
ClientProxy.java
package proxy;
import items.BasicItem;
import main.Trauma;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
public class ClientProxy extends ServerProxy {
#Override
public void registerClientStuff () {
registerItemModel(Trauma.uranium);
}
public static void registerItemModel(BasicItem item) {
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Trauma.MODID + ":" + item.getRegistryName(), "inventory"));
}
}
ServerProxy.java
package proxy;
public class ServerProxy {
public void registerClientStuff() {}
}
uranium.json
{
"parent": "item/generated",
"textures": {
"layer0": "Trauma:items/uranium"
}
}
uranium.png
ingame
Also I don't know why the item in inventory isn't called uranium...
I spent two hours on fixing the problem and it didn't help so it would be really nice if somebody of you may help me.
Thanks :)
Don't use the Model Mesher:
The model mesher is Vanilla (Mojang) code and using it correctly has always been finicky and unreliable, failing if you called it too early and failing if you called it too late. So Forge added the ModelLoader class to resolve that problem.
Replace this line:
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(...)
With this line:
ModelLoader.setCustomModelResourceLocation(...)
The ... contents are identical.
Second, depending on what version of Minecraft you're using, you should...:
Stop using GameRegistry.Register
Instead use the RegistryEvent.Register<T> events (where <T> will be <Block> to register blocks, <Item> to register items, etc)
Register your models in the ModelRegistryEvent and no where else.
This event is #SideOnly(CLIENT) and can be subscribed to in your client proxy, avoiding the need to forward references through your proxy class. Eg. I do it like this, where lines 197-199 is the most common scenario needed, where the array is populated during the item registration event. The rest of that method handles the custom state mappers and custom mesh definitions that are used by only a handful of items/blocks and not relevant here.
Include your Mod ID in your unlocalized name. The best way to do this would be setUnlocalizedName(getRegistryName().toString());
See also the Forge documentation on events.
I am new to creating minecraft plugins, however not new to programming, I am following a tutorial very thoroughly, the video has good ratings so it is trusted, when watching the video the guy has no problems what so ever (Youtube video on developing minecraft plugins) , so I did some research into solutions but always the line through the code.
Eclipse gives me the option for: #SuppressWarnings("deprecation") which allows the code to be used still but I would rather have no need of that usage.
Basically my question is why is there the need of the line going through the code and how do I find a solution to get rid of it.
Main class:
package com.jc1;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin
{
public Permission pPermission = new Permission("playerAbilities.allowed");
#Override
public void onEnable()
{
new BlockListener(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(pPermission);
}
#Override
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("giveitems") && sender instanceof Player)
{
Player p = (Player) sender;
if(p.hasPermission("playerAbilities.allowed"))
{
p.setItemInHand(new ItemStack(Material.DIAMOND_BOOTS));
}
return true;
}
return false;
}
}
Secondary class:
package com.jc1;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockListener implements Listener
{
public BlockListener(Core plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent e)
{
Player p = e.getPlayer();
if(!p.hasPermission("playerAbilities.allowed"))
{
e.setCancelled(true);
}
}
}
The method is deprecated, meaning that it is not recommended to be used anymore and is most likely replaced with another method.
Methods that are deprecated may still work as intended.
A simple search for the method reveals (this) documentation, stating:
players can duel wield now use the methods for the specific hand instead
which referes to the #see references:
getItemInMainHand() and getItemInOffHand().
Use this:
player.getInventory().getItemInMainHand()
Instead of:
player.getItemInHand()
Hope this helps! :D
This is a follow up to a question I asked here.
I have copied and pasted this code from this tutorial. When I paste it into Android Studio, the 'this' parameter of of content.getLoadManager.initLoader() is highlighted in red and shows the following error:
Wrong 3rd Argument Type. Found 'com.example.carl.loaderDemo.FooLoaderClient', requried: 'android.app.LoaderManager.LoaderCallBacks
I've ran into this previously (see first link). I was hoping this tutorial would help but I just seem to be going in endless circles!
Can anyone point me in the right direction?!
package com.example.carl.loaderdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
public class FooLoader extends AsyncTaskLoader {
public FooLoader(Context context, Bundle args) {
super(context);
// do some initializations here
}
public String loadInBackground() {
String result = "";
// ...
// do long running tasks here
// ...
return result;
}
}
class FooLoaderClient implements LoaderManager.LoaderCallbacks {
Activity context;
// to be used for support library:
// FragmentActivity context2;
public Loader onCreateLoader(int id, Bundle args) {
// init loader depending on id
return new FooLoader(context, args);
}
#Override
public void onLoadFinished(Loader loader, Object data) {
}
public void onLoaderReset(Loader loader) {
// ...
}
public void useLoader() {
Bundle args = new Bundle();
// ...
// fill in args
// ...
Loader loader =
context.getLoaderManager().initLoader(0, args, this);
// with support library:
// Loader loader =
// context2.getSupportLoaderManager().initLoader(0, args, this);
// call forceLoad() to start processing
loader.forceLoad();
}
}
Screenshot of error message:
There is a mismatch in your imports:
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
But you need
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
import android.content.Loader;
You cannot mix the support library with the android framework. Alternatively you can subclass FragmentActivity and call getSupportLoaderManager() instead.
You're implementing android.support.v4.app.LoaderManager.LoaderCallbacks but the client is expecting android.app.LoaderManager.LoaderCallbacks. You need to be consistent in which loader API you're using.