Results 1 to 4 of 4
  1. #1
    Shrek is offline Member
    Join Date
    May 2015
    Posts
    2
    Rep Power
    0

    Default How to remove the balls on click?

    import acm.program.*;
    import acm.graphics.*;
    import acm.util.*;
    import java.awt.event.*;
    import java.awt.*;

    public class targetProgram1 extends GraphicsProgram
    {
    public static final int APPLICATION_WIDTH= 500;
    public static final int APPLICATION_HEIGHT= 500;

    GOval ball;
    GOval ball1;

    int dirX = 25;
    int dirY = 0;
    int dirX1 = 0;
    int dirY1 = 25;
    int max = 0;
    int max1 = 0;
    int score;

    public void init() {

    addMouseListeners();
    score = 100;


    }

    public void run(){

    setBackground(Color.WHITE);

    GOval ball = new GOval(50,50);
    ball.setFilled(true);
    ball.setColor(Color.RED);
    add(ball, 50, 100);

    GOval ball1 = new GOval(50,50);
    ball1.setFilled(true);
    ball1.setColor(Color.GREEN);
    add(ball1, 50, 50);

    GLabel myLabel = new GLabel("score is: " + score);
    add(myLabel, 300, 50);


    while(true){
    ball.move(dirX,dirY);
    pause(50);

    ball1.move(dirX1,dirY1);
    pause(50);

    if (ball.getX() >= APPLICATION_WIDTH)
    {
    dirX = -5;
    }
    if (ball1.getY() >= APPLICATION_HEIGHT)
    {
    dirY1 = -5;
    }
    if(ball.getX() <= max)
    {
    dirX= 5;
    }
    if(ball1.getY() <= max1)
    {
    dirY1= 5;
    }

    }
    }
    }

    public void mouseClicked(MouseEvent e) {//note that this is a self contained method


    double x = e.getX();
    double y = e.getY();
    GObject theObject = getElementAt(x,y);

    if(theObject == ball)
    remove(ball);
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to remove the balls on click?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Shrek is offline Member
    Join Date
    May 2015
    Posts
    2
    Rep Power
    0

    Default Re: How to remove the balls on click?

    What?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to remove the balls on click?

    Quote Originally Posted by Shrek View Post
    What?
    Place your code between code tags. The link will show you how. It is also a good idea to read the forum rules prior to posting.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Bouncing Balls Program, List balls do not move
    By VettesRus in forum New To Java
    Replies: 10
    Last Post: 10-01-2013, 04:39 PM
  2. bouncing Balls
    By mystery81 in forum New To Java
    Replies: 4
    Last Post: 09-14-2012, 12:16 AM
  3. Replies: 8
    Last Post: 02-23-2012, 06:08 PM
  4. Replies: 2
    Last Post: 06-22-2011, 01:19 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM

Tags for this Thread

Posting Permissions

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