简体   繁体   English

如何将此事件方法从C#转换为Java?

[英]How to convert this event method from C# to java?

I have this class in C# that implements interface IPhoto: 我在C#中具有实现接口IPhoto的此类:

class BorderPhoto : Form, IPhoto
    {
        IPhoto pho;
        Color color;    
        public BorderPhoto(IPhoto p, Color c)
        {
            pho = p;
            color = c;
            this.Paint += new PaintEventHandler(Drawer);
        }    
        public void Drawer(object sender, PaintEventArgs e)
        {
            pho.Drawer(sender, e);
            e.Graphics.DrawRectangle(new Pen(color, 10), 25, 15, 215, 225);
        }
    }

And this is the interface in C#: 这是C#中的接口:

namespace GivenWihInterface
{
    interface IPhoto
    {
        void Drawer(object sender, PaintEventArgs e);
    }
}

I would like to convert this code in java, this is the java version class I have: 我想在Java中转换此代码,这是我拥有的Java版本类:

package GivenWihInterface;
import java.awt.Color;
public class BorderPhoto {
    IPhoto pho;
    Color color;

    public BorderPhoto(IPhoto p, Color c)
    {
        pho = p;
        color = c;
        //this.Paint += new PaintEventHandler(Drawer); 
        // How to pass the delegate PaintEventHandler hire
    }

    public void Drawer(/*How to pass parameters*/)
    {
        //pho.Drawer(sender, e); 
        // How to implement paint event

        //e.Graphics.DrawRectangle(new Pen(color, 10), 25, 15, 215, 225); 
        // How to implement paint event
    }

}

And this is the interface: 这是接口:

package GivenWihInterface;
public interface IPhoto {
    public void Drawer(/*How to pass parameters*/);    
}

I have a problem with assigning a delegate and declaring the Object and Event parameters not sure what event to pass as a parameter, and how to assign it as a delegate in java. 我在分配委托并声明Object和Event参数时遇到问题,不确定在参数中传递哪个事件以及如何在Java中将其分配为委托。 Can someone help me with this problem? 有人可以帮我解决这个问题吗?

EDIT: 编辑:

I tried to use the Tangible Software Solutions Converter with no luck, I am still experiencing the same problem: 我尝试使用无形软件解决方案转换器很幸运,但我仍然遇到相同的问题:

在此处输入图片说明

public class BorderPhoto{

    IPhoto pho;
    Color color;

    public BorderPhoto(IPhoto p, Color c)
    {
        pho = p;
        color = c;
        this.addPaintEventhandler(new IPhoto(){
            public void Drawer(object sender, ...other parameters...){
                // put your code here
            }
        }); 
    }

    public void addPaintEventHandler(Iphoto pho){
        this.pho = pho;
    }

    public void onDraw(this, ...other parameters...){
        pho.Drawer(this, ...other parameters...);
    }
}

Property named pho holds a handler object, ie it will handle the draw, so pass it an object which wants to/can handle the drawing, which in your code is an object of type IPhoto so pass it an object of IPhoto. 名为pho的属性包含一个处理程序对象,即它将处理绘图,因此将要/可以处理该绘图的对象传递给它,在您的代码中,该对象是IPhoto类型的对象,因此将其传递给IPhoto对象。

Here we are passing the object as an anonymous object, ie instead of creating the object beforehand and then passing it, we created the object at the point where it was needed, that too without explicitly extending the interface. 在这里,我们将对象作为匿名对象传递,即,不是先创建对象然后再传递它,而是在需要它的位置创建对象,这也无需显式扩展接口。

Once the handler object is attached to the property, you can just invoke the Drawer() of the object, and the object will start drawing. 将处理程序对象附加到属性后,您只需调用该对象的Drawer(),该对象就会开始绘制。

Pass it any required parameters as mentioned in the interface. 如界面中所述,将任何必需的参数传递给它。 Most probably it will require a sender object, which here is the BorderPhoto object. 最有可能需要一个发件人对象,这里是BorderPhoto对象。 This object is needed if you want to call some method of the BorderPhoto object when you draw, like the canvas on which to draw, which is held by BorderPhoto. 如果要在绘制时调用BorderPhoto对象的某些方法(例如,由BorderPhoto保留的绘制画布),则需要此对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM