Results 1 to 11 of 11
- 09-16-2010, 02:22 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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
and the return isJava 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); } } }
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.Java Code:1 2 3 4 1 2 3 4
Thanks in advance
- 09-16-2010, 02:27 AM #2
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
- 09-16-2010, 02:35 AM #3
I missed what the problem is.the cause of the problem
Or is it that you didn't understand what would happen when your program was executed?
- 09-16-2010, 04:48 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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.
- 09-16-2010, 01:24 PM #5
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?I want to know the distance from one to another.
but my distances are not what they are supposed to be.
Show your code for this.why does (3-5)^2 return -4
- 09-16-2010, 06:20 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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;
The call from the other method isJava 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; }
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));
- 09-16-2010, 08:37 PM #7
why does (3-5)^2 return -4What did you expect?Java Code:int value binary 32 bit 2's complement -------------------------------------------------- (3-5) = -2 11111111111111111111111111111110 -------------------------------------------------- 2 10 -------------------------------------------------- bitwise XOR 11111111111111111111111111111100 = -4 --------------------------------------------------
http://download.oracle.com/javase/tu...bolts/op3.html
db
- 09-17-2010, 02:04 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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.
- 09-17-2010, 02:30 AM #9
Depends on the definition of the ^ operator.
-
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.
- 09-17-2010, 04:04 AM #11
Similar Threads
-
how to add more than one paint method
By gautham in forum Java 2DReplies: 2Last Post: 04-06-2010, 07:07 AM -
Java Paint Method?
By leapinlizard in forum Java 2DReplies: 2Last Post: 02-11-2010, 07:01 PM -
an error in paint method
By hopey in forum Java 2DReplies: 7Last Post: 04-24-2009, 10:12 PM -
How to paint on a Netbean 6.5 generated applet?
By Pucho in forum NetBeansReplies: 1Last Post: 03-31-2009, 05:04 PM -
[SOLVED] calling javascript from applet method
By shwein in forum Java AppletsReplies: 5Last Post: 11-01-2008, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks