Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 09, 2016, 07:39:53 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: java applet problems  (Read 7633 times)

alexp

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 39
  • Is cheese the plural of choose?????
    • View Profile
    • Donate to Member
java applet problems
« on: April 24, 2006, 02:06:44 AM »
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
Why is it that writers write, but fingers don't fing, grocers don't groce, and hammers don't ham? If the plural of tooth is teeth, why isn't the plural of booth beeth? One goose, 2 geese. So, one moose, 2 meese? One index, two indices? Is cheese the plural of choose?

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,421
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: java applet problems
« Reply #1 on: April 24, 2006, 07:09:03 AM »
Quote
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.

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

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 39
  • Is cheese the plural of choose?????
    • View Profile
    • Donate to Member
Re: java applet problems
« Reply #2 on: April 24, 2006, 08:09:27 AM »
 :-[ 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;}
    }
Why is it that writers write, but fingers don't fing, grocers don't groce, and hammers don't ham? If the plural of tooth is teeth, why isn't the plural of booth beeth? One goose, 2 geese. So, one moose, 2 meese? One index, two indices? Is cheese the plural of choose?

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,421
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: java applet problems
« Reply #3 on: April 24, 2006, 08:14:20 AM »
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

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 39
  • Is cheese the plural of choose?????
    • View Profile
    • Donate to Member
Re: java applet problems
« Reply #4 on: April 24, 2006, 09:23:14 AM »
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?

Quote
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) {};
      }
   }
}
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.
Why is it that writers write, but fingers don't fing, grocers don't groce, and hammers don't ham? If the plural of tooth is teeth, why isn't the plural of booth beeth? One goose, 2 geese. So, one moose, 2 meese? One index, two indices? Is cheese the plural of choose?

hoblingr

  • Supporting Member
  • Joined in 2007
  • **
  • default avatar
  • Posts: 3
    • View Profile
    • Donate to Member
Re: java applet problems
« Reply #5 on: April 06, 2007, 04:38:41 PM »
Hi,

The problem must be cause your applet is not multithreaded, so it has no time to refresh the graphics. You should make your drawing process in another thread and it will solve your problem.
For your popup not showing up, run your code in debug mode with the applet viewer to see how the code go.

BTW, avoid 'main' as a variable name, it is 'reserved' for the public static void main(String[] args) app entry point  :)

Stef
« Last Edit: April 06, 2007, 04:40:43 PM by hoblingr »

jeremejazz

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 58
  • hey!
    • View Profile
    • Personal Website
    • Donate to Member
Re: java applet problems
« Reply #6 on: April 06, 2009, 06:14:20 AM »
hmmm.... i'll try to work on this some time