ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 使用JNativeHook制作游戏

2019-10-03 04:01:39  阅读:447  来源: 互联网

标签:java javafx overlay transparent jnativehook


我正在为PC游戏开发一个简单的叠加程序.它只是一个位于屏幕中心的透明矩形,但其大小由用户的鼠标滚轮控制.
因此,概念是简单地将透明矩形的大小与敌方玩家的大小相匹配以计算他的距离.

遗憾的是,我无法通过常规鼠标监听器实现这一点,因为鼠标必须同时专注于游戏和叠加程序.
我正在尝试JNativeHook,但我不能让我的矩形更新.有什么建议?

public class Main extends Application implements NativeMouseWheelListener {

    Rectangle r = new Rectangle();
    int y = 540; 
    int width = 75;
    int height = 180;
    int velocity = 10;

    @Override
    public void start(Stage stage) throws Exception {
         AnchorPane root = new AnchorPane();
         r = rect(); 
         root.getChildren().add(r);
         root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");

         Scene scene = new Scene(root, 1920, 1080); 
         scene.setFill(null);

         stage.initStyle(StageStyle.TRANSPARENT);
         stage.setScene(scene);
         stage.setX(0);
         stage.setY(0);
         stage.show();
         stage.setAlwaysOnTop(true);
    }

    public void nativeMouseWheelMoved(NativeMouseWheelEvent e) {
        int direction = e.getWheelRotation();
        System.out.println("Mouse Wheel Moved: " + direction);
        r.setY(r.getY() + direction);
    }

    public Rectangle rect() {
        r.setWidth(width);
        r.setHeight(height);
        r.setX(960 - (width/2));
        r.setY(540);
        r.setFill(Color.TRANSPARENT);
        r.setStroke(Color.BLACK);
        return r;
    }

    public static void main(String[] args) {
        go();
        launch(args);
    }

    public static void go() {
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            ex.printStackTrace();
            System.exit(1);
        }

        GlobalScreen.getInstance().addNativeMouseWheelListener(new Main());
    }
}

解决方法:

我强烈建议使用@ vince-emigh建议并在与ui环境相同的线程上运行JNativeHook,方法是使用AbstractExecutorService的自定义实现,如下所示.

public class JavaFxDispatchService extends AbstractExecutorService {
    private boolean running = false;

    public JavaFxDispatchService() {
        running = true;
    }

    public void shutdown() {
        running = false;
    }

    public List<Runnable> shutdownNow() {
        running = false;
        return new ArrayList<Runnable>(0);
    }

    public boolean isShutdown() {
        return !running;
    }

    public boolean isTerminated() {
        return !running;
    }

    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return true;
    }

    public void execute(Runnable r) {
        Platform.runLater(r);
    }
}

然后使用该类作为事件调度程序:

// Set the event dispatcher to a swing safe executor service.
GlobalScreen.setEventDispatcher(new JavaFxDispatchService());

// Initialize native hook.
try {
    GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
    System.err.println("There was a problem registering the native hook.");
    System.err.println(ex.getMessage());
    ex.printStackTrace();
    System.exit(1);
}

GlobalScreen.addNativeKeyListener(this);

有关详细信息,请参阅wiki中的Swing文章

标签:java,javafx,overlay,transparent,jnativehook
来源: https://codeday.me/bug/20191003/1846853.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有