Removed Areas > Java
java applet problems
alexp:
I'm sooooo close to finishing this Java applet I'm working on. I only have three things which I'm struggling to get to work.
#1 The solution
The user presses a button and the applet should run through the solution to the puzzle. I kinda have it working in so much as the applet seems to run through the solution but the display doesn't get refreshed until the solution is finished.
--- --- public void solution() {
int solutionSteps = 9;
for (int i = 0; i < solutionSteps; i++) {
doSolutionStep(i);
try {Thread.sleep(1500);}
catch (Exception e) {e.printStackTrace();}
}
}
public void doSolutionStep(int step) {
int y = 0, z = 0;
switch(step) {
case 1 : {
for (int i = z; i < theJeeps.length; i++) {
forward(theJeeps[i]);
try {Thread.sleep(1500);}
catch (Exception e) {e.printStackTrace();}
}
for (int x = 1; x < theJeeps.length; x++) {
theJeeps[y].transfer(theJeeps[x]);
}
home(theJeeps[y]);
y++;
z++;
break;
}
case 2 : {
for (int i = z; i < theJeeps.length; i++) {
forward(theJeeps[i]);
try {Thread.sleep(1500);}
catch (Exception e) {e.printStackTrace();}
}
for (int x = 1; x < theJeeps.length; x++) {
theJeeps[y].transfer(theJeeps[x]);
}
home(theJeeps[y]);
y++;
z++;
break;
}I don't see why the display shouldn't get refreshed as I'm calling the forward method which refreshes the display so I assumed that everytime the forward method runs the display would get refreshed.
#2 When the user completes the puzzle it's supposed to pop up a dialog box with a congratulatory message. However I can't seem to figure out why it's not working.
--- --- protected boolean forward() {
this.desert = desert;
if (won()) {
desert.gameWon();
return false;
}
else {
if (canGoForward()) {
distance += 40;
petrol -= 1;
return true;
}
else {return false;}
}
}
private boolean won() {
if (distance == (9 * 40)) {
return true;
}
else {return false;}
}The IDE I'm using (eclipse) flags up the line desert.gameWon(); with the following error: The assignment to variable desert has no effect which I guess is why it's not working but I don't really understand why/how.
#3 The IDE is flagging up all the classes with the following messages: The serializable class (name of class) does not declare a static final serialVersionUID field of type long I'm not really sure what this means, it doesn't seem to be affecting the running of the applet.
I have attached a zipped copy of the applet & please be gentle with me as I'm still learning Java :D
mouser:
The IDE I'm using (eclipse) flags up the line desert.gameWon(); with the following error: The assignment to variable desert has no effect which I guess is why it's not working but I don't really understand why/how.
--- End quote ---
surely it's talking about the line above:
--- ---this.desert = desert;
which appears to be an assignment of a variable to itself.
(and if it's not meant to be, if you actually have two different variables, once a class variable and one a function variable, both named desert, then shame on you).
alexp:
:-[ well it used to called this.main = main but for some reason I changed al the mains to deserts (don't ask me why :)) because the gameWon() method is in the Desert class but it doesn't seem to get called.
--- ---final class Jeep {
public String name;
public int petrol, distance;
private Desert main;
protected Jeep(String jeepName) {
petrol = 0;
distance = 0;
name = jeepName;
}
private boolean canGoForward() {
if ((distance / 40) <= (petrol - 1)) {
return true;
}
else {return false;}
}
protected boolean forward() {
this.main = main;
if (won()) {
main.gameWon();
return false;
}
else {
if (canGoForward()) {
distance += 40;
petrol -= 1;
return true;
}
else {return false;}
}
}
private boolean won() {
if (distance == 120) {
// if (distance == (9 * 40)) {
return true;
}
else {return false;}
}
mouser:
i think you are misunderstanding the "this" pointer.
this refers to the object you are currently in.
so within an object function (method):
this.VARIABLE is the same as VARIABLE
(unless you have two different variables with the same name and one is hidden).
so when you do
this.desert = desert;
or
this.main = main;
you are saying
X = X
it's a meaningless assignment with no effect.
alexp:
I know but I earlier on in the applet I have set the Desert class as a super class
--- --- //sets the Desert class as the super class
public Desert() {
super();
}
and in this class doesn't everytime I have main mean that I'm telling the code to look in the Desert class for the method?
final class ControlsPanel extends JPanel implements ActionListener {
private JButton bReset, bRules, bSolution;
private JPanel buttonsPanel;
private Desert main;
// constructor
ControlsPanel(Desert main) {
this.main = main;
panel code goes here
}
public void actionPerformed (ActionEvent e) {
if (e.getSource() == bRules) {
JOptionPane.showMessageDialog(null, main.rules(), "Rules", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == bReset) {
int userResponse = JOptionPane.showConfirmDialog(null, "Are you sure you want to reset?", null, JOptionPane.YES_NO_OPTION);
if (userResponse == JOptionPane.YES_OPTION) {main.newGame();}
if (userResponse == JOptionPane.NO_OPTION) {};
}
if (e.getSource() == bSolution) {
int userResponse = JOptionPane.showConfirmDialog(null, "Are you sure you want to see the solution?", null, JOptionPane.YES_NO_OPTION);
if (userResponse == JOptionPane.YES_OPTION) {main.solution();}
if (userResponse == JOptionPane.NO_OPTION) {};
}
}
}
--- End quote ---
so using the same thinking thats what I tried to do with the Jeep class, but for some reason it doesn't seem to work in this class.
Navigation
[0] Message Index
[#] Next page
Go to full version