2

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

invzbl3
  • 5,872
  • 9
  • 36
  • 76
  • where did you put AL1 in ? problably Al1's parent has a layout which doesnt support cartesian coordinates (x, y) pixel based positioning functionality. – Ömer Erden Jun 08 '16 at 16:03
  • Its nothing but simply a name for my `JRadiobutton`.Obviously it has `.getX()` and 'getY()` methood.I also created an analog clock using `JRadiobutton` and `JLabel` – Akhil Vijay Jun 08 '16 at 16:10
  • I didn't ask what AL1 is . I asked which object contains AL1, we call this object as a AL1's parent , and what is the AL1's parent's layout type ? – Ömer Erden Jun 08 '16 at 16:13
  • I placed all the components on `jLayeredpane` described layout part in question now. I make some edition on my question Please check it out – Akhil Vijay Jun 08 '16 at 16:44
  • http://stackoverflow.com/questions/8758229/place-components-at-arbitrary-x-y-coordinates, can you check this?, i cant test it right now but making layout of continer null should work.But it should be Al1's parent, I suggest you to just test it with empty contentPane without layeredpane, then try to implement in your layout system. – Ömer Erden Jun 09 '16 at 07:58
  • I check in that way too.But same problem.Any way thanks for your help.I am working on it – Akhil Vijay Jun 09 '16 at 08:18

1 Answers1

1

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 / 50 F)) * 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.

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45