Reputation:
This is probably an elementary question. However, I have completed reading the 12th Chapter of Java Programming for the Absolute Beginner and have approached some sample code. I get a compiler error when I try to compile this sample code program. I have all the related programs located within the same package, so this is not the problem.
The error occurs at this line:
minefield.addMineFieldListener(this);
It states:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
- Erroneous sym type: MineField.addMineFieldListener at MinePatrol.<init>
(MinePatrol.java:21)
My main question is: Why is this occuring?
Thank you very much for your time and cooperation reagrding this matter.
HERE IS THE CODE FOR MinePatrol.java:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.net.URL;
import java.net.MalformedURLException;
public class MinePatrol extends GUIFrame
implements MineFieldListener, ActionListener {
protected MineField minefield;
protected Button resetButton;
protected Label flagCountLabel;
public MinePatrol() {
super("MinePatrol");
minefield = new MineField(10, 10, 10);
minefield.addMineFieldListener(this);
add(minefield, BorderLayout.CENTER);
MediaTracker mt = new MediaTracker(this);
Image[] imgs = {
Toolkit.getDefaultToolkit().getImage("flag.gif"),
Toolkit.getDefaultToolkit().getImage("mine.gif"),
Toolkit.getDefaultToolkit().getImage("explode.gif") };
for (int i=0; i < imgs.length; i++) {
mt.addImage(imgs[i], i);
}
try {
mt.waitForAll();
} catch (InterruptedException e) {}
minefield.setImages(imgs[0], imgs[1], imgs[2]);
try {
minefield.setAudioClips(
Applet.newAudioClip(new URL("file:reveal.wav")),
Applet.newAudioClip(new URL("file:flag.wav")),
Applet.newAudioClip(new URL("file:unflag.wav")),
Applet.newAudioClip(new URL("file:explode.wav")));
} catch (MalformedURLException e) {}
flagCountLabel = new Label("Flags: 10", Label.CENTER);
add(flagCountLabel, BorderLayout.NORTH);
resetButton = new Button("Reset");
resetButton.addActionListener(this);
add(resetButton, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String args[]) {
new MinePatrol();
}
public void mineFieldSolved(MineFieldEvent e) {
flagCountLabel.setText("You Win!");
}
public void mineFieldRandomized(MineFieldEvent e) {
flagCountLabel.setText("Flags: " + minefield.getMineCount());
}
public void mineFieldDetonated(MineFieldEvent e) {
flagCountLabel.setText("You exploded into tiny bits... RIP");
}
public void mineFieldFlagCountChanged(MineFieldEvent e) {
flagCountLabel.setText("Flags: "
+ (minefield.getMineCount() - minefield.getFlagCount()));
}
public void actionPerformed(ActionEvent e) {
minefield.randomize();
}
}
Here is the code for MineField.java
import java.awt.*;
import java.util.*;
import java.applet.AudioClip;
public class MineField extends Panel
implements MineCellListener {
protected int rows, cols, mines, flagged, revealed;
protected AudioClip revealClip, flagClip, unflagClip, explodeClip;
protected MineCell[][] cells;
//keeps track of MineCell indices
protected Hashtable pointIndex;
protected Vector listeners;
public MineField(int nRows, int nCols, int nMines) {
rows = nRows > 0 ? nRows : 1;
cols = nCols > 0 ? nCols : 1;
cells = new MineCell[rows][cols];
mines = nMines >= 0 && nMines < rows * cols ? nMines : 1;
pointIndex = new Hashtable(rows * cols);
setLayout(new GridLayout(rows, cols));
for (int r=0; r < cells.length; r++) {
for (int c=0; c < cells[r].length; c++) {
cells[r][c] = new MineCell();
cells[r][c].addMineCellListener(this);
//Points use (x, y) coordinates to x=c and y=r
pointIndex.put(cells[r][c], new Point(c, r));
cells[r][c].setSize(25, 25);
add(cells[r][c]);
}
}
listeners = new Vector();
randomizeField(false);
}
protected void randomizeField(boolean fireEvent) {
Random rand = new Random();
int index;
//initialize empty
for (int r=0; r < cells.length; r++) {
for (int c=0; c < cells[r].length; c++) {
cells[r][c].resetContents(MineCell.EMPTY);
}
}
//randomly place all mines
for (int m=0; m < mines; m++) {
do {
index = rand.nextInt(rows * cols);
}
while (cells[index / cols][index % cols].getContents() != MineCell.EMPTY);
cells[index / cols][index % cols].resetContents(MineCell.MINE);
}
setNumberClues();
flagged = revealed = 0;
//does not fire flagCountChanged, only mineFieldRandomized
if (fireEvent) (new EventThread(new MineFieldEvent(this,
MineFieldEvent.RANDOMIZED))).start();
}
public void randomize() {
randomField(true);
}
public int getFlagCount() {
return flagged;
}
public int getMineCount() {
return mines;
}
//counts and sets the number of mines surrounding each cell
protected void setNumberClues() {
int nMines;
for (int r=0; r < cells.length; r++) {
for (int c=0; c < cells[r].length; c++) {
if (cells[r][c].getContents() != MineCell.MINE) {
nMines = 0;
//count the number of mines surrounding this cell
for (int dr = r - 1; dr <= r + 1; dr++) {
//prevent ArrayIndexOutOfBoundsException
//continue puts control back to the beginning of the loop
if (dr < 0 || dr >= cells.length) continue;
for (int dc = c -1; dc <= c + 1; dc++) {
if (dc < 0 || dc >= cells[dr].length) continue;
if (cells[dr][dc].getContents() == MineCell.MINE) nMines++;
}
}
cells[r][c].resetContents(nMines);
}
}
}
}
public class MineFieldEvent extends java.util.EventObject {
protected int eventID;
//event id constants
public final static int SOLVED = 0,
RANDOMIZED = 1,
DETONATED = 2,
FLAG_COUNT_CHANGED = 3;
public MineFieldEvent(Object source, int id) {
super(source);
eventID = id;
}
public int getID() {
return eventID;
}
}
protected class EventThread extends Thread {
MineFieldEvent e;
EventThread(MineFieldEvent mfe) {
e = mfe;
}
public void addMineFieldListener(MineFieldListener mfl) {
listeners.addElement(mfl);
}
public void removeMineFieldListener(MineFieldListener mfl) {
listeners.removeElement(mfl);
}
public void run() {
switch(e.getID()) {
case MineFieldEvent.SOLVED:
for (int i=0; i < listeners.size(); i++) {
((MineFieldListener)listeners.elementAt(i)).mineFieldSolved(e);
}
break;
case MineFieldEvent.RANDOMIZED:
for (int i=0; i < listeners.size(); i++) {
((MineFieldListener)listeners.elementAt(i)).mineFieldRandomized(e);
}
break;
case MineFieldEvent.DETONATED:
for (int i=0; i < listeners.size(); i++) {
((MineFieldListener)listeners.elementAt(i)).mineFieldDetonated(e);
}
break;
case MineFieldEvent.FLAG_COUNT_CHANGED:
for (int i=0; i < listeners.size(); i++) {
((MineFieldListener)listeners.elementAt(i)).mineFieldFlagCountChanged(e);
}
break;
}
}
}
public void setImages(Image f, Image m, Image e) {
MineCell.setImages(f, m, e);
}
public void setAudioClips(AudioClip r, AudioClip f, AudioClip u,
AudioClip e) {
revealClip = r; flagClip = f; unflagClip = u; explodeClip = e;
}
/* Reveals areas mine-free minecells */
protected void showSafeArea(MineCell start) {
//get the point index for the starting cell
Point p = (Point)pointIndex.get(start);
for (int r = p.y - 1; r <= p.y + 1; r++) {
if (r < 0 || r >= cells.length) continue;
for (int c = p.x - 1; c <= p.x + 1; c++) {
if (c < 0 || c >= cells[r].length || !cells[r][c].isHidden())
continue;
if (cells[r][c].isFlagged()) {
flagged--;
(new EventThread(new MineFieldEvent(this,
MineFieldEvent.FLAG_COUNT_CHANGED))).start();
}
cells[r][c].setHidden(false);
revealed++;
if(cells[r][c].getContents() == MineCell.EMPTY)
showSafeArea(cells[r][c]);
}
}
}
protected void checkIfSolved() {
if (flagged + revealed == rows * cols) {
//solved if we get here.
revealAll();
(new EventThread(new MineFieldEvent(this,
MineFieldEvent.SOLVED))).start();
}
}
public void revealAll() {
for (int r=0; r < cells.length; r++) {
for (int c =0; c < cells.length; c++) {
cells[r][c].setHidden(false);
}
}
}
public void mineCellRevealed(MineCellEvent e) {
if (revealClip != null) revealClip.play();
revealed++;
if ((MineCell)e.getSource()).getContents() == MineCell.EMPTY)
showSafeArea((MineCell)e.getSource());
checkIfSolved();
}
public void mineCellDetonated(MineCellEvent e) {
//game over show all
if (explodeClip != null) explodeClip.play();
revealAll();
(new EventThread(new MineFieldEvent(this,
MineFieldEvent.DETONATED))).start();
}
public void mineCellFlagged(MineCellEvent e) {
if (flagged >= mines) {
((MineCell)e.getSource()).getHidden(true);
return;
}
if (flagClip != null) flagClip.play();
flagged++;
(new EventThread(new MineFieldEvent(this,
MineFieldEvent.FLAG_COUNT_CHANGED))).start();
checkIfSolved();
}
public void mineCellUnflagged(MineCellEvent e) {
if (unflagClip != null) unflagClip.play();
flagged--;
(new EventThread(new MineFieldEvent(this,
MineFieldEvent.FLAG_COUNT_CHANGED))).start();
}
}
Upvotes: 0
Views: 624
Reputation: 21748
This may be the NetBeans bug 199293. It is fixed now but not yet old. If you use NetBeans, check maybe you need to update. The error message Uncompilable source code - Erroneous sym type matches and I do not see anything in your code to result an error message so wise. Try to build with Eclipse or Ant.
Upvotes: 1