Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Graphic Program, sun follows a semi-circle

 
Kevin Sahakian
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUIs forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic