Merhaba Arkadaşlar,
Java ödevimi bitirdim hoca sağ click e flag eklememizi istedi google da çok araştırdım ancak bulamadım bilen biri yardım ederse sevinirim.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Board {
private Cell[][] cells;
private int cellID = 0;
private static int side = 20;
private int limit = side-2;
public JPanel addCells(){
JPanel panel = new JPanel(new GridLayout(side,side));
cells = new Cell[side][side];
for(int i = 0; i< side; i++){
for(int j = 0; j<side; j++){
cells[i][j] = new Cell(this);
cells[i][j].setId(getID());
panel.add(cells[i][j].getButton());
}
}
return panel;
}
public void plantMines(){
ArrayList<Integer>loc = generateMinesLocation(16);
for(int i : loc){
getCell(i).setValue(-1);
}
}
// Choose random places for mines
public ArrayList<Integer> generateMinesLocation(int q){
ArrayList<Integer> loc = new ArrayList<Integer>();
int random;
for(int i = 0; i<q;){
random = (int)(Math.random()* (side*side));
if(!loc.contains(random)){
loc.add(random);
i++;
}
}
return loc;
}
public void setCellValues(){
for(int i = 0; i<side; i++){
for(int j = 0; j<side; j++){
if(cells[i][j].getValue() != -1){
if(j>=1 && cells[i][j-1].getValue() == -1) cells[i][j].incrementValue();
if(j<= limit && cells[i][j+1].getValue() == -1) cells[i][j].incrementValue();
if(i>=1 && cells[i-1][j].getValue() == -1) cells[i][j].incrementValue();
if(i<= limit && cells[i+1][j].getValue() == -1) cells[i][j].incrementValue();
if(i>=1 && j>= 1 && cells[i-1][j-1].getValue() == -1) cells[i][j].incrementValue();
if(i<= limit && j<= limit && cells[i+1][j+1].getValue() == -1) cells[i][j].incrementValue();
if(i>=1 && j<= limit && cells[i-1][j+1].getValue() == -1) cells[i][j].incrementValue();
if(i<= limit && j>= 1 && cells[i+1][j-1].getValue() == -1) cells[i][j].incrementValue();
}
}
}
}
public void scanForEmptyCells(){
for(int i = 0; i<side; i++){
for(int j = 0; j<side; j++){
if(!cells[i][j].isNotChecked()){
if(j>=1 && cells[i][j-1].isEmpty()) cells[i][j-1].checkCell();
if(j<= limit && cells[i][j+1].isEmpty()) cells[i][j+1].checkCell();
if(i>=1 && cells[i-1][j].isEmpty()) cells[i-1][j].checkCell();
if(i<= limit && cells[i+1][j].isEmpty()) cells[i+1][j].checkCell();
if(i>=1 && j>= 1 && cells[i-1][j-1].isEmpty()) cells[i-1][j-1].checkCell();
if(i<= limit && j<= limit && cells[i+1][j+1].isEmpty()) cells[i+1][j+1].checkCell();
if(i>=1 && j<= limit && cells[i-1][j+1].isEmpty()) cells[i-1][j+1].checkCell();
if(i<= limit && j>= 1 && cells[i+1][j-1].isEmpty()) cells[i+1][j-1].checkCell();
}
}
}
}
public int getID(){
int id = cellID;
cellID++;
return id;
}
public Cell getCell(int id){
for(Cell[] a : cells){
for(Cell b : a){
if(b.getId() == id) return b;
}
}
return null;
}
public void fail(){
for(Cell[] a : cells){
for(Cell b : a){
b.reveal();
}
}
}
public void setBoard(){
JFrame frame = new JFrame();
frame.setTitle("~~~~~~~~~~~~~ BOOOM KORKTUN MU ~~~~~~~~~~~~~");
frame.setSize(400,400);
frame.setLocation(100,100);
frame.setLayout(new FlowLayout());
JLabel zorluk = new JLabel(/*"Free Button"*/);
JButton denex = new JButton("Yenile");
denex.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
Board board4 = new Board();
board4.setBoard();
}
});
JButton deneme = new JButton("Kolay");
deneme.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent t) {
Board.side=15;
frame.setVisible(false);
Board board = new Board();
board.setBoard();
}
});
JButton deneme1 = new JButton("Orta");
deneme1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent g) {
Board.side=10;
frame.dispose();
Board board2 = new Board();
board2.setBoard();
}
});
JButton deneme2 = new JButton("Zor");
deneme2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent s) {
Board.side=5;
frame.dispose();
Board board = new Board();
board.setBoard();
}
});
frame.add(zorluk);
frame.add(denex);
frame.add(deneme);
frame.add(deneme1);
frame.add(deneme2);
frame.add(addCells());
plantMines();
setCellValues();
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//frame.setResizable(false);
frame.setVisible(true);
}
}---*-*-*-*-*-*-
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Cell implements ActionListener{
private JButton button;
private Board board;
private int value;
private int id;
private boolean notChecked;
public Cell(Board board) { //hücre
button = new JButton();
button.addActionListener(this);
button.setPreferredSize(new Dimension(18,18));
button.setMargin(new Insets(0,0,0,0));
this.board = board;
notChecked = true;
}
public JButton getButton() {
return button;
}
public int getValue() {
return value;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setValue(int value) {
this.value = value;
}
public void displayValue(){
if(value==-1){
button.setText("\u2600");
button.setBackground(Color.BLUE);
}else if(value!=0){
button.setText(String.valueOf(value));
}
}
public void checkCell(){
button.setEnabled(false);
displayValue();
notChecked = false;
if(value == 0) board.scanForEmptyCells();
if(value == -1) board.fail();
}
public void incrementValue(){
value++;
}
public boolean isNotChecked(){
return notChecked;
}
public boolean isEmpty(){
reveal();
return isNotChecked() && value==0;
}
public void reveal(){ // boş buttonları açığa çıkarma
displayValue();
button.setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent e) {
checkCell();
}
}
---------------------*-*-*-*-*-
public class Main{
public static void main(String[] args){
Board board = new Board();
board.setBoard();
}
}