dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Intermediate
Mar 3, 2009

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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);
        }
    }
}
Anghel Leonard
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date