CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Aug 2011
    Posts
    13

    CONFUSED! Please help!

    I'm writing a method with some code that looks like this:

    public void fastPlod(Graphics s) {
    s.clearRect(tortX+1, tortY, WIDTH, HEIGHT);
    tortX += 60;
    System.out.println(tortX);
    }

    The current value of tortX = 300. When running this method, for some reason, the variable tortX gets assigned the incremental value 3 times.

    For example, the output is supposed to just read 360. However, when I run this, the output says 360, 420, 480. In other words, it seems to execute the assignment statement 3 times. To clarify, there are no loops, and everything compiles okay. It's just this mysterious assignment statement thing.

    Does anyone know why? Really annoying me. Thanks!

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    Where is that method called from? From a paint method?
    The output means that the method was called 3 times. Check your logic to see where and why.

    If you want to know who is calling a quick way is to add this to the method:
    try{throw new Exception("who called");}catch(Exception x){x.printStackTrace();}
    Norm

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: CONFUSED! Please help!

    Quote Originally Posted by Norm
    If you want to know who is calling a quick way is to add this to the method:
    try{throw new Exception("who called");}catch(Exception x){x.printStackTrace();}
    An easier way of getting a stack trace is:
    Code:
    Thread.dumpStack();
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    Thanks. I liked the other because you could put your own message into it.
    Norm

  5. #5
    Join Date
    Aug 2011
    Posts
    13

    Re: CONFUSED! Please help!

    Thanks for the suggestions. I still can't seem to find where the method is called 3 times. And yes, it's called from the paint method. Here's the full code:

    import java.awt.*;
    import java.applet.*;

    public class Project2 extends Applet {

    Image tortoise, hare;
    int tortX = 300, hareX = 300;
    final int tortY = 101, hareY = 301, WIDTH = 19, HEIGHT = 49;

    public void init() {
    tortoise = getImage(getDocumentBase(), "images/tortoise.gif");
    hare = getImage(getDocumentBase(), "images/hare.gif");
    }

    public void paint(Graphics screen) {
    drawRace(screen);
    fastPlod(screen);
    }

    public void drawRace(Graphics screen) {
    // GENERATES INITIAL GRAPHICS FOR RACE
    setBackground(new Color(64, 224, 208));
    screen.drawRect(300, 100, 1000, 50);
    screen.drawRect(300, 300, 1000, 50);
    int lineX = 320, lineYi = 100, lineYf = 150;
    for (int i = 1; i<=98; i++) {
    if (lineX == 1300) {
    lineX = 320; lineYi = 300; lineYf = 350;
    }
    screen.drawLine(lineX, lineYi, lineX, lineYf);
    lineX += 20;
    }
    screen.fillRect(tortX+1, 101, 19, 49);
    screen.fillRect(hareX+1, 301, 19, 49);
    screen.drawImage(tortoise, 84, 80, this);
    screen.drawImage(hare, 91, 271, this);
    Font f = new Font("Times New Roman", Font.BOLD, 24);
    screen.setFont(f);
    screen.drawString("Project 2 Race Simulator", 300, 55);
    }

    private void fastPlod(Graphics s) {
    s.clearRect(tortX+1, tortY, WIDTH, HEIGHT);
    tortX += 60;
    System.out.println(tortX);
    }
    }

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    it's called from the paint method.
    The paint method is called many times. How often do you call the repaint() method or shadow the frame or do something that would cause the JVM to call the paint method.
    Bottomline, don't change control variables in paint or methods called by paint.
    Change your logic so you can control EXACTLY when the variables are changed.
    You'll probably want to use a Timer to call your method that will change the location where to draw.
    Norm

  7. #7
    Join Date
    Aug 2011
    Posts
    13

    Re: CONFUSED! Please help!

    I'm relatively new to Java so I'm not sure what you mean by shadow the frame. However, I don't use the repaint method anywhere in my program. Here's the extent of what I've written:

    Code:
    	
    import java.awt.*;
    import java.applet.*;
    
    public class Project2 extends Applet {
            Image tortoise, hare;
    	int tortX = 300, hareX = 300;
    	final int tortY = 101, hareY = 301, WIDTH = 19, HEIGHT = 49;
    	
    	public void init() {
    		tortoise = getImage(getDocumentBase(), "images/tortoise.gif");
    		hare = getImage(getDocumentBase(), "images/hare.gif");
    	}
    	
    	public void paint(Graphics screen) {
    		drawRace(screen);	
    		fastPlod(screen);
    	}
    	
    	public void drawRace(Graphics screen) {
    		// GENERATES INITIAL GRAPHICS FOR RACE
    		setBackground(new Color(64, 224, 208));
    		screen.drawRect(300, 100, 1000, 50);
    		screen.drawRect(300, 300, 1000, 50);
    		int lineX = 320, lineYi = 100, lineYf = 150;
    		for (int i = 1; i<=98; i++) {	
    			if (lineX == 1300) {
    				lineX = 320; lineYi = 300; lineYf = 350;
    			}
    			screen.drawLine(lineX, lineYi, lineX, lineYf);
    			lineX += 20;
    		}
    		screen.fillRect(tortX+1, 101, 19, 49);
    		screen.fillRect(hareX+1, 301, 19, 49);
    		screen.drawImage(tortoise, 84, 80, this);
    		screen.drawImage(hare, 91, 271, this);
    		Font f = new Font("Times New Roman", Font.BOLD, 24);
    		screen.setFont(f);
    		screen.drawString("Project 2 Race Simulator", 300, 55);
    	}
    	
    	public void fastPlod(Graphics s) {
    		s.clearRect(tortX+1, tortY, WIDTH, HEIGHT);
    		tortX += 60;
    		s.fillRect(tortX+1, tortY, WIDTH, HEIGHT);
    		System.out.println(tortX);
    
    	}
    }
    I'm used to there being a main() method from which to control program flow and execution of other methods. How would I go about using something similar in applets?

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    By shadowing I mean, when your program is showing on the screen, open another window and drag it over the top of your program (shadowing it) and then off again. That should cause paint() to be called several times.

    There are methods in an applet that the browser calls at different stages of the applet's life. This is documented in the applet tutorial. Go to this site and Find: Applet
    http://download.oracle.com/javase/tu...ybigindex.html
    The main method is called once when the java command starts the class.
    Norm

  9. #9
    Join Date
    Aug 2011
    Posts
    13

    Re: CONFUSED! Please help!

    Thanks for your help. So unlike a traditional java program where there is a main method that executes line by line, applets execute methods like init(), start(), stop(), etc. at different stages of an applet's life?

    I'm still confused on how to translate my code into an applet, though. Basically, I've been treating the paint method as a main method where the program executes one line at a time and all the logic is written in there.

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    the program executes one line at a time
    All code executes that way in any method anywhere.

    Only put in the paint method what is needed to paint the currently desired image.
    Move the other code to a different method. For example do not increment the tortX variable in paint or any of its called methods.
    Norm

  11. #11
    Join Date
    Aug 2011
    Posts
    13

    Re: CONFUSED! Please help!

    All code executes that way in any method anywhere.
    I understand that, but I think what I'm trying to say is that I'm used to there being a central method from which other methods are called. In applets, there is no "central" method. Instead, there are init, start, stop, etc. I don't know to translate my code into those methods. And if I just make another method, when/where does that get called?

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    Yes, applets are different.
    The "main" for applets is the browser. It calls the various applet methods according to the cycle of applet usage as per the tutorial. The applet is controlled by the browser which is controlled by the user.
    if I just make another method, when/where does that get called?
    It would be your responsibility to call it.
    Norm

  13. #13
    Join Date
    Aug 2011
    Posts
    13

    Re: CONFUSED! Please help!

    Thanks for the help, Norm. I think I got the applet working. Here's the finish code:

    Code:
    import java.awt.*;
    import java.applet.*;
    
    public class Project2 extends Applet {
    	Image tortoise, hare;
    	int tortX = 250, hareX = 250; 
    	final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50;
    	int turn; String turnNum;
    	int move; String tMove, hMove;
    	AudioClip clip;
    
    	public void init() {
    		tortoise = getImage(getDocumentBase(), "images/tortoise.gif");
    		hare = getImage(getDocumentBase(), "images/hare.gif");
    		move = 0; turn = 0;
    		AudioClip clip = getAudioClip(getCodeBase(), "sounds/tada.wav");
    		control();
    	}
    
    	
    	public void control() {
    		while ((tortX < 985) || (hareX < 985)) {
    			move = (int)(10 * Math.random());
    			switch (move) {
    				case 1:
    				case 2:
    					tortX += (3 * WIDTH);
    					hareX += (9 * WIDTH);
    					tMove = "Fast Plod"; hMove = "Big Hop";
    					break;
    				case 3:
    				case 4:
    				case 5:
    					tortX += (3 * WIDTH);
    					hareX += WIDTH;
    					tMove = "Fast Plod"; hMove = "Small Hop";
    					break;
    				case 6:
    					tortX += WIDTH;
    					if (hareX == 250) {} // DO NOTHING
    					else if (hareX <= (250 + (11 * WIDTH)))
    						hareX = 250;
    					else 
    						hareX -= (12 * WIDTH);
    					tMove = "Slow Plod"; hMove = "Big Slip";
    					break;
    				case 7:
    				case 8:
    					tortX += (1 * WIDTH);
    					if (hareX == 250) {} // DO NOTHING
    					else if (hareX <= (250 + (WIDTH)))
    						hareX = 250;
    					else 
    						hareX -= (2 * WIDTH);
    					tMove = "Slow Plod"; hMove = "Small Slip";
    					break;
    				case 9:
    				case 10:
    					if (tortX == 250) {} // DO NOTHING
    					else if (tortX <= (250 + (5 * WIDTH)))
    						tortX = 250;
    					else 
    						tortX -= (6 * WIDTH);
    					tMove = "Slip"; hMove = "Fall Asleep. Zzz...";
    					break;
    					// Hare falls asleep. No action. 
    			}
    			
    			turn++; turnNum = (turn + "");
    			repaint();
    			for (int i = 1; i <= 10; i++) { delay(); }
    		}
    
    		tortX = 985; hareX = 985;
    		repaint();
    	}
    
    	public void paint(Graphics screen) {
    		drawRace(screen);
    
    		if (tortX >= 985) {
    			screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
    			screen.drawString("Tortoise Wins", 650, 240);
    			clip.play();
    			clearCurrent(screen); 
    			fillNext(screen);
    		} else if (hareX >= 985) {
    			screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
    			screen.drawString("Tortoise Wins", 650, 240);
    			clip.play();
    			clearCurrent(screen); 
    			fillNext(screen);
    		} else {		
    			screen.drawString(("Turn " + turnNum), 621, 55);
    			screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
    			screen.drawString(tMove, 59, 65); screen.drawString(hMove, 66, 255); 
    			clearCurrent(screen);
    			fillNext(screen);
    		}
    
    		stop();
    	}
    
    	public void clearCurrent(Graphics s) {
    		s.clearRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
    		s.clearRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
    
    	}
    
    	public void fillNext (Graphics s) {
    		s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
    		s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
    
    	}
    
    	public void drawRace(Graphics s) {
    		// GENERATES INITIAL GRAPHICS FOR RACE
    		setBackground(new Color(64, 224, 200));
    		s.drawRect(250, 100, 750, 50);
    		s.drawRect(250, 300, 750, 50);
    		int lineX = 265, lineYi = 100, lineYf = 150;
    		for (int i = 1; i <= 98; i++) {
    			if (lineX == 1000) {
    				lineX = 265; lineYi = 300; lineYf = 350;
    			}
    			s.drawLine(lineX, lineYi, lineX, lineYf);
    			lineX += 15;
    		}
    		s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1);
    		s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1);
    		s.drawImage(tortoise, 59, 80, this);
    		s.drawImage(hare, 66, 271, this);
    		s.setFont(new Font("Times New Roman", Font.BOLD, 24));
    		s.drawString("Project 2 Race Simulator", 250, 55);
    	}
    
    	public void delay() {
    		for (int i = 0; i < 90000000; i++) {}
    	}
    
    	public void stop() {}
    }
    Only thing... when I load the applet into the appletviewer, it doesn't display properly. However, when I open the html file, it works fine for the most part. Sometimes it runs fine, sometimes it doesn't. Any solution come to mind?

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    Could you describe in more detail what you are seeing.
    Norm

  15. #15
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: CONFUSED! Please help!

    Time for your next lesson. Your code should use a Timer as I suggested earlier.
    You do NOT want to have a loop the burns CPU cycles to pass the time.
    You should create a Timer and have the listener method call the control method every few milliseconds to update the positions of the racers. Get rid of the delay() method. The control loop should not have a while loop, use an if instead. It should call repaint and exit.

    I suggest you write a very small program that uses a Swing Timer that calls a listener which prints out a message to say it was called. Very simple. When you get that to work then you can take the logic to your applet.
    Norm

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured