Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem converting applet to JApplet

 
Geeta Balasubramanian
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]

Following is code for JApplet

[code]
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/* <applet code="SpinningLine" height="500" width="500"></applet>*/
public class SpinningLine extends JApplet implements ItemListener,ActionListener
{
JButton btnStart , btnStop;
JRadioButton rbC,rbAC;
ButtonGroup bg;
String msg="", msg1="";
int x1=250,y1=250,r=100,l,s,p=0;
double q;
JPanel p1, p2;
int p2x, p2y, p2w,p2h;
public void init()
{
p1 = new JPanel();
p1.setLayout(new FlowLayout());
btnStart=new JButton("Start");
btnStop=new JButton("Stop");
btnStart.addActionListener(this);
btnStop.addActionListener(this);
bg=new ButtonGroup();
rbC = new JRadioButton("Clockwise",true);
rbAC=new JRadioButton("AntiClockwise",false);
rbC.addItemListener(this);
rbAC.addItemListener(this);
p1.add(rbC);
p1.add(rbAC);
bg.add(rbC);
bg.add(rbAC);
p1.add(btnStart);
p1.add(btnStop);
getContentPane().add(p1,BorderLayout.NORTH);
p2 = new JPanel();
p2.setSize(400,450);
p2.setBorder(BorderFactory.createTitledBorder("Drawing Area"));
p2x = p2.getX();
p2y = p2.getY();
p2w =(int)p2.getWidth();
p2h =(int)p2.getHeight();
getContentPane().add(p2,BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
msg=ae.getActionCommand();
repaint();
showStatus(msg);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()== rbC)
{
msg1=rbC.getActionCommand();
}
else
{
msg1=rbAC.getActionCommand();
}
showStatus(msg1);
}
public void paint(Graphics g)
{
if(msg.equals("Start"))
{
if(msg1.equals("Clockwise"))
{
if(p==360)
{
p=0;
p=p+1;
}
else
{
p=p+1;
}
g.setColor(Color.red);
}
else if(msg1.equals("AntiClockwise"))
{
if(p==0)
{
p=360;
p=p-1;
}
else
{
p=p-1;

}
g.setColor(Color.blue);
}
q=(p*3.14)/180;
l=(int)(r*Math.cos(q));
s=(int)(r*Math.sin(q));
g.drawLine(x1,y1,x1+l,y1+s);
repaint(1000, p2x, p2y, p2w, p2h);
}
else
{
g.drawLine(x1,y1,x1+l,y1+s);
}
}
}
[/code]
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic