Results 1 to 5 of 5
  1. #1
    pro85 is offline Member
    Join Date
    Apr 2008
    Posts
    16
    Rep Power
    0

    Question wrong source code

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;

    public class PlayBalloon extends Applet implements ActionListener {

    private Button grow, shrink, left, right;
    private Balloon myBalloon;

    public void init() {
    grow = new Button("Grow");
    add(grow);
    grow.addActionListener(this);
    shrink = new Button("Shrink");
    add(shrink);
    grow.addActionListener("this");
    left = new Button("Left");
    add(left);
    left.addActionListener(this);
    right = new Button("Right");
    add(right);
    right.addActionListener(this);

    myBalloon = new Balloon();
    }

    public void actionPerformed(ActionEvent event) {
    if (event.getSource() == grow)
    myBalloon.grow();
    if (event.getSource() == shrink)
    myBalloon.shrink();
    if (event.getSource() == left)
    myBalloon.left();
    if (event.getSource() == right)
    myBalloon.right();
    repaint();
    }

    public void paint(Graphics g) {
    myBalloon.display(g);
    }
    }

    class Balloon {

    private int diameter = 10;
    private int xCoord = 20, yCoord = 50;

    public void display(Graphics g) {
    g.drawOval(xCoord, yCoord, diameter, diameter);
    }

    public void left() {
    xCoord = xCoord - 10;
    }

    public void right() {
    xCoord = xCoord + 10;
    }

    public void grow() {
    diameter = diameter + 5;
    }

    public void shrink() {
    diameter = diameter - 5;
    }
    }

    error line 16, how to edit?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What's the error you get there? Seems you have copied this code and don't know what's exactly happen. And I don't think that anyone wants to edit this code for you here in our community.

    Post your complete error message and ask your question more clearly.

  3. #3
    pro85 is offline Member
    Join Date
    Apr 2008
    Posts
    16
    Rep Power
    0

    Default

    error line 16 shrink.addActionListener("this");

    it mention
    addActionListener(java.awt.event.ActionListener) in java.awt.Button can not be applied to (java.lang.String)

    how to solve it?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The error message is telling you exactly what's wrong. You're passing a String to the addActionListener method when it's expecting an ActionListener object. Quotes around "this" make it a String. No quotes makes it refer to the current object. So get rid of the quotes.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 06:44 PM
  2. Replies: 1
    Last Post: 11-28-2008, 07:27 PM
  3. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 02:50 AM
  4. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 12:26 PM
  5. Need a source code
    By vissu007 in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 08:08 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
  •