Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: paint brush not leaving trail?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default paint brush not leaving trail?

    Hey, I'm new to android, and I just started fiddling with some simple example code I found online to get some practice. I found some code that has a red dot follow your finger, but I'd like to make the dot leave a trail like a paint brush. It looks like the program erases the previous paint area and puts down a new dot at your finger each frame; I made the 'V' in the upper left corner move to find that. Anyone have any clues?

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup.LayoutParams;
     
    public class MainActivity extends Activity implements OnTouchListener  {
        private float x;
        private float y;
        private int moveX;
        Paint paint = new Paint();
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            MyCustomPanel view = new MyCustomPanel(this);
     
            ViewGroup.LayoutParams params = 
                                new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                                                           LayoutParams.FILL_PARENT);
            addContentView(view, params);
            view.setOnTouchListener(this);
     
        }
        private class MyCustomPanel extends View {
     
            public MyCustomPanel(Context context) {
                super(context);
     
            }
            @Override
            public void onDraw(Canvas canvas) {
     
                paint.setColor(Color.GREEN);
                paint.setStrokeWidth(6);
     
                canvas.drawLine(moveX,10,50,50,paint);
                paint.setColor(Color.RED);
     
                canvas.drawLine(50, 50, 90, 10, paint);
                canvas.drawCircle(50, 50, 3, paint);
                moveX++;
                canvas.drawCircle(x,y,3,paint);
            }
        }
        public boolean onTouch(View v, MotionEvent event) {
            x = event.getX();
            y = event.getY();
            v.invalidate();
            return true;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: paint brush not leaving trail?

    Can you save the previous positions in a list and draw them from the list.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: paint brush not leaving trail?

    After the user starts drawing for a while, won't it need more and more resources to remember all the points, and slow it down?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: paint brush not leaving trail?

    If you don't remember previous points, how can you draw them?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: paint brush not leaving trail?

    ...By slightly changing the graphic each time a touchEvent is invoked, then displaying it? Wait, do all graphics remember the previous points and then display them like that?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: paint brush not leaving trail?

    Sorry, I don't know how Android works.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: paint brush not leaving trail?

    Quote Originally Posted by JavaInProgress View Post
    ...By slightly changing the graphic each time a touchEvent is invoked, then displaying it?
    If you just want to maintain a screenshot (so to speak) of the canvas, that will work, but there would be no way to undo what has been done.

    Quote Originally Posted by JavaInProgress View Post
    Wait, do all graphics remember the previous points and then display them like that?
    You would have to store the points somewhat in this manner to support an undo feature. Otherwise how would the program determine the previous color of any pixel that is overwritten?

    Both work, which one suits your needs? (or a combination of the two)

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: paint brush not leaving trail?

    I was planning on a combo of the two. It would remember the last 10 or so modifications, then when a new modification is made, the last in the series is permanently added to the canvas.

Similar Threads

  1. Help! Image leaving marks
    By nivangerow in forum AWT / Java Swing
    Replies: 5
    Last Post: September 10th, 2011, 03:00 PM
  2. Moving sprite leaves a trail
    By chris2307 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 26th, 2011, 09:01 AM

Tags for this Thread