91 lines
2.1 KiB
Java
91 lines
2.1 KiB
Java
package matrix2;
|
|
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
|
|
public class Lettera {
|
|
|
|
private final BufferedImage image;
|
|
private final Point position;
|
|
private float alpha = 1.0f;
|
|
|
|
public Lettera(char c, int size, Point position) {
|
|
this.position = position;
|
|
this.image = creaLetteraImage(c, size, Color.GREEN);
|
|
}
|
|
|
|
private BufferedImage creaLetteraImage(char c, int size, Color color) {
|
|
BufferedImage img = new BufferedImage(
|
|
size, size,
|
|
BufferedImage.TYPE_INT_ARGB
|
|
);
|
|
|
|
Graphics2D g2 = img.createGraphics();
|
|
|
|
g2.setRenderingHint(
|
|
RenderingHints.KEY_TEXT_ANTIALIASING,
|
|
RenderingHints.VALUE_TEXT_ANTIALIAS_ON
|
|
);
|
|
|
|
Font font = new Font("Monospaced", Font.BOLD, size);
|
|
g2.setFont(font);
|
|
g2.setColor(color);
|
|
|
|
FontMetrics fm = g2.getFontMetrics();
|
|
int x = (size - fm.charWidth(c)) / 2;
|
|
int y = ((size - fm.getHeight()) / 2) + fm.getAscent();
|
|
|
|
g2.drawString(String.valueOf(c), x, y);
|
|
g2.dispose();
|
|
|
|
return img;
|
|
}
|
|
|
|
public void draw(Graphics2D g2) {
|
|
Composite old = g2.getComposite();
|
|
|
|
g2.setComposite(
|
|
AlphaComposite.getInstance(
|
|
AlphaComposite.SRC_OVER, alpha
|
|
)
|
|
);
|
|
|
|
g2.drawImage(
|
|
image,
|
|
(int) position.x,
|
|
(int) position.y,
|
|
null
|
|
);
|
|
|
|
g2.setComposite(old);
|
|
}
|
|
|
|
// ===== LOGICA =====
|
|
|
|
public void dissolve(float amount) {
|
|
alpha -= amount;
|
|
if (alpha < 0f) alpha = 0f;
|
|
}
|
|
|
|
public boolean isDead() {
|
|
return alpha <= 0f;
|
|
}
|
|
|
|
public BufferedImage getImage() {
|
|
return image;
|
|
}
|
|
|
|
public Point getPosition() {
|
|
return position;
|
|
}
|
|
public Point getPositionGlobal(Point colonnaPos) {
|
|
// colonnaPos = posizione (x,y) della colonna sullo schermo
|
|
return new Point(colonnaPos.x + this.position.x,
|
|
colonnaPos.y + this.position.y);
|
|
}
|
|
|
|
public int getSize() {
|
|
return image.getWidth();
|
|
}
|
|
}
|