简体   繁体   中英

replace handleEvent(Event e) by processEvent(AWTEvent e)

I can't find a way to transform the code when replacing the deprecated function handleEvent with processEvent . can anyone help?

how to get x, y and the id of the event on processEvent?

here is my function:

public boolean handleEvent(Event evt) {

      if (evt.target == this) {
        // move floatting text zone
        if (dragingTextZone == true) {
            this.posStr.x = evt.x;
            this.posStr.y = evt.y;
            repaint(this.posStr.x,
                    this.posStr.y,
                    1000,
                    (int) (_imageViewer.getCurrent_font().getSize() * _imageViewer.getScalefactor()));

            if (evt.id == Event.MOUSE_DOWN) {
                dragingTextZone = false;
                addTextToPicture();
            }
        }

        if (evt.id == Event.MOUSE_DRAG) {

            if (dragitem.isDragging()) {
                repaint(dragitem.getX(),
                        dragitem.getY(),
                        dragitem.getWidth(),
                        dragitem.getHeight());
                dragitem.drag(evt.x, evt.y);
                repaint(dragitem.getX(),
                        dragitem.getY(),
                        dragitem.getWidth(),
                        dragitem.getHeight());
            }
        }
        else {
            if (evt.id == Event.MOUSE_UP) {
                // setting crop zone
                if (dragingCropZone || dragingMask) {
                    endDrag(evt);
                }
                else if (dragingLine) {
                    addLineToPicture();
                    endDrag(evt);
                }
            }
            if (evt.id == Event.MOUSE_DOWN) {
                if (getEditMode() == EDTMODE_ALL) {
                    if (evt.modifiers == Event.CTRL_MASK) {
                        startDragHighLight(evt);
                    }
                    else if (evt.modifiers == Event.ALT_MASK) {
                        startDragLine(evt);
                    }
                    else {
                        if (clickedOnFocusedItem(evt)) {
                            startDragMask(evt);
                        }
                    }
                }
            }
        }
    }

    return super.handleEvent(evt); // This passess the mouse click back up to Applet
}

Thank you,

Rather than a single Event class that includes everything, the new (relatively speaking - it's been around since Java 1.1) AWTEvent approach is to have different subclasses of AWTEvent for different purposes. In your case it's the MouseEvent subclass you're interested in, so you would need to call enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK) to enable handling of the relevant events, then override processMouseEvent and processMouseMotionEvent to do the handling.

But the preferred way to handle events is not to override the process* methods but rather to register separate listener objects. In your case you would create another class (possibly an inner class inside your main one) which is a subclass of MouseAdapter , override the relevant event hook methods in that, then register an instance of the adapter class by calling both addMouseListener and addMouseMotionListener on the main component.

There is extensive documentation on the Oracle website (and elsewhere) on how to implement event handling in an AWT or Swing application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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