I have a java applet program which is Spinning the Line (some graphics).
Now problem is tat earlier i had done tat using awt applet but now i have to convert the same program
in to JApplet.
now i thought it would be easy but now when i am converting to Swing JApplet
the same logic of the program is not working fine.
i am not sure if some technical diffrences between the awt and swing applet are responsible for that.
can any one please help me find the reason and i would be greatrful if any one can give me the program
converted in swing or please give me some hints so i can do it.
Thanks in advance
[code]
// Program that works fine with awt applet
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
//<applet code=SpinningLine height=500 width=500></applet>
public class SpinningLine extends Applet implements ActionListener,ItemListener
{
String msg1="",msg="",text="";
Button start,stop;
int x1=200,y1=200,r=100,l,s,p=0;
CheckboxGroup cbg;
Checkbox clock,anticlock;
double q;
public void init()
{
start = new Button("start");
stop = new Button("stop");
cbg = new CheckboxGroup();
clock = new Checkbox("clock",cbg,true);
anticlock = new Checkbox("anticlock",cbg,false);
//add to applet
add(start);
add(stop);
add(clock);
add(anticlock);
start.addActionListener(this);
stop.addActionListener(this);
clock.addItemListener(this);
anticlock.addItemListener(this);
}
public void actionPerformed(ActionEvent ae) {
//gets the caption of the button clicked
msg = ae.getActionCommand();
repaint();
}
public void itemStateChanged(ItemEvent arg0) {
//
repaint();
}
public void paint(Graphics g)
{
msg1 = cbg.getSelectedCheckbox().getLabel();
if(msg.equals("start"))
{
if(msg1.equals("clock"))
{
if(p==360)
{
p=0;
p=p+1;
}
else
{
p=p+1;
}
}
if(msg1.equals("anticlock"))
{
if(p==0)
{
p=360;
p=p-1;
}
else
{
p=p-1;
}
}
//converts the angle to radians
q = (p*3.14)/180;
//calculate the polar coordinates
l=(int)(r*Math.cos(q));
s=(int)(r*Math.sin(q));
//dreaw line using above coordinates
g.drawLine(x1,y1,x1+l,y1+s);//its +l NOT 1
repaint(550);
}
else
g.drawLine(x1,y1,x1+l,y1+s);//its x1+l NOT 1
}
}
[/code]
In Swing custom painting is done be extendings a JComponent (or JPanel) and by overriding the paintComponent(...) method of the component. Then you add the component to the contenpt pane of the the JApplet.
You should NOT be overriding the paint() method of the JApplet.