Results 1 to 3 of 3
  1. #1
    codelock is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Smile Ball program leaving trails

    I have made the following changes in Ball.java as per my requirements but its leaving a trail behind.
    Any help?


    import java.awt.*;

    import javax.swing.*;
    public class Ball extends Thread implements Runnable
    {
    public JPanel box;
    private static final int XSIZE = 30;
    private static final int YSIZE = 30;
    private double offsetX=200;
    private Image i;
    private Graphics doubleG;

    private int k,l,x = 0;
    private int y = 0;

    private int dx = 2;
    private int dy = 2;
    double n,p,m,theta;
    double ang;

    public Ball(JPanel b)
    {
    box = b;

    }

    public void setXCoord(int XIn)
    {x=XIn;}
    public void setYCoord(int YIn)
    {y=YIn;}


    public void draw()
    {


    Graphics g = box.getGraphics();
    g.fillOval(k, l, XSIZE, YSIZE);

    g.dispose();

    }

    public void move()
    {
    ang=theta;
    Graphics g = box.getGraphics();
    g.clearRect(x, y, XSIZE, YSIZE);

    k=x;
    l=y;
    m=(double)(y-350)/(x-600);
    theta=Math.atan(m);

    n=200*Math.cos(theta);
    p=200*Math.sin(theta);

    while(true)
    { if(Math.sqrt((k-600)*(k-600)+(l-350)*(l-350))<200)
    break;

    if(x<600 && y<350){
    k+=5*Math.cos(theta);
    l+=5*Math.sin(theta);
    draw();
    }

    if(x>600 && y<350){
    k-=5*Math.cos(theta);
    l-=5*Math.sin(theta);
    draw();
    }

    if(x<600 && y>350){
    k+=5*Math.cos(theta);
    l+=5*Math.sin(theta);
    draw();
    }

    if(x>600 && y>350){
    k-=5*Math.cos(theta);
    l-=5*Math.sin(theta);
    draw();
    }



    try {
    Thread.sleep(50);
    }
    catch (Exception e) {
    }

    }


    while(true)

    {if(x==600&&y==150)
    break;

    if(x<600 && y<350 || x<600 && y>350){

    k=(int)(-offsetX*Math.cos(ang)+600);
    l=(int)(-offsetX*Math.sin(ang)+350);
    draw();

    ang-=.3;
    }

    else{ k=(int)(offsetX*Math.cos(ang)+600);
    l=(int)(offsetX*Math.sin(ang)+350);
    draw();

    ang-=.3;
    }
    try {
    Thread.sleep(400);
    }
    catch (Exception e) {
    }

    }


    Dimension d = box.getSize();

    if (x < 0)
    {
    x = 0; dx = -dx;
    }

    if (x + XSIZE >= d.width)
    {
    x = d.width - XSIZE; dx = -dx;
    }
    if (y < 0)
    {
    y = 0; dy = -dy;
    }

    if (y + YSIZE >= d.height)
    {
    y = d.height - YSIZE; dy = -dy;
    }

    g.setColor(Color.red);
    g.fillOval(x, y, XSIZE, YSIZE);
    g.dispose();
    }
    public void bounce()
    {
    for (int i = 1; i <= 2000; i++)
    {
    move();

    try
    {

    Thread.sleep(50);
    }
    catch(InterruptedException e)
    {
    System.err.println("InterruptedException" + e.getMessage());
    }
    }
    }

    public void run()
    {
    try
    {
    for (int run=0; run < 5; run++)
    {

    bounce ();
    Thread.sleep((long)(Math.random() * 500));//this is the thread random generator multiply by 500 for 5 seconds

    }
    }
    catch (Exception e)
    {
    System.err.println(e.toString());
    }
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Ball program leaving trails

    Removed from Ball program

    Don't post to old dead threads and don't hijack another poster's thread.

    Go through these and edit your post accordingly:
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Ball program leaving trails

    Never use getGraphics() of a Component. Learn how to perform custom painting the correct way:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

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

Similar Threads

  1. help! bouncing ball program
    By gryd00 in forum New To Java
    Replies: 2
    Last Post: 05-10-2011, 07:58 AM
  2. Ball program
    By codeStone in forum Advanced Java
    Replies: 10
    Last Post: 02-25-2011, 01:32 PM
  3. Bouncing Ball program
    By lost1000 in forum New To Java
    Replies: 3
    Last Post: 11-18-2010, 03:26 PM
  4. Bouncing ball program... having problems..
    By ilovenayoon in forum Java Applets
    Replies: 1
    Last Post: 12-08-2009, 04:16 PM
  5. Image with JWindow -> trails
    By ofir3dvb in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2008, 05:21 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
  •