Results 1 to 11 of 11
  1. #1
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default Applet calling paint method twice = for loop running twice?

    Hi guys, semi-long time troller but new member.

    I am trying to learn java and in an exercise to write a simple applet w/ a for loop inside, I am getting some interesting results. I wrote a test class to help me debug but I feel the cause of the problem is beyond my java understanding.

    With a simple applet class and paint method, like the one below, the oval is drawn except the output in the console is double what my for loop should be. See below for code + return


    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class test extends Applet{
    	public void paint (Graphics p){
    		p.fillOval(10, 10, 5, 5);
    	
    		for (int i=1; i<=4; i++){
    		System.out.println(i);
    		}
    	
    	}
    }
    and the return is

    Java Code:
    1
    2
    3
    4
    1
    2
    3
    4
    Am I using poor form when writing this simple program? Perhaps the for loop should be outside of the paint method? The real purpose of the for loop is to draw different ovals on the applet, which is why it would be convenient to have it inside. I don't think it matters but I don't have any other ideas as to the problem.

    Thanks in advance

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    You don't have any control over how often and how many times a painting method is called. You can request a repaint() but even there, multiple pending repaint() calls will be coalesced into one painting event.

    Why does it matter to you if your ovals are drawn twice instead of once?

    db

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    the cause of the problem
    I missed what the problem is.
    Or is it that you didn't understand what would happen when your program was executed?

  4. #4
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    the issue I am concerning myself with is the fact that my for statement has a random generator in it, that changes some variables so during ONE run of the program, I will get (in my case) TWO results, although I am certain it's just the last one that sticks. I say two because it always outputs 2 sets, I know if I resize/move the applet it will keep pumping out results.

    so long as I am not outputting anything to the console (which I am just doing to debug) then the end user would never know. But what if someone did want to output to console, they could never get rid of this and are forced to use it as-is and stick with the last set of stored vars? I don't want to get crazy to try and fix this I am just curious, I think I can carry on now that I know this is suppose to happen.

    the second and hopefully last problem I am having is something I hope you can help me with. I have a simple object I made that both contain distances and I want to know the distance from one to another. I thought I was doing this right but my distances are not what they are supposed to be.

    the last issue that I found after a bit of not-fun looking was to my surprise,
    why does (3-5)^2 return -4?!?!?! I don't think I can use that operator the way I am used to.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    I want to know the distance from one to another.
    but my distances are not what they are supposed to be.
    Is this 2 dimensional distance between two x,y points? What equation are you using to compute the distance? What does the code generate and what is it supposed to be?

    why does (3-5)^2 return -4
    Show your code for this.

  6. #6
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    ok i changed it to use the Math.pow function which works as expected, but I will post the original code that started this distance problem.

    Class code;

    Java Code:
    public class Ship{
    	int xloc,yloc;
    	int radius=2;
    /**
     * Initializes the ship with coords (x,y)
     * @param x x coordinate
     * @param y y coordinate
     */
    	public Ship(int x,int y){
    		xloc = x;
    		yloc = y;
    	}
    
    	public double distance(Ship other)	{
    		double dist = Math.sqrt((xloc-other.xloc)^2+(yloc-other.yloc)^2);
    		return dist;
    	}
    The call from the other method is

    Java Code:
    Random rand = new Random();
    Ship centerShip = new Ship(centerx,centery);
    int randX = rand.nextInt(2*radius)+offset+1;
    int randY = rand.nextInt(2*radius)+offset+1;
    Ship d1 = new Ship(randX,randY);
    
    System.out.println(centerShip.distance(d1));

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    why does (3-5)^2 return -4
    Java Code:
    int value        binary 32 bit 2's complement
    --------------------------------------------------
    (3-5)
    = -2            11111111111111111111111111111110
    --------------------------------------------------
       2                                          10
    --------------------------------------------------
    bitwise XOR     11111111111111111111111111111100 = -4
    --------------------------------------------------
    What did you expect?

    http://download.oracle.com/javase/tu...bolts/op3.html

    db

  8. #8
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    Looks like I need to take caution when thinking about traditional math and Java math because

    (-2)^2 is indeed on paper and in a calculator, 4.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    Depends on the definition of the ^ operator.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are we all getting the feeling that the OP mistakenly feels that ^ is a power operator or something on that order? When in doubt, read the tutorials.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    Are we all getting the feeling that the OP mistakenly feels that ^ is a power operator or something on that order? When in doubt, read the tutorials.
    For which I provided a link.

    Oh well. You can take a horse to water ...

Similar Threads

  1. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 07:07 AM
  2. Java Paint Method?
    By leapinlizard in forum Java 2D
    Replies: 2
    Last Post: 02-11-2010, 07:01 PM
  3. an error in paint method
    By hopey in forum Java 2D
    Replies: 7
    Last Post: 04-24-2009, 10:12 PM
  4. Replies: 1
    Last Post: 03-31-2009, 05:04 PM
  5. [SOLVED] calling javascript from applet method
    By shwein in forum Java Applets
    Replies: 5
    Last Post: 11-01-2008, 04:45 PM

Posting Permissions

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