Working on a school programming project. The program uses graphics and the timer class to represent the sun rising and setting across a "skyline". I'm just about done, but I can't seem to find a good way to get the sun to move across the skyline right. Ultimately I want it to move across the panel in a smooth, concave fashion but right now I can't even get it to set. I was thinking that I would use a loop to stop it from going up the y-axis at some point directly North in the sky, then have another loop to bring it back down on the SE horizon. I haven't been able to get this to work since the loops seem to skip over the repaint() resulting in the sun jumping from the SW horizon to the top middle. I want it to start at the point (0,400), and end at (400,800).
The locus of a circle is (dsinθ, dcosθ)
So you want to convert your 12 hours to angles (12hours ≡ πradians) and work out the sin and cos of that angle. Use the timer object not to move the sun but to change the “time”. In paintComponent (which should have protected access), you can call a method which resets k and z (peculiar names for fields, why not x and y) using the sin and cos.
Beware: the convention that sin and cos are worked out from has 0 at 3o’clock and runs anti‑clockwise.
Some poor style, using == true and similar. Never use that; it is error‑prone, too. You write if (b)... or if (!b)... Poor design using addActionListener(this). Never mind that you see it in many books, it is non‑object‑oriented programming and should ba avoided like the plague.
Joined: Oct 13, 2005
Moving to our GUIs forum.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Java Graphic Program, sun follows a semi-circle