This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Java Graphic Program, sun follows a semi-circle

Kevin Sahakian
Greenhorn
Posts: 4
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).

Looking mostly at lines 51 - 55

Any suggestions?

Campbell Ritchie
Sheriff
Posts: 48424
56
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.

Campbell Ritchie
Sheriff
Posts: 48424
56
Moving to our GUIs forum.

 Consider Paul's rocket mass heater.