Plateau graphique v2 *score,tourJoueur,tailleTour,selection* + maj makefile,readme
This commit was merged in pull request #11.
This commit is contained in:
90
fr/iut_fbleau/Avalam/ui/PieceButton.java
Normal file
90
fr/iut_fbleau/Avalam/ui/PieceButton.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package fr.iut_fbleau.Avalam.ui;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* La classe <code>PieceButton</code> représente graphiquement une tour Avalam.
|
||||
* Il s'agit d'un bouton rond :
|
||||
*
|
||||
* <ul>
|
||||
* <li>coloré selon le joueur</li>
|
||||
* <li>affichant sa hauteur</li>
|
||||
* <li>avec un effet de survol visuel</li>
|
||||
* </ul>
|
||||
*
|
||||
* Ce composant sert d'interaction principale pour cliquer les pions.
|
||||
*
|
||||
* @author
|
||||
* @version 1.0
|
||||
*/
|
||||
public class PieceButton extends JButton {
|
||||
|
||||
private Color color;
|
||||
private int height;
|
||||
private boolean hover = false;
|
||||
|
||||
/** Position de la tour sur la grille. */
|
||||
public final int row, col;
|
||||
|
||||
/**
|
||||
* Constructeur.
|
||||
*
|
||||
* @param c couleur graphique du pion
|
||||
* @param height hauteur de la tour
|
||||
* @param row ligne du pion
|
||||
* @param col colonne du pion
|
||||
*/
|
||||
public PieceButton(java.awt.Color c, int height, int row, int col) {
|
||||
this.color = c;
|
||||
this.height = height;
|
||||
this.row = row;
|
||||
this.col = col;
|
||||
|
||||
setBorderPainted(false);
|
||||
setContentAreaFilled(false);
|
||||
setFocusPainted(false);
|
||||
setOpaque(false);
|
||||
|
||||
addMouseListener(new java.awt.event.MouseAdapter() {
|
||||
@Override public void mouseEntered(java.awt.event.MouseEvent e) { hover = true; repaint(); }
|
||||
@Override public void mouseExited (java.awt.event.MouseEvent e) { hover = false; repaint(); }
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Dessine le pion rond ainsi que son chiffre au centre.
|
||||
*/
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
Graphics2D g2 = (Graphics2D) g.create();
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
// Couleur foncée lorsque survolé
|
||||
g2.setColor(hover ? color.darker() : color);
|
||||
g2.fillOval(0, 0, getWidth(), getHeight());
|
||||
|
||||
// Hauteur affichée au centre
|
||||
g2.setColor(java.awt.Color.BLACK);
|
||||
g2.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
|
||||
String txt = String.valueOf(height);
|
||||
FontMetrics fm = g2.getFontMetrics();
|
||||
|
||||
int x = (getWidth() - fm.stringWidth(txt)) / 2;
|
||||
int y = (getHeight() + fm.getAscent()) / 2 - 3;
|
||||
|
||||
g2.drawString(txt, x, y);
|
||||
g2.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rend le bouton réellement rond (zone cliquable circulaire).
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(int x, int y) {
|
||||
double dx = x - getWidth()/2.0;
|
||||
double dy = y - getHeight()/2.0;
|
||||
return dx*dx + dy*dy <= (getWidth()/2.0)*(getWidth()/2.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user