Hello All.

Iv'e been writing a simple drawing App as homework for university.
Drawing works fine, but the canvas should also be cleared when the App
is coming back from the background. When switching bach i get the
following:

IllegalThreadStateException: Thread already started.
at java.lang.Thread (Thread.java: 1284)

and directed to line 134 in my code:

class Panel extends SurfaceView implements SurfaceHolder.Callback {

//some code

                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                        synchronized (_thread) {
                                if (!_thread.getRunning()) {
                                        _thread.setRunning(true);
                                        _thread.start();    /////←←←← LINE 134 
!!!
                                }
                        }
                }


Thanks for your helping !!

Amit from Berlin



**** HERE IS MY COMPLETE CODE : ******** (All is in one file)

package de.fu.amit;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SimpleDraw extends Activity {


        private final int MAX_WIDTH = 20;
        private DrawingThread _thread;
        private Panel panel;
        private int backgroundColor;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //second parameter defines the background color
                backgroundColor = new Random().nextInt();
                panel = new Panel(this, backgroundColor);
                setContentView(panel);
        }

        @Override
        protected void onStop() {
                panel.clearPanel();
                super.onStop();
        }

        @Override
        protected void onRestart() {
                super.onRestart();
        }

        class Panel extends SurfaceView implements SurfaceHolder.Callback {

                private Line startedLine;
                private Paint linePaint;
                private int backgroundColor;
                private ArrayList<Line> lines = new ArrayList<Line>();

                public Panel(Context context, int backgroundColor) {
                        super(context);
                        getHolder().addCallback(this);
                        _thread = new DrawingThread(getHolder(), this);
                        setFocusable(true);
                        this.backgroundColor = backgroundColor;
                }

                protected void clearPanel(){
                        lines = new ArrayList<Line>();
                }

                private void pickColors() {
                        Random r = new Random();
                        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                        linePaint.setColor(r.nextInt());
                        linePaint.setStrokeWidth(Math.abs(r.nextInt()) % 
MAX_WIDTH);
                        linePaint.setStyle(Style.STROKE);
                }


                @Override
                public boolean onTouchEvent(MotionEvent event) {
                        Log.i("Simple-Draw:", event.toString() + " x:" + 
event.getX()
                                        + " y:" + event.getY());

                        synchronized (_thread.getSurfaceHolder()) {
                                if (event.getAction() == MotionEvent.ACTION_UP) 
{
                                        return true;
                                } else if (event.getAction() == 
MotionEvent.ACTION_DOWN) {
                                        /**
                                         * on a finger-down even, new color 
will be picked for the
                                         * next line means, if a line has 
already been started it
                                         * will keep it's color and the new 
color will be valid for
                                         * the lines to come.
                                         */
                                        pickColors();
                                }

                                if (startedLine == null || 
startedLine.isComplete()) {
                                        /**
                                         * creating a new line in case no line 
has been started.
                                         * with the created class attribute 
linePaint
                                         */
                                        startedLine = new Line(event.getX(), 
event.getY(),
                                                        linePaint);
                                } else { // we have a non completed line, so we 
complete it
                                        startedLine.setEnd(event.getX(), 
event.getY());
                                        // and add to the list
                                        lines.add(startedLine);

                                        startedLine = 
startedLine.getContinue(linePaint);
                                }

                                return true;
                        }
                }

                @Override
                public void onDraw(Canvas canvas) {
                        canvas.drawColor(backgroundColor);
                        /*
                         * Bitmap bitmap; Coordinates coords;
                         */

                        for (Line line : lines) {
                                canvas.drawLine(line.x_start, line.y_start, 
line.x_end,
                                                line.y_end, line.getPaint());
                        }
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int
width,
                                int height) {
                        // TODO Auto-generated method stub
                }

                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                        synchronized (_thread) {
                                if (!_thread.getRunning()) {
                                        _thread.setRunning(true);
                                        _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;
                        _thread.setRunning(false);
                        while (retry) {
                                try {
                                        _thread.join();
                                        retry = false;
                                } catch (InterruptedException e) {
                                        Log.e("caught an exception: 
surfaceDestroyed()",e.toString());
                                }
                        }
                }

        }

        class DrawingThread extends Thread {
                private SurfaceHolder _surfaceHolder;
                private Panel _panel;
                private boolean _run = false;

                public DrawingThread(SurfaceHolder surfaceHolder, Panel panel) {
                        _surfaceHolder = surfaceHolder;
                        _panel = panel;
                }

                public SurfaceHolder getSurfaceHolder() {
                        return _surfaceHolder;
                }

                public boolean getRunning() {
                        return _run;
                }

                public void setRunning(boolean run) {
                        _run = run;
                }

                @Override
                public void run() {
                        Canvas c;
                        while (_run) {
                                c = null;
                                try {
                                        c = _surfaceHolder.lockCanvas(null);
                                        synchronized (_surfaceHolder) {
                                                _panel.onDraw(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);
                                        }
                                }
                        }
                }
        }

        class Line {
                private Float x_start;
                private Float y_start;
                private Float x_end;
                private Float y_end;
                private Paint paint;

                protected Line(Float x, Float y, Paint paint) {
                        this.x_start = x;
                        this.y_start = y;
                        this.paint = paint;
                }

                public Paint getPaint() {
                        return paint;
                }

                protected Line(Float x_start, Float y_start, Float x_end, Float
y_end,
                                Paint paint) {
                        this.x_start = x_start;
                        this.y_start = y_start;
                        this.x_end = x_end;
                        this.y_end = y_end;
                        this.paint = paint;
                }

                protected void setEnd(Float x, Float y) {
                        this.x_end = x;
                        this.y_end = y;
                }

                protected Line getContinue(Paint paint) {
                        return new Line(x_end, y_end, paint);
                }

                protected boolean isComplete() {
                        return ((x_end != null) && (y_end != null) && (x_start 
!= null) &&
(y_start != null));
                }

                @Override
                public String toString() {
                        return "line: Start(" + x_start.toString() + ","
                                        + y_start.toString() + ") End: (" + 
x_end.toString() + ","
                                        + y_end.toString() + ")";
                }
        }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to