Akhil Vijay Akhil Vijay - 1 year ago 60
Java Question

Java codet derived from Javascript logic is not working prpperly

I am using NetBeans8 IDE .

Here is a java scrippt function from this Fiddle

function animate() {
xnow = parseInt(item.style.left);
item.style.left = (xnow+1)+'px';
ynow = parseInt(item.style.top);
item.style.top = (ynow+Math.sin(2*Math.PI*(xnow/50))*10) + "px";

Here the programmer creates s moving sine wave using java script.

And with the same idea, by making some slight changes, I create a java program using
timer t
.The equation is exactly the same as above.But my
is going somewhere uncontrollable.
Is it possible to create a moving sine wave by this method ?

Kindly help me to solve this..Thanks in advance.

Here is my bit of java code

Timer t = new Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {

//AL1 is the name given to radiobutton
int xnow=AL1.getX()+1;
int ynow=AL1.getY();
ynow=(int) (ynow+Math.sin(2*Math.PI*(xnow/50))*10);
AL1.setLocation(xnow, ynow);

private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
AL1.setLocation(0, 200);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(Lpne, javax.swing.GroupLayout.DEFAULT_SIZE, 1030, Short.MAX_VALUE)
.addComponent(Lpne, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)

Please try to make my
under control..

Thanks to all...

sine wave

Answer Source

2 different issue effects your logic,

  • Layout : You need to put your Swing Component to a container component which has no layout . or we can call it as a plane without layout.

  • Division : Divison operator accepts 2 operands and returns a value.These 2 operands and return value has a type. Divison operand's return type is equal to operand's type which is the largest set of numbers. example : int / short returns int or int / int returns int or float / int returns float

So lets consider division in javascript, javascript has just one type and its number (set of real number). so result will always be a real number.

But in java you defined xnow as int and 50 is constant int then result must be a int this means :

30 / 50 = 0;

So you have to make one of your operand real number. In Java you can pick Float type for that.

30 / 50F = 0.6;

50F is a constant float value.

So if you change the code like this ;

    Timer t = new Timer(10, new ActionListener() 
        public void actionPerformed(java.awt.event.ActionEvent e) 
            int xnow = AL1.getX() + 1;
            int ynow = AL1.getY();

            ynow = (int) (ynow + Math.sin(2*Math.PI*(xnow/50F)) * 10);
            AL1.setLocation(xnow, ynow);

with in this kind of container

private void initGui()
    mainFrame = new JFrame("Test Frame");

    AL1 = new JRadioButton("Radio");
    AL1.setSize(100, 100);
    AL1.setLocation(50, 50);


Your logic will work like in javascript.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download