48 lines
1.1 KiB
Java
48 lines
1.1 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
public class Q2_Damier{
|
|
public static void main(String[] args) {
|
|
if (args.length < 2){
|
|
System.out.println("Usage: java Q2_Damier <columns> <rows>");
|
|
return 1;
|
|
}
|
|
Fenetre fenetre = new Fenetre(columns, rows);
|
|
}
|
|
}
|
|
|
|
class Fenetre{
|
|
private int colums;
|
|
private int rows;
|
|
private static Color color1 = new Color(0,0,0);
|
|
private static Color color2 = new Color(255,255,255);
|
|
|
|
public Fenetre(int columns, int rows){
|
|
this.columns = columns;
|
|
this.rows = rows;
|
|
}
|
|
public void creerFenetre(){
|
|
GridLayout grille = new GridLayout(this.rows, this.columns);
|
|
|
|
JFrame fenetre = new JFrame();
|
|
fenetre.setSize(500, 300);
|
|
fenetre.setLocation(0, 0);
|
|
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
fenetre.setLayout(grille);
|
|
|
|
for(int y = 0; y < this.rows; y++){
|
|
for(int x = 0; x < this.colums; x++){
|
|
JButton case = new JButton();
|
|
if ((x%2==0 && y%2==0) || (x%2==1 && y%2==1)){
|
|
case.setBackground(Fenetre.color1);
|
|
}
|
|
else{
|
|
case.setBackground(Fenetre.color2);
|
|
}
|
|
fenetre.add(case);
|
|
}
|
|
}
|
|
fenetre.setVisible(true);
|
|
}
|
|
}
|