2023-05-21 15:13:45 -07:00
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.*;
|
|
|
|
import javax.swing.*;
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
// The face at the top of the game
|
2023-05-21 15:13:45 -07:00
|
|
|
public class Face extends JComponent {
|
2023-05-25 19:44:14 -07:00
|
|
|
// The width and height
|
2023-05-21 15:13:45 -07:00
|
|
|
public static final int SIZE = 26;
|
2023-05-25 19:44:14 -07:00
|
|
|
// The indexes of each face in the array
|
2023-05-21 15:13:45 -07:00
|
|
|
public static final int HAPPY_INDEX = 0;
|
|
|
|
public static final int SHOCKED_INDEX = 1;
|
|
|
|
public static final int DEAD_INDEX = 2;
|
|
|
|
public static final int COOL_INDEX = 3;
|
|
|
|
public static final int PRESSED_INDEX = 4;
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
// The main game canvas to refer back to
|
2023-05-21 15:13:45 -07:00
|
|
|
private final Canvas GAME_CANVAS;
|
2023-05-25 19:44:14 -07:00
|
|
|
|
|
|
|
private Image[] faces;
|
|
|
|
// The current face index
|
2023-05-21 15:13:45 -07:00
|
|
|
private int face;
|
2023-05-25 19:44:14 -07:00
|
|
|
// The last face index, before being pressed, in order to revert on unpress
|
2023-05-21 15:13:45 -07:00
|
|
|
private int unpressedFace;
|
2023-05-25 19:44:14 -07:00
|
|
|
// Whether the face is being pressed right now
|
2023-05-21 15:13:45 -07:00
|
|
|
private boolean pressed;
|
2023-05-25 19:44:14 -07:00
|
|
|
// Whether the mouse is over the face
|
2023-05-21 15:13:45 -07:00
|
|
|
private boolean mouseOver;
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
public Face(Canvas gameCanvas) {
|
2023-05-21 15:13:45 -07:00
|
|
|
GAME_CANVAS = gameCanvas;
|
|
|
|
face = HAPPY_INDEX;
|
|
|
|
pressed = false;
|
|
|
|
mouseOver = false;
|
|
|
|
setPreferredSize(new Dimension(SIZE, SIZE));
|
2023-05-25 19:44:14 -07:00
|
|
|
// We want to show the pressed sprite only if the mouse is down while
|
|
|
|
// the mouse is over the face, reverting back when it's no longer over
|
|
|
|
// the face but coming back as soon as the mouse comes back
|
2023-05-24 17:34:20 -07:00
|
|
|
addMouseListener(new MouseAdapter() {
|
2023-05-25 19:44:14 -07:00
|
|
|
// The only way to initiate a pressed face state is when the mouse
|
|
|
|
// is over it and is pressed down
|
2023-05-21 15:13:45 -07:00
|
|
|
@Override
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
pressed = true;
|
|
|
|
unpressedFace = face;
|
|
|
|
setFace(PRESSED_INDEX);
|
|
|
|
}
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
// If the face recieves a mouseReleased event while the mouse is
|
|
|
|
// over it, that means that it was a completed click, so restart the
|
|
|
|
// game on the canvas, also resetting back to the happy face that
|
|
|
|
// should be shown at the start of every game
|
2023-05-21 15:13:45 -07:00
|
|
|
@Override
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
pressed = false;
|
|
|
|
if (mouseOver) {
|
|
|
|
setFace(HAPPY_INDEX);
|
|
|
|
GAME_CANVAS.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
// If it was previously being pressed, reshow the pressed sprite as
|
|
|
|
// soon as mouse enters
|
2023-05-21 15:13:45 -07:00
|
|
|
@Override
|
|
|
|
public void mouseEntered(MouseEvent e) {
|
|
|
|
mouseOver = true;
|
|
|
|
if (pressed)
|
|
|
|
setFace(PRESSED_INDEX);
|
|
|
|
}
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
// Temporarily show the unpressed face, but keep the pressed boolean
|
|
|
|
// true in case the mouse reenters
|
2023-05-21 15:13:45 -07:00
|
|
|
@Override
|
|
|
|
public void mouseExited(MouseEvent e) {
|
|
|
|
mouseOver = false;
|
|
|
|
if (pressed)
|
|
|
|
setFace(unpressedFace);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
public void setImages(Image[] faces) {
|
|
|
|
this.faces = faces;
|
|
|
|
}
|
|
|
|
|
2023-05-21 15:13:45 -07:00
|
|
|
public void setFace(int face) {
|
|
|
|
this.face = face;
|
|
|
|
repaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-05-24 17:34:20 -07:00
|
|
|
public void paintComponent(Graphics gr) {
|
|
|
|
super.paintComponent(gr);
|
|
|
|
Graphics2D g = (Graphics2D) gr;
|
|
|
|
|
2023-05-25 19:44:14 -07:00
|
|
|
g.drawImage(faces[face], 0, 0, this);
|
2023-05-21 15:13:45 -07:00
|
|
|
}
|
|
|
|
}
|