Akhil Vijay Akhil Vijay - 5 months ago 10
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";
setTimeout(animate,20);
}


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
jRadioButton
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() {
@Override
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);
t.start();
}


javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(Lpne, javax.swing.GroupLayout.DEFAULT_SIZE, 1030, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Lpne, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
);


Please try to make my
jRadioButton
under control..

Thanks to all...
IT IS THE FINAL RESULT

sine wave

Answer

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");
    mainFrame.setSize(800,600);
    mainFrame.setLayout(null);


    AL1 = new JRadioButton("Radio");
    AL1.setSize(100, 100);
    AL1.setVisible(true);
    AL1.setLocation(50, 50);
    mainFrame.add(AL1);

    mainFrame.setVisible(true);
}

Your logic will work like in javascript.