Reputation: 392
I want to make a graphical representation of an ultrasonic sensor's distance data(not yet implemented)
I want to draw a rectangle representing the gokart, with 6 lines in front and 6 in the back. The code seems to be butchered badly, due to my newbie java knoweledge. When testing out the line drawing methods, the program won't start. Any ideas why?
package com.example.gokartdst;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
private RefreshThread drawThread;
public static int screenWidth;
public static int screenHeight;
private float density;
public static float tX;
public static float tY;
public int i;
public int j;
private GoKart[][] detectors;
public GameView(Context context, float density) {
super(context);
this.density = density;
detectors=new GoKart[1][5];
for(i=0;i<=1;i++)
{
for(j=0;j<=5;j++)
{
detectors[i][j]=new GoKart(i,j);
}
}
//detectors = new GoKart(Color.WHITE, 15.0, dpTodx(30));
getHolder().addCallback(this);
drawThread = new RefreshThread(getHolder(), this);
}
/*public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
}
*/
// Itt számolja újra a szükséges pozíciókat
public void update(Canvas c) {
if (c != null) {
screenWidth = c.getWidth();
screenHeight = c.getHeight();
}
//detectors.BallPosUpdate();
}
//Újrarajzolás
@Override
public void onDraw(Canvas canvas) {
if (canvas != null) {
canvas.drawColor(Color.BLACK);
for(i=0;i<=1;i++)
for(j=0;j<=5;j++)
{
detectors[i][j].BallDraw(canvas);
}
//Log.i("onDraw", "onDraw");
}
}
public int dpTodx(int dp) {
int px = (int) (dp*density);
return px;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(drawThread!=null)
{
drawThread.setRunning(true);
drawThread.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
if(drawThread!=null)
{
drawThread.setRunning(false);
while (retry) {
try {
drawThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
// A RefreshThread a fő szál, hogy ne az UI szálon fusson. Így gyorsabb.
class RefreshThread extends Thread {
private SurfaceHolder mSurfaceHolder;
private GameView gameView;
private boolean enabled = false;
public RefreshThread(SurfaceHolder surfaceHolder, GameView panel) {
mSurfaceHolder = surfaceHolder;
gameView = panel;
}
public void setRunning(boolean run) {
enabled = run;
}
@Override
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
final double ns = 1000000000.0 / 60.0;
double delta = 0;
int frames = 0;
int updates = 0;
Canvas c;
while (enabled) {
c = null;
long now = System.nanoTime();
delta += (now - lastTime) / ns;
//Log.i("DELTA", Double.toString(delta));
lastTime = now;
try {
c = mSurfaceHolder.lockCanvas(null);
while (delta >= 1) {
synchronized (mSurfaceHolder) {
if (gameView != null) {
gameView.update(c);
updates++;
delta--;
}
}
}
gameView.onDraw(c);
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
Log.i("Frames", "Frames: " + frames + " | Updates: " + updates);
updates = 0;
frames = 0;
}
} finally {
// igy biztos nem hagyjuk
// inkonzisztens állapotban a Surface-t
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
}
The Gokart object:
package com.example.gokartdst;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.Paint;
public class GoKart {
private int i;
private double j;
private int dst;
ShapeDrawable mLinesDrawable;
Paint paint= new Paint();
public GoKart(int ix, double jx) {
this.i = ix;
this.j = jx;
mLinesDrawable = new ShapeDrawable(new RectShape());
}
public void BallDraw(Canvas canvas) {
/*mLinesDrawable.setBounds(X - diameter / 2, Y - diameter / 2, X + diameter / 2, Y + diameter / 2);
mLinesDrawable.getPaint().setColor(color);
mLinesDrawable.draw(canvas);
*/
if(i==0)
{
if(j==1)
{
canvas.drawLine(30, 30, 40, 30, paint);
}
if(j==2)
{
canvas.drawLine(40, 30, 50, 30, paint);
}
}
}
And the Logkat:
09-20 11:01:29.320: W/dalvikvm(5277): threadid=1: thread exiting with uncaught exception (group=0x415efba8)
09-20 11:01:29.320: E/AndroidRuntime(5277): FATAL EXCEPTION: main
09-20 11:01:29.320: E/AndroidRuntime(5277): Process: com.example.soccergame, PID: 5277
09-20 11:01:29.320: E/AndroidRuntime(5277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.soccergame/com.example.gokartdst.GameActivity}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.os.Handler.dispatchMessage(Handler.java:102)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.os.Looper.loop(Looper.java:136)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-20 11:01:29.320: E/AndroidRuntime(5277): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 11:01:29.320: E/AndroidRuntime(5277): at java.lang.reflect.Method.invoke(Method.java:515)
09-20 11:01:29.320: E/AndroidRuntime(5277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-20 11:01:29.320: E/AndroidRuntime(5277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-20 11:01:29.320: E/AndroidRuntime(5277): at dalvik.system.NativeStart.main(Native Method)
09-20 11:01:29.320: E/AndroidRuntime(5277): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
09-20 11:01:29.320: E/AndroidRuntime(5277): at com.example.gokartdst.GameView.<init>(GameView.java:36)
09-20 11:01:29.320: E/AndroidRuntime(5277): at com.example.gokartdst.GameActivity.onCreate(GameActivity.java:17)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.Activity.performCreate(Activity.java:5231)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-20 11:01:29.320: E/AndroidRuntime(5277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-20 11:01:29.320: E/AndroidRuntime(5277): ... 11 more
Upvotes: 1
Views: 727
Reputation: 6942
your array detectors
is of 5 length
for(j = 0; j <= 5; j++) {
detectors[i][j] = new GoKart(i, j);
}
while you are assigning values till 6th index as condition in for loop is j <= 5
make it to j < 5
It should be
for(j = 0; j < 5; j++) {
detectors[i][j] = new GoKart(i, j);
}
Upvotes: 1
Reputation: 2860
It's because of your for loop, it should be
for(i=0;i<1;i++)
{
for(j=0;j<5;j++)
{
detectors[i][j]=new GoKart(i,j);
}
}
Note I changed both the outside and the innerloop. Both stop conditions should use the < operator not the <=
Whenever you create an array (or in this case 2D array) of size N, you should iterate only so long as i (or j) is smaller than N, because if you will keep going when i <= N
it will also continue to i = N + 1
which will cause an ArrayIndexOutOfBoundsException which basically says you are asking for a value that is outside of the bounds of the array.
Upvotes: 1