devxlogo

Create a New Event Using AWTEventMulticaster

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);        }    }}
See also  Small Business Strategies with Venmo
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist