Create a New Event Using AWTEventMulticaster

This code shows how to create a new event using AWTEventMulticaster.

import java.awt.*;import java .awt.event.*;class Buton extends Component{    private Image button_pressed,button_released;    private boolean press=false;         ActionListener actionListener;            public Buton(){        button_pressed=Toolkit.getDefaultToolkit().getImage("?"); //pressed button        button_released=Toolkit.getDefaultToolkit().getImage("?"); //released button                        MediaTracker mt = new MediaTracker(this);        mt.addImage(button_pressed,0);mt.addImage(button_released,1);        try {              mt.waitForAll();              } catch (InterruptedException e)                      {System.out.println(e.getMessage()); }        enableEvents( AWTEvent.MOUSE_EVENT_MASK );        setSize( button_pressed.getWidth(null),button_pressed.getHeight(null) );             }                public Dimension getPreferredSize(){        return getSize();        }            public void paint(Graphics g){        if(press)g.drawImage(button_pressed,0,0, this);              if(!press)g.drawImage(button_released,0,0,this);               }    public void update(Graphics g){paint(g);}    public void processEvent(AWTEvent e){        if (e.getID()==MouseEvent.MOUSE_PRESSED){            press=true;repaint();        }        if (e.getID()==MouseEvent.MOUSE_RELEASED){            press=false;repaint();Event();        }        super.processEvent(e);    }        public void addActionListener(ActionListener l){        actionListener=AWTEventMulticaster.add(actionListener, l);    }    public void removeActionListener(ActionListener l){        actionListener=AWTEventMulticaster.remove(actionListener, l);    }    private void Event(){        if (actionListener!=null){            ActionEvent t=new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"press");            actionListener.actionPerformed(t);        }    }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: