1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Panelcentre extends JPanel Panelcentre {
// Permettra de dessiner des cercles.
private Ellipse2D.Float ellipse = new Ellipse2D.Float();
/** {@InheritDoc}
*/
@Override protected void paintComponent(Graphics g) {
// Appel a la super methode pour dessiner les bases du panel.
super.paintComponent(g);
Dimension size = getSize();
// Un panel ca peut avoir une bordure, voir la classe Border.
Insets insets = getInsets();
int canvasWidth = size.width - (insets.left + insets.right);
int canvasHeight = size.height - (insets.top + insets.bottom);
// On se restreint a la zone de dessin.
Graphics2D g2d = (Graphics2D)g.create(insets.left, insets.top, canvasWidth, canvasHeight);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try {
// On dessine le fond du panel.
// On dessine le labyrinthe.
[...]
// On dessine les fourmis.
for (Ant ant : antList) {
float centerX = optionnalXMargin + width * (ant.x - simulationArea.minX) / (float)simulationArea.width; // (ou simulationArea.maxX - simulationArea.minX). Generalement simulationArea.minX == 0.
float centerY = optionalYMargin + height * (ant.y - simulationArea.minY) / (float)simulationArea.height; // (ou simulationArea.maxY - simulationArea.minY). Generalement simulationArea.minY == 0.
ellipse.setFrameFromCenter(centerX, centerY, centerX + 2, centerY + 2);
// Et pis si on voulait faire un affichage optimise on verifierai que l'ellipse est bien affichee dans la zone de clip du Graphics avant de la dessiner
g2d.setPaint(Color.BLACK); // ou g2d.setColor(Color.BLACK);
g2d.fill(ellipse);
}
}
finally {
g2d.dispose();
}
} |
Partager