*
The moose likes Swing / AWT / SWT and the fly likes Java Graphic Program, sun follows a semi-circle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java Graphic Program, sun follows a semi-circle" Watch "Java Graphic Program, sun follows a semi-circle" New topic
Author

Java Graphic Program, sun follows a semi-circle

Kevin Sahakian
Greenhorn

Joined: Nov 19, 2012
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

Joined: Oct 13, 2005
Posts: 38508
    
  23
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

Joined: Oct 13, 2005
Posts: 38508
    
  23
Moving to our GUIs forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Graphic Program, sun follows a semi-circle