-
Very urgently. Please help.
Dear all,
I need to pass my homework tomorrow, but i spent 2 more hrs still cannt complete
it. please help.
I need to do a class that just two checkbox in the applet, one is circle,
one is rectangle. when user click the circle, it draw circle, when the user
click rectangle, it draws rectangle. If user unclick, the circle or rectangle
should disappear.
Here is my coding, please help.
***********************************************
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.* ;
public class DrawObject extends JApplet implements ItemListener
{
public boolean lCir = false, lRect = false;
public JPanel chkPanel = new JPanel();
JCheckBox chkCir = new JCheckBox("Draw CIRCLE");
JCheckBox chkRect = new JCheckBox("Draw RECTANGEL");
public void init()
{
chkPanel.setLayout(new FlowLayout());
chkCir.addItemListener(this);
chkPanel.add(chkCir);
chkRect.addItemListener(this);
chkPanel.add(chkRect);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(chkPanel);
setSize(400,400);
}
public void itemStateChanged(ItemEvent e)
{
lCir = chkCir.isSelected();
lRect = chkRect.isSelected();
repaint();
}
public void paint(Graphics s)
{
super.paint(s);
if (lCir = true)
s.fillOval(100,100,50,50);
if (lRect = true)
s.fillRect(200,200,40,40);
}
}
****************************************************************
<HTML>
<APPLET CODE = "DrawObject.class" width = 400 height = 400>
</APPLET>
</HTML>
****************************************************************
I dont know why the first time it draw circle and rectangle even i set
the two boolean to false.
I dont know why i click the checkbox , it doesnt do anything.
Thank a lot
Toms
-
Re: Very urgently. Please help.
"tomsng" <tomsng@sinaman.com> wrote:
>
>Dear all,
>I need to pass my homework tomorrow, but i spent 2 more hrs still cannt
complete
>it. please help.
Toms,
Most of us have taken Java courses and have done the work ourselves. That
is how you learn and how you earn the right to be called a programmer. Everyone
of us has spent sleepless nights debugging to find an insidious bug. If you
are having problems, I suggest that you see your instructor or a tutor for
your course. We aren't going to do your homework for you.
Regards,
C--
-
Re: Very urgently. Please help.
To answer your question about why you see the circle and rectangle upon initialization...check
out your paint method. You are assigning the value true to each of your
boolean variables thereby forcing the boolean evaluation to true...you want
to evaluate whether the value is equivalent to true or false. In order to
do that you must use == operator.
Cheers,
Mike
"tomsng" <tomsng@sinaman.com> wrote:
>
>Dear all,
>I need to pass my homework tomorrow, but i spent 2 more hrs still cannt
complete
>it. please help.
>I need to do a class that just two checkbox in the applet, one is circle,
>one is rectangle. when user click the circle, it draw circle, when the user
>click rectangle, it draws rectangle. If user unclick, the circle or rectangle
>should disappear.
>Here is my coding, please help.
>***********************************************
>import java.awt.*;
>import javax.swing.*;
>import java.applet.*;
>import java.awt.event.* ;
>
>public class DrawObject extends JApplet implements ItemListener
>{
> public boolean lCir = false, lRect = false;
> public JPanel chkPanel = new JPanel();
> JCheckBox chkCir = new JCheckBox("Draw CIRCLE");
> JCheckBox chkRect = new JCheckBox("Draw RECTANGEL");
>
> public void init()
> {
> chkPanel.setLayout(new FlowLayout());
> chkCir.addItemListener(this);
> chkPanel.add(chkCir);
> chkRect.addItemListener(this);
> chkPanel.add(chkRect);
> getContentPane().setLayout(new FlowLayout());
> getContentPane().add(chkPanel);
> setSize(400,400);
> }
>
> public void itemStateChanged(ItemEvent e)
> {
> lCir = chkCir.isSelected();
> lRect = chkRect.isSelected();
> repaint();
> }
>
> public void paint(Graphics s)
> {
> super.paint(s);
> if (lCir = true)
> s.fillOval(100,100,50,50);
> if (lRect = true)
> s.fillRect(200,200,40,40);
> }
>}
>****************************************************************
><HTML>
><APPLET CODE = "DrawObject.class" width = 400 height = 400>
></APPLET>
></HTML>
>****************************************************************
>I dont know why the first time it draw circle and rectangle even i set
>the two boolean to false.
>I dont know why i click the checkbox , it doesnt do anything.
>
>Thank a lot
>
>Toms
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|