Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Animating sine waves

Thread view: 
Albert - 18 Jan 2008 01:49 GMT
I've been reading about animation using Java applets and I've come
across this example from http://www.javaworld.com/jw-03-1996/animation/Example4Applet.html

I'm having a lot of trouble understanding what is going on (that is,
the maths behind it all). Now I know sine as sin in trigonometry
problems and I know that the applet does indeed display the sine wave
as would be plotted on a graph but what's with multiplying by such
tiny numbers and adding 1, adding or subtracting the frame to the
iterator?

More related to java, what's a dimension and how does it take the
size() method?
Thomas Fritsch - 18 Jan 2008 10:47 GMT
> I've been reading about animation using Java applets and I've come
> across this example from http://www.javaworld.com/jw-03-1996/animation/Example4Applet.html
[quoted text clipped - 8 lines]
> More related to java, what's a dimension and how does it take the
> size() method?
A Dimension is has a width and a height. In your applet example the
size() method gives the dimension of the entire applet, i.e. width=500
and height=50. You should look into the HTML code of Example4Applet.html
to see how it comes to exactly these width and heigth.

You can look into the Java documentation at
<http://java.sun.com/j2se/1.5.0/docs/api/>,
especially the links given there to "Dimension", "Math", and "Graphics".
From there you will find the explanations of every single function,
like "sin" (on the "Math" page) and "size" (on the "Dimension" page).

Signature

Thomas

Thomas Fritsch - 18 Jan 2008 11:00 GMT
> You can look into the Java documentation at
> <http://java.sun.com/j2se/1.5.0/docs/api/>,
> especially the links given there to "Dimension", "Math", and "Graphics".
> From there you will find the explanations of every single function, like
> "sin" (on the "Math" page) and "size" (on the "Dimension" page).

--little correction--:
... "width" and "height" (on the "Dimension" page), "size" (on the
"Applet" page).

Signature

Thomas

Albert - 18 Jan 2008 21:35 GMT
On Jan 18, 9:47 pm, Thomas Fritsch <i.dont.like.s...@invalid.com>
wrote:
> > I've been reading about animation using Java applets and I've come
> > across this example fromhttp://www.javaworld.com/jw-03-1996/animation/Example4Applet.html
[quoted text clipped - 22 lines]
> --
> Thomas

I understand what the function is, I can look that up in any maths
book but I don't understand how the developer is using it.
Thomas Fritsch - 20 Jan 2008 16:18 GMT
> I understand what the function is, I can look that up in any maths
> book but I don't understand how the developer is using it.

[I don't know your current knowledge-level of Java or ComputerScience in
general. Therefore some of the hints below may or may not seem trivial
to you.]

Some facts to know beforehand:
(*) In Java the origin (x=0,y=0) is in top-left corner.
    x runs to the right, y runs to the bottom.
(*) In Java Math.sin(...) expects the angle in radians, not in degrees.

For convenience I quote the Java code from
<http://www.javaworld.com/jw-03-1996/animation/Example4Applet.html> here:
    public void paint(Graphics g) {
    Dimension d = size();
    int h = d.height / 2;
    for (int x = 0 ; x < d.width ; x++) {
       int y1 = (int)((1.0 + Math.sin((x - frame) * 0.05)) * h);
       int y2 = (int)((1.0 + Math.sin((x + frame) * 0.07)) * h);
       g.drawLine(x, y1, x, y2);
    }
    }
and the HTML code
  <applet code=Example4Applet.class width=500 height=50>
    <param name=fps value=10>
  </applet>

A strategy for understanding an algorithm is stepping through it with
pencil and paper (and a desk-calculator for the sine) as if you were the
computer.

(1) In the HTML code above note the applet parameter: fps=10 (by the
way: "fps" probably means "frames per second").

(2) Now look into the complete Example4Applet.java (into method
"init()"). There the "fps" applet-parameter is used to calculate a
variable "delay". You get delay = 20 (Repeat that calculation fo yourself!)

(3) Now look into method "run()" of Example4Applet.java.
There the "delay" variable is used to increment the variable "frame"
(which initially had the value 0) by 1 every 20 milliseconds.

(4) Proceed to method "paint(Graphics)". Now your paper-work begins:
Step through the paint method line by line; do the the calculation of
that line (you remembered to switch your desk-calculator to radians),
write down the calculated value of the variables.
Your sheet of paper might look like this:
  d = Dimension(width=500,height=50)
  h = 50/2 = 25
  frame = 0
  /* 1st iteration of the for-loop */
  x = 0
  y1 = (int)((1.0 + Math.sin((0 - 0) * 0.05)) * 25) = 1
  y2 = (int)((1.0 + Math.sin((0 + 0) * 0.07)) * 25) = 1
  //draw a black line from (x|y1) to (x|y2)
  /* 2nd iteration of the for-loop */
  x = 1
  y1 = (int)((1.0 + Math.sin((1 - 0) * 0.05)) * 25) = (int) 2.25 = 2
  y2 = (int)((1.0 + Math.sin((1 + 0) * 0.07)) * 25) = (int) 2.75 = 2
  //draw a black line from (x|y1) to (x|y2)
  /* 3rd iteration of the for-loop */
  .....

Signature

Thomas



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.