Files
racing-game/src/Dashboard.java

78 lines
1.7 KiB
Java

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Supplier;
import javax.swing.JButton;
import javax.swing.JLabel;
/**
* bouton en bas, couleur choisi de la fenetre
* affiche: carburant restant et tour de piste
*/
public class Dashboard extends GameView
{
private final JLabel label;
private final JButton button;
private final Car car;
private static boolean isPaused = false;
public Dashboard(Car car, String title, Supplier<Boolean> fn, int width, int height, int x, int y)
{
super("Dashboard: " + title, width, height, x, y);
this.car = car;
this.label = new JLabel();
this.button = new JButton();
// ajout background
frame.setBackground(car.getColor());
init(fn);
// mettre un layout (text au centre et bouton en bas)
// this.setLayout(new BorderLayout());
// this.add(label, BorderLayout.CENTER);
// this.add(button, BorderLayout.SOUTH);
this.setLayout(new BorderLayout());
this.add(label, BorderLayout.CENTER);
this.add(button, BorderLayout.SOUTH);
}
// fonction uniquement pour l'affichage dynamique
private void updateButtonText()
{
button.setText(isPaused ? "En Pause" : "En Cours");
}
private void init(Supplier<Boolean> fn)
{
updateButtonText();
// classe pour le bouton
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e)
{
isPaused = fn.get();
updateButtonText();
}
};
this.button.addMouseListener(ma);
}
@Override
public void run()
{
label.setText(
"<html><table><tr><td>Carburant Restant: " + car.getFuel()
+ "</td></tr><tr><td>Nombre de Tour: " + car.getRound()
+ "</td></tr></table></html>"
);
}
}