I'm currently using NetBeans8 IDE.
Check out this java script 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);
}
The developer here creates a moving sine wave using JavaScript.
Following the same concept, with some minor modifications, I've written a Java program using timer t
. The equation used is identical to the one above. However, my jRadioButton
seems to be moving uncontrollably. Can a moving sine wave be achieved through this method?
Please assist me in resolving this issue. Thank you in advance.
Below is the snippet of my 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)
);
Could someone help bring my jRadioButton
under control?
Thank you to everyone. This marks the final outcome.