Interpreter-Fehler StackOverflowError

Endymion

Bekanntes Mitglied
Ich versuche momentan Minesweeper nachzuprogrammieren. Momentan bin ich dabei, alle Zellen im Umkreis zu öffnen, wenn sich auf keiner Zelle im Umkreis eine Mine befindet. Wenn ich nun aber eine Zelle öffne, in deren Umgebung sich keine Mine befindet, bekomme ich folgende Fehlermeldung:
Java:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
	at javax.swing.SwingUtilities.computeUnion(Unknown Source)
	at javax.swing.RepaintManager.extendDirtyRegion(Unknown Source)
	at javax.swing.RepaintManager.addDirtyRegion0(Unknown Source)
	at javax.swing.RepaintManager.addDirtyRegion(Unknown Source)
	at javax.swing.JFrame.repaint(Unknown Source)
	at java.awt.Component.repaint(Unknown Source)
	at UI.deleteCellButton(UI.java:74)
	at Cell.openCell(Cell.java:48)
	at Cell.openCell(Cell.java:78)
	at Cell.openCell(Cell.java:78)
	at Cell.openCell(Cell.java:78)
	// und so weiter
Ich würde gerne wissen, was das für eine Exception ist, und wie ich dieses Problem löse.
Folgender Code:
Java:
import java.lang.Math;
import java.util.ArrayList;
public class Main {
	public static final int[][] DIMENSIONS = {
		{
			9, 9, 10
		},
		{
			16, 16, 40
		},
		{
			16, 30, 99
		}
	};
	private int[] options;
    private int difficulty;
    private UI ui;
    private Cell[][] field;
    public void setDifficulty(int difficulty) {
        this.difficulty = difficulty;
    }
    public int getDifficulty() {
    	return difficulty;
    }
    public static void main(String[] args) {
        new Main();
    }
    public Main() {
        ui = new UI(this);
    }
    public void startGame() {
        ui.deleteL();
        ui.deleteEasyButton();
        ui.deleteNormalButton();
        ui.deleteHardButton();
        options = DIMENSIONS[difficulty];
        field = new Cell[options[0]][options[1]];
        for(int x = 0; x < options[0]; x++) {
        	for(int y = 0; y < options[1]; y++) {
        		field[x][y] = new Cell(ui, x, y);
        		ui.addCell(field[x][y]);
        	}
        }
        for(int i = 0; i < options[2]; i++) {
        	setMine();
        }
    }
    private void setMine() {
    	int x = (int) (Math.random() * options[0]);
    	int y = (int) (Math.random() * options[1]);
    	Cell c = field[x][y];
    	if(! c.hasMine()) {
    		c.setMine();
    	}
    	else {
    		setMine();
    	}
    }
    public ArrayList<Cell> getNearCells(int x, int y) {
    	ArrayList<Cell> cells = new ArrayList<Cell>();
    	addCellToList(cells, x - 1, y - 1);
    	addCellToList(cells, x, y - 1);
    	addCellToList(cells, x + 1, y - 1);
    	addCellToList(cells, x - 1, y);
    	addCellToList(cells, x + 1, y);
    	addCellToList(cells, x - 1, y + 1);
    	addCellToList(cells, x, y + 1);
    	addCellToList(cells, x + 1, y + 1);
    	return cells;
    }
    public int getNearMines(int x, int y) {
    	int mines = 0;
    	for(Cell c : getNearCells(x, y)) {
    		if(c.hasMine()) {
    			mines++;
    		}
    	}
    	return mines;
    }
    private void addCellToList(ArrayList<Cell> list, int x, int y) {
    	try {
    		list.add(field[x][y]);
    	}
    	catch(ArrayIndexOutOfBoundsException e) {
    	}
    }
}
Java:
import javax.swing.*;
import java.awt.event.*;
public class UI extends JFrame {
	private Main main;
	private JLabel l;
	private JButton easyButton;
	private JButton normalButton;
	private JButton hardButton;
	int openedCells = 0;
	public Main getMain() {
		return main;
	}
	public void deleteL() {
		remove(l);
		repaint();
	}
	public void deleteEasyButton() {
		remove(easyButton);
		repaint();
	}
	public void deleteNormalButton() {
		remove(normalButton);
		repaint();
	}
	public void deleteHardButton() {
		remove(hardButton);
		repaint();
	}
	public UI(Main newMain) {
		main = newMain;
		setLayout(null);
		setTitle("Mariosweeper");
		setBounds(0, 0, 650, 370);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		l = new JLabel("Schwierigkeitsgrad wählen!");
		l.setBounds(0, 0, 175, 15);
		add(l);
		easyButton = new JButton("leicht");
		normalButton = new JButton("mittel");
		hardButton = new JButton("schwer");
		easyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				main.setDifficulty(0);
				main.startGame();
			}
		});
		normalButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				main.setDifficulty(1);
				main.startGame();
			}
		});
		hardButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				main.setDifficulty(2);
				main.startGame();
			}
		});
		easyButton.setBounds(0, 30, 100, 30);
		normalButton.setBounds(115, 30, 100, 30);
		hardButton.setBounds(230, 30, 100, 30);
		add(easyButton);
		add(normalButton);
		add(hardButton);
		setVisible(true);
	}
	public void addCell(Cell c) {
		JButton b = c.getB();
		add(b);
		b.setBounds(c.getX(), c.getY(), c.WIDTH, c.HEIGHT);
	}
	public void deleteCellButton(Cell c) {
		remove(c.getB());
		repaint();
	}
	public void addCellText(Cell c) {
		JLabel l = c.getL();
		add(l);
		l.setBounds(c.getX(), c.getY(), c.WIDTH, c.HEIGHT);
	}
}
Java:
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class Cell {
	public static final int WIDTH = 20;
	public static final int HEIGHT = 20;
	private UI ui;
	private Main main;
	private int x;
	private int y;
	private int xPosition;
	private int yPosition;
	private boolean hasMine;
	private int openedCells;
	private JButton b;
	private JLabel l;
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public boolean hasMine() {
		return hasMine;
	}
	public JButton getB() {
		return b;
	}
	public JLabel getL() {
		return l;
	}
	public Cell(UI uiNew, int xNew, int yNew) {
		ui = uiNew;
		main = ui.getMain();
		xPosition = xNew;
		yPosition = yNew;
		x = xNew * WIDTH;
		y = yNew * HEIGHT;
		b = new JButton();
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openCell();
			}
		});
		l = new JLabel();
	}
	public void openCell() {
		ui.deleteCellButton(this);
		if(!hasMine) {
			int nearCells = main.getNearMines(xPosition, yPosition);
			l.setText("" + nearCells);
			switch(nearCells) {
			case 0:
				l.setText("");
				break;
			case 1:
				l.setForeground(Color.BLACK);
				break;
			case 2:
				l.setForeground(Color.BLUE);
				break;
			case 3:
				l.setForeground(Color.GREEN);
				break;
			case 4:
				l.setForeground(Color.YELLOW);
				break;
			case 5:
				l.setForeground(Color.ORANGE);
				break;
			case 6:
				l.setForeground(Color.RED);
				break;
			}
			if(nearCells == 0) {
				for(int i = 0; i < main.getNearCells(xPosition, yPosition).size(); i++) {
					if(openedCells <= 100) {
						openCell();
					}
					else {
						openedCells = 0;
					}
				}
			}
		}
		ui.addCellText(this);
	}
	public void setMine() {
		hasMine = true;
		l.setText("x");
		l.setForeground(Color.RED);
	}
}
 
S

SlaterB

Gast
openCell() ruft immer wieder sich selber auf, das geht ein paar mal gut, aber nicht so oft und kann schon nach wenigen ms zuviel sein,
lieber ne Schleife
 

Ähnliche Java Themen

Neue Themen


Oben