Results 1 to 2 of 2
  1. #1
    Poor Bee is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default what is wrong with this program ?

    This applet is to create two labels ( Oval ) and ( Line ) . so when we click the button Oval it draws Oval shape , and same with Line ..

    *** The problem is when I run the prog .. Its shows the 2 buttons and the Oval shape is already there .. when I click the (Line) button it doesnt work ( it changes the Oval color from yellow to green ) ...

    ** I want to have separate action for each button. .
    so when I click Oval .. it shows Oval with one color
    and when I click Line ..it shows a line with one color ( the Oval shoud be disppear from da screen when Line is viewd )...


    This is my program :


    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    public class Two extends Applet implements ActionListener
    {
    Button g,y;
    char c;
    public void init()
    {
    setLayout(new FlowLayout());
    g=new Button("Oval");
    y=new Button("Line");
    g.addActionListener(this);
    y.addActionListener(this);
    add(g);
    add(y);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==g)
    {
    c='g';
    repaint();
    }
    if(e.getSource()==y)
    {
    c='y';
    repaint();
    }
    }
    public void paint(Graphics g)
    {
    g.setColor(Color.green);
    if(c=='g')
    g.setColor(Color.yellow);
    if(c=='y')
    g.drawLine(100,100,100,100);
    g.fillOval(100,100,100,100);
    }
    }
    Last edited by Poor Bee; 05-07-2008 at 06:03 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    //  <applet code="AppletTwo" width="400" height="400"></applet>
    //  atThePrompt>appletviewer AppletTwo.java
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class AppletTwo extends Applet implements ActionListener
    {
        Button g,y;
        char c;
    
        public void init()
        {
            System.out.println("default layout = " +
                                getLayout().getClass().getName());
            setLayout(new FlowLayout());
            g=new Button("Oval");
            y=new Button("Line");
            g.addActionListener(this);
            y.addActionListener(this);
            add(g);
            add(y);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==g)
            {
                c='g';
            }
            if(e.getSource()==y)
            {
                c='y';
            }
            repaint();
        }
    
        public void paint(Graphics g)
        { 
            if(c=='g')
            {
                g.setColor(Color.yellow);
                g.fillOval(100,100,100,100);
            }
            if(c=='y')
            {
                g.setColor(Color.green);
                //            x1, y1, x2, y2
    //            g.drawLine(100,100,100,100);
                g.drawLine(50,50,200,200);
            }
        }
    }

Similar Threads

  1. wrong values
    By mark-mlt in forum New To Java
    Replies: 8
    Last Post: 04-25-2008, 12:11 PM
  2. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:16 PM
  3. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 AM
  4. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 AM
  5. Is there somethign wrong with this code?
    By Soda in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •