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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
   | public class ExempleMonopoly extends JPanel {
 
	private final static int CASECOINS=4;// une case coin fait 2 fois la hauteur d'une case de bord vertical et 2 fois la largeur d'une case de bord horizontal
	private final static int NBCASES = 10 + CASECOINS;
 
	public ExempleMonopoly() {
		super();
	}
 
	public static void main(String[] args) {
 
		JFrame frame = new JFrame("Monopoly");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new ExempleMonopoly();
 
 
		frame.getContentPane().add(panel);
 
		frame.setSize(400, 400);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
 
	}
 
	@Override
	public void paint(Graphics g) {
 
		super.paint(g);
 
		g = g.create(); // il vaut mieux travailler sur une copie, pour éviter de laisser le contexte graphique dans un état modifié
 
		int width = getWidth()-1;
		int height = getHeight()-1;
 
		int largeurCase = width/NBCASES;
		int hauteurCase = height/NBCASES;
 
		// on compense les arrondis dans la derrermination des largeurs et hauteurs de cases
		width = largeurCase*NBCASES;
		height = hauteurCase*NBCASES; 
 
		// on centre pour prendre comptre la différence entre largeurxhauteur du composant et celles compensées
		AffineTransform centrage = AffineTransform.getTranslateInstance(getWidth()/2-width/2, getHeight()/2-height/2);
		((Graphics2D)g).setTransform(centrage);
 
		// ligne du bas
		g.setColor(Color.BLACK);
		dessineLigne(g, 0, height, largeurCase, hauteurCase*2);
 
		// ligne de gauche
		AffineTransform rotation1 = new AffineTransform(centrage);
		rotation1.concatenate(AffineTransform.getRotateInstance(Math.PI/2, height/2, width/2));
		((Graphics2D)g).setTransform(rotation1);		
		g.setColor(Color.RED);
		dessineLigne(g, height/2-width/2, width/2+height/2, hauteurCase, largeurCase*2);
 
		// ligne d'en haut
		AffineTransform rotation2 = new AffineTransform(centrage);
		rotation2.concatenate(AffineTransform.getRotateInstance(Math.PI, height/2, width/2));
		((Graphics2D)g).setTransform(rotation2);		
		g.setColor(Color.BLUE);
		dessineLigne(g, height-width, width, largeurCase, hauteurCase*2);
 
		// ligne de droite
		AffineTransform rotation3 = new AffineTransform(centrage);
		rotation3.concatenate(AffineTransform.getRotateInstance(-Math.PI/2, height/2, width/2));
		((Graphics2D)g).setTransform(rotation3);		
		g.setColor(Color.GREEN.darker());
		dessineLigne(g, width/2-height/2, width+(width-height)/2, hauteurCase, largeurCase*2);
 
		g.dispose();
 
	}
 
	private void dessineLigne(Graphics g, int offset, double hauteur, int largeurCase, int hauteurCase) {
		// case du coin
		g.drawRect(offset, (int)(hauteur-hauteurCase), largeurCase*2, hauteurCase);
		// autres cases
		offset += largeurCase*2;
		for(int i=NBCASES-CASECOINS; i>0; i--) {
			g.drawRect(offset, (int)(hauteur-hauteurCase), largeurCase, hauteurCase);
			offset+=largeurCase;
		}
	}
 
} | 
Partager