public interface Clickable extends Paintable {
boolean isResponsibleForPoint(Point position);
void addMouseListener(MouseListener l);
void removeMouseListener(MouseListener l);
void fireMouseEvent(MouseEvent e);
}
public class Smiley implements Clickable {
private List<MouseListener> listenerList;
private int x;
private int y;
private Color mainColor = Color.YELLOW;
private final int DIAMETER = 50;
public Smiley(int x, int y) {
this.x = x;
this.y = y;
listenerList = new ArrayList<MouseListener>();
}
public void setRandomColor() {
Random random = new Random();
mainColor = new Color(random.nextInt(256), random.nextInt(256),
random.nextInt(256));
}
public void paintObject(Graphics2D g) {
g.setColor(mainColor);
g.fillOval(x, y, DIAMETER, DIAMETER);
g.setColor(Color.BLACK);
g.fillOval(x + 15, y + 15, 8, 8);
g.fillOval(x + 30, y + 15, 8, 8);
g.draw(new Arc2D.Double(x + 10, y + 10, 30, 30, 190, 160, Arc2D.OPEN));
}
public boolean isResponsibleForPoint(Point position) {
float r = DIAMETER / 2;
float center_x = x + r;
float center_y = y + r;
float dist = (float) Math.sqrt(Math.pow(position.x - center_x, 2)
+ Math.pow(position.y - center_y, 2));
return dist < r;
}
public void addMouseListener(MouseListener l) {
listenerList.add(l);
}
public void removeMouseListener(MouseListener l) {
listenerList.remove(l);
}
public void fireMouseEvent(MouseEvent e) {
for (MouseListener currentListener : listenerList) {
switch (e.getID()) {
case MouseEvent.MOUSE_CLICKED:
currentListener.mouseClicked(e);
break;
case MouseEvent.MOUSE_ENTERED:
currentListener.mouseEntered(e);
break;
case MouseEvent.MOUSE_EXITED:
currentListener.mouseExited(e);
break;
case MouseEvent.MOUSE_PRESSED:
currentListener.mousePressed(e);
break;
case MouseEvent.MOUSE_RELEASED:
currentListener.mouseReleased(e);
}
}
}
}
public class GeneralPaintPanel extends JPanel {
private List<Clickable> clickables = new ArrayList<Clickable>();
public GeneralPaintPanel() {
addMouseListener(new MouseListener() {
private void dispatchEvent(MouseEvent e) {
for (Clickable clickable : clickables) {
if (clickable.isResponsibleForPoint(e.getPoint())) {
MouseEvent event = new MouseEvent((Component) e.getSource(), e.getID(),
e.getWhen(), e.getModifiers(), e.getX(),
e.getY(), e.getClickCount(),
e.isPopupTrigger(), e.getButton());
event.setSource(clickable);
clickable.fireMouseEvent(event);
}
}
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
dispatchEvent(e);
}
@Override
public void mousePressed(MouseEvent e) {
dispatchEvent(e);
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
dispatchEvent(e);
}
});
}
public void addPaintable(Clickable paintable) {
clickables.add(paintable);
repaint();
}
public void removePaintable(Paintable paintable) {
clickables.remove(paintable);
repaint();
}
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D) graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Clickable clickable : clickables) {
clickable.paintObject(g);
}
}
}
public class GeneralMainPanel extends JPanel {
public GeneralMainPanel() {
setLayout(new BorderLayout());
final GeneralPaintPanel paintPanel = new GeneralPaintPanel();
paintPanel.setPreferredSize(new Dimension(400, 400));
add(paintPanel, BorderLayout.CENTER);
JButton button = new JButton("Paint object");
add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x = (int) (Math.random() * (paintPanel.getWidth() - 100));
int y = (int) (Math.random() * (paintPanel.getHeight() - 100));
Smiley smiley = new Smiley(x, y);
smiley.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
Smiley smiley = (Smiley) e.getSource();
smiley.setRandomColor();
}
});
paintPanel.addPaintable(smiley);
}
});
}
}