DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: java applet problems  (Read 12,927 times)

alexp

  • Is cheese the plural of choose?????
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 41
java applet problems
« on: April 24, 2006, 07:06 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.
Code
	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.
Code
    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: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: java applet problems
« Reply #1 on: April 24, 2006, 12:09 PM »
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:
Code
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

  • Is cheese the plural of choose?????
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 41
Re: java applet problems
« Reply #2 on: April 24, 2006, 01:09 PM »
:-[ 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.

Code
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: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: java applet problems
« Reply #3 on: April 24, 2006, 01:14 PM »
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

  • Is cheese the plural of choose?????
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 41
Re: java applet problems
« Reply #4 on: April 24, 2006, 02:23 PM »
I know but I earlier on in the applet I have set the Desert class as a super class

Code
	//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
  • **
  • Posts: 3
Re: java applet problems
« Reply #5 on: April 06, 2007, 09:38 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, 09:40 PM by hoblingr »

jeremejazz

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

Original address: https://www.donationcoder.com/forum/index.php?topic=3367.0