如何将代码从C ++转换为Java for Android? [英] How to convert code from C + + to Java for Android?

查看:65
本文介绍了如何将代码从C ++转换为Java for Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有能正常工作的代码,我想将其添加到Android上的程序中.

I have code in C + + that works and I would like to add it to my program on Android.

extern bool rysuj;
extern bool kwadrat;
bool rys=false;
int x1, y1;
int x2, y2;
TChild *Child;

Graphics::TBitmap* bitmap = new Graphics::TBitmap;
TCanvas* dtCanvas = new TCanvas;
HWND hOkno;
RECT rcOkno;
unsigned uWidth;
unsigned uHeight;

FormMouseDown(int X, int Y)
{

   	bitmap->Width=uWidth;
   	bitmap->Height=uHeight;
	TRect src= Canvas->ClipRect;
  	bitmap->Canvas->CopyRect(src,Canvas,src);

   	if (kwadrat)
   	{
     		x1=X;
      		y1=Y;
   	}
   	rys=true;

}
FormMouseMove(int X, int Y)
{
   	if (rys && kwadrat)
   	{
        	Canvas->Draw(0,0, bitmap);
        	Canvas->Rectangle(x1, y1, X, Y);
   	}

}

FormMouseUp(int X, int Y)
{
        bitmap->Width=uWidth;
        bitmap->Height=uHeight;
        TRect src= Canvas->ClipRect;
        bitmap->Canvas->CopyRect(src,Canvas,src);

   	rys=false;


}
FormPaint(TObject *Sender)
{
        Canvas->Draw(0,0, bitmap);
}



请快速回复.我将非常感激:D



Please to quickly response. I would be very grateful :D

推荐答案

您不会将C ++文件转换为Java样式".您需要围绕现有的C ++代码创建一个JNI包装器.此JNI包装器实际上是Java可以调用的C ++代码.

通过包装器,我的意思是您不必修改现有的C ++代码库.最好用这种包装纸,或者说这种装订通常很薄.包装器代码仅用于公开现有功能,而不是实现它们.最好将实现保留在(便携式)C ++代码库中.

如果代码库不是太大,那么我建议您手动编写此包装器,如《 JavaTM Native Interface程序员指南和规范》
中所述
现在,如果您尝试绑定大型库,则可能会出现问题.因此,关于工具,我没有用过,而是看一下SWIG和相关的SWIG Java文档.

根据首页的说明,这就是您要的内容:

SWIG通常用于解析C/C ++接口并生成[Java,Python,PHP ...]调用C/C ++代码所需的胶水代码".

javah在某些情况下可能很有用,但这不是您要的.它从Java类中发现的本机声明中提取JNI样板代码.关于javac,它与Java编译器无关.
You don''t "convert C++ file to java style". You need to create a JNI wrapper around your existing C++ code. This JNI wrapper is actually C++ code that can be called by Java.

By wrapper I mean that you shouldn''t have to modify your existing C++ code base. This wrapper, or better said this binding should generally be very thin. The wrapper code is only meant to expose existing functionalities, not to implement them. It is better to leave the implementation in the (portable) C++ code base.

If the code base isn''t too large, then I recommend that you write this wrapper by hand, as explained in The JavaTM Native Interface Programmer''s Guide and Specification

Now, if you are trying to bind a large library, it may be problematic. So, in regard to tools, I haven''t used that, but have a look at SWIG, and the relevant SWIG Java documentation.

According to the homepage description, it''s what you''re asking for:

SWIG is typically used to parse C/C++ interfaces and generate the ''glue code'' required for [Java, Python, PHP, ...] to call into the C/C++ code.

javah can be useful in certain cases, but it''s not what you ask for. It extracts JNI boiler plate code out of native declarations found in Java classes. Regarding javac, it''s the Java compiler, that''s irrelevant.


@Override
        public boolean onTouchEvent(MotionEvent event) 
        {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) 
            {
                case MotionEvent.ACTION_DOWN:
                    if (kwadrat) {
                    	rect = new Rect();
                        touch_start(x, y);
                		invalidate();
					}

                    tryb_rys = true;
                    break;
                case MotionEvent.ACTION_MOVE:

                    if (kwadrat && tryb_rys) {
                    	rect.set((int)mX, (int)mY, (int)event.getX(), (int)event.getY());
                    	mCanvas.drawRect(rect, mPaint);
                		invalidate();

                	break;
                case MotionEvent.ACTION_UP:

                    tryb_rys = false;
                	break;
            }
            return true;
        }



我有类似的东西,但是当我绘制这个矩形时,它会留下痕迹.我只想用几行代码来解决这个问题.



I have something like that but when i draw this rectangle it leave a trace. I only want a few lines of code to fix this.


这篇关于如何将代码从C ++转换为Java for Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆