Hello i have a little problem with moving ball around screen, also i'm confused why i cannot put this variables double x = 0, y = 16, muvx = 0, muvy = 0; at the public class MainActivity to be local variables and also i don't know how to set the timer to be on 0.5 sec. Timer t = new Timer(this, 5); its shows me error at this line too. Here is the code please explain me what causes the errors and how do i make the ball move, Thank you

public class MainActivity extends Activity  {

static int F_WIDTH = 800;
static int F_HEIGHT = 600;
static int MIN_X = 0; // left - ball
static int MIN_Y = 16; // up - ball
static int MAX_X = 742; // right - ball
static int MAX_Y = 521; // down - ball
static int BALL_SIZE_X = 100;
static int BALL_SIZE_Y = 100;
Timer t = new Timer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new ArcView(this));
    RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.main_layout);

    rlayout.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            right(); // <---- this is the error
        }

    });
}
static class ArcView extends View {
    Paint paint;
    Path path;
    RectF rect;

    public ArcView(Context context) {
        super(context);
        rect = new RectF(50, 50, BALL_SIZE_X, BALL_SIZE_Y);

        paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.CYAN);
        paint.setStrokeWidth(30);

        path = new Path();
        path.addArc(rect, 360, 360);
        setBackgroundColor(Color.WHITE);
    }
    float x = 0, y = 16, muvx = 0, muvy = 0;

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x,y,40, paint);
    }

    public void right() {
        muvx = 2;
        muvy = 0;
    }

    public void actionPerformed() {
        invalidate();
        x += muvx;
        y += muvy;
        if (x < MIN_X) {
            x = MIN_X;
        }
        if (x > MAX_X) {
            x = MAX_X;
        }
        if (y < MIN_Y) {
            y = MIN_Y;
        }
        if (y > MAX_Y) {
            y = MAX_Y;
        }
    }
}}

I read this post, but I'm not even sure what language it's in, but basically you have to repaint/redraw every time you update position.

This is a standard programming assignment I think...Here's my Java version for wehn I took Java upteen million years ago. (I never delete code...haha)

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;

public class Checkers extends java.applet.Applet implements Runnable {

  Thread runner;
  int xpos;
  Image offscreenImg;
  Graphics offscreenG;

  public void init() {
    offscreenImg = createImage(this.size().width,                                     this.size().height);
    offscreenG = offscreenImg.getGraphics();
  }

  public void start() {
    if (runner == null); {
      runner = new Thread(this);
      runner.start();
    }
  }

  public void stop() {
    if (runner != null) {
      runner.stop();
      runner = null;
    }
  }

  public void run() {
    while (true) {
      for (xpos = 5; xpos <= 105; xpos+=4) {
        repaint();
        try { Thread.sleep(100); }
        catch (InterruptedException e) { }
      }
     xpos = 5;
    }
  }

  public void update(Graphics g) {
     paint(g);
  }


  public void paint(Graphics g) {
    // Draw background onto the buffer area
    offscreenG.setColor(Color.black);
    offscreenG.fillRect(0,0,100,100);
    offscreenG.setColor(Color.white);
    offscreenG.fillRect(100,0,100,100);
    // Draw checker
    offscreenG.setColor(Color.red);
    offscreenG.fillOval(xpos,5,90,90);

    // Now, transfer the entire buffer onto the screen
    g.drawImage(offscreenImg,0,0,this);
  }

  public void destroy() {
     offscreenG.dispose();
  }
}

It's not a ball, it's checkers...but the concept is the same. (obviously we don't do your coding assingments for you, right?) :-)

have a great day...good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.