How to make C++ code draw on Java components

跟風遠走 提交于 2019-12-13 06:31:56

问题


We are designing physics app, and we would like to have it written in Java, and also we want to use root (root is writen in C++). Root has some nice graphical features -- it is able to draw very nice 3D charts, and I'm thinking whether it's possible to embedd those charts in Java app.

Is there any way to make C++ code draw on for example JPanel?

Is there a way to pass back mouse/keyboard events? - we would like to have some interactivity, like rotating a 3d chart.

I asked similar question about embedding a lot of C++ code (that is not related to drawing anything) in Java app (also about root) it's here.


回答1:


Since SWT is built to be a fairly thin wrapper around native widgets, it may be easier to work with than Swing/AWT for your particular task. I realize this particular link is a bit old, but it does show how to quickly wrap a native widget.




回答2:


The JNI comment on the question itself seems to be the best way forward - I don't think you want to draw with root as-is; you'll want to write some interface code in between which is called from Java via JNI, which asks 'root' to redraw its image at a given size (the appropriate size for the image being something only the java component would know). Pass it back to the java component from the JNI call as just 32-bit image data, for instance.

I would not assume you can draw into a java component even in AWT; as it's basically java wrappers around native components - where the drawing is usually done in the native component itself (by the operating system, not by the java code).



来源:https://stackoverflow.com/questions/1534752/how-to-make-c-code-draw-on-java-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!