Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is the code that I have with Android Studio in Java so far. However, I run the program and find out that the small

This is the code that I have with Android Studio in Java so far. However, I run the program and find out that the small rectangle that I'm trying to have has a trail when it's moving. I only want to have a moving rectangle without having a trail leaving behind. Any help would be appreciated. Thank you!

//game activity.java

package com.example.pongs21; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class GameActivity extends Activity { private static final String TAG = "Game Activity"; private PongView gameView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); gameView = new PongView(this); setContentView(gameView); gameView.requestFocus(); } } 

//main activity.java

package com.example.pongs21; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "APong"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Set up click listeners for all the buttons View continueButton = findViewById(R.id.continue_button); continueButton.setOnClickListener(this); View newButton = findViewById(R.id.new_button); newButton.setOnClickListener(this); View aboutButton = findViewById(R.id.about_button); aboutButton.setOnClickListener(this); View exitButton = findViewById(R.id.exit_button); exitButton.setOnClickListener(this); } // ... public void onClick(View v) { switch (v.getId()) { case R.id.about_button: Intent i = new Intent(this, About.class); startActivity(i); break; // More buttons go here (if any) ... case R.id.new_button: Intent g = new Intent(this, GameActivity.class); startActivity(g); break; case R.id.exit_button: finish(); break; } } public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.d(TAG, "onConfigurationChanged " + newConfig.orientation); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } 

//PongView.java

 package com.example.pongs21; import java.util.Vector; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.graphics.Paint.Style; import android.graphics.Rect; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.animation.AnimationUtils; public class PongView extends SurfaceView implements SurfaceHolder.Callback, Runnable { private final GameActivity gameActivity; private Thread _thread; private int screenWidth; // screen width private int screenHeight; // screen height Context _context; private SurfaceHolder _surfaceHolder; private boolean _run = false; // All pong variables go in here private float rPaddle; // example variable set by touch event private final static int MAX_FPS = 30; //desired fps private final static int FRAME_PERIOD = 1000 / MAX_FPS; // the frame period private Paint background = new Paint(); private Paint dark = new Paint(); //added // private Paint colorOfBall = new Paint(); // private Paint colorInsideBall = new Paint(); private float ballX = 10.0f; // ball location private float ballY = 10.0f; //added private float xPos = 0.0f; private float yPos = 0.0f; private float xDir = 1.0f; private float yDir = 1.0f; // private float ballXloc = 0.0f; //ball X direction move per update // private float ballYloc = 0.0f; // private float ballXsize = 30.0f; // private float ballYsize = 30.0f; // private float insideBallEdge = 3.0f; public PongView(Context context) { super(context); _surfaceHolder = getHolder(); getHolder().addCallback(this); this.gameActivity = (GameActivity) context; _context = context; setFocusable(true); setFocusableInTouchMode(true); } public void setRPaddle(float rp) { synchronized (_surfaceHolder) { rPaddle = rp; } } @Override public void run() { float avg_sleep = 0.0f; float fcount = 0.0f; long fps = System.currentTimeMillis(); Canvas c; while (_run) { c = null; long started = System.currentTimeMillis(); try { c = _surfaceHolder.lockCanvas(null); synchronized (_surfaceHolder) { // Update game state update(); } // draw image drawImage(c); } finally { // do this in a finally so that if an exception is thrown // during the above, we don't leave the Surface in an // inconsistent state if (c != null) { _surfaceHolder.unlockCanvasAndPost(c); } } float deltaTime = (System.currentTimeMillis() - started); int sleepTime = (int) (FRAME_PERIOD - deltaTime); if (sleepTime > 0) { try { _thread.sleep(sleepTime); } catch (InterruptedException e) { } } } } public void pause() { _run = false; boolean retry = true; while (retry) { try { _thread.join(); retry = false; } catch (InterruptedException e) { // try again shutting down the thread } } } public void initialize(int w, int h) { screenWidth = w; screenHeight = h; // create paints, rectangles, init time, etc background.setColor(0xff200040); // should really get this from resource file dark.setColor(0xffdddddd); } protected void update() { // game update goes here // if (ballY > 15.0f) // { // ballX = 10.0f; // ballY += 1.0f; // } // ballY++; // invalidate(); //canvas.drawRect(xPos, ballY, ballX+10, ballY+10, dark); //xPos += 1.0f; } protected void drawImage(Canvas canvas) { //if (canvas == null) return; int width = getWidth(); int height = getHeight(); // Draw commands go here canvas.drawRect(ballX, ballY, ballX+1000, ballY+1000, background); canvas.drawRect(ballX, ballY, ballX+10, ballY+10, dark); //canvas.drawRect(xPos, ballY, ballX+10, ballY+10, dark); //ballX += 1.0f; ballX = ballX + xDir; // if (ballX>width+20) // { // ballX=-20; // } if (ballX>width-10 || ballX<10) { xDir=-xDir; } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { screenWidth = w; screenHeight = h; super.onSizeChanged(w, h, oldw, oldh); initialize(w, h); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { _run = true; _thread = new Thread(this); _thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // simply copied from sample application LunarLander: // we have to tell thread to shut down & wait for it to finish, or else // it might touch the Surface after we return and explode boolean retry = true; _run = false; while (retry) { try { _thread.join(); retry = false; } catch (InterruptedException e) { // we will try it again and again... } } } @Override public boolean onTouchEvent(MotionEvent event) { setRPaddle(event.getY() / screenHeight); return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions