import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MenuItem extends Container
{
private static final long serialVersionUID = 1L;
private static final Font DEFAULT_FONT = new Font("Serif", Font.PLAIN, 12);
private static final Color DEFAULT_FOREGROUND = Color.BLACK;
private static final Color DEFAULT_BACKGROUND = Color.WHITE;
private static final Lock LISTENER_LOCK = new ReentrantLock();
private String text;
private BufferedImage buffImg;
private List<ActionListener> listenerList;
public MenuItem()
{
this(null);
}
public MenuItem(String text)
{
super();
int width = 0;
if(text != null)
width = getFontMetrics(DEFAULT_FONT).stringWidth(text);
int height = DEFAULT_FONT.getSize();
setSize(width, height);
setForeground(DEFAULT_FOREGROUND);
setBackground(DEFAULT_BACKGROUND);
setFont(DEFAULT_FONT);
setFocusable(false);
this.text = text;
listenerList = new LinkedList<ActionListener>();
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if(getFont() != null && text != null)
{
g.setColor(getForeground());
g.drawString(text, 0, 9);
}
}
public void update(Graphics g)
{
if(buffImg == null || (buffImg.getWidth() != getWidth() || buffImg.getHeight() != getHeight()))
buffImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics buffG = buffImg.getGraphics();
paint(buffG);
g.drawImage(buffImg, 0, 0, null);
}
public void setText(String text)
{
int width = 0;
if(text != null)
width = getFontMetrics(DEFAULT_FONT).stringWidth(text);
setSize(width, getHeight());
this.text = text;
}
public void setFont(Font font)
{
int height = 0;
if(font != null)
height = font.getSize();
setSize(getWidth(), height);
super.setFont(font);
}
public void addActionListener(ActionListener listener)
{
LISTENER_LOCK.lock();
try
{
listenerList.add(listener);
}
finally
{
LISTENER_LOCK.unlock();
}
}
public void removeActionListener(int i)
{
LISTENER_LOCK.lock();
try
{
listenerList.remove(i);
}
finally
{
LISTENER_LOCK.unlock();
}
}
public void removeActionListener(ActionListener listener)
{
LISTENER_LOCK.lock();
try
{
listenerList.remove(listener);
}
finally
{
LISTENER_LOCK.unlock();
}
}
public void selected()
{
final ActionEvent actionEvent = new ActionEvent(this, 0, "Select");
LISTENER_LOCK.lock();
try
{
for(final ActionListener listener:listenerList)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
listener.actionPerformed(actionEvent);
}
});
}
}
finally
{
LISTENER_LOCK.unlock();
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.setSize(300, 300);
final MenuItem menuItem = new MenuItem("RED");
menuItem.setLocation(100, 100);
menuItem.setForeground(Color.RED);
menuItem.setBackground(Color.BLACK);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("RED!!!");
}
});
frame.add(menuItem);
frame.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent arg0)
{
menuItem.selected();
}
});
frame.setVisible(true);
}
}