Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:17:54+00:00 2026-06-03T18:17:54+00:00

I’m trying to make a simple paint app. I’m having trouble with the array

  • 0

I’m trying to make a simple paint app. I’m having trouble with the array that stores the paint, and I can’t find a solution. If I comment out the parts that have to do with the array it runs, but then (obviously) the app doesn’t work as expected. When I open up the app it crashes with this code:

package com.example.paint.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class PaintView extends SurfaceView implements Callback {

PaintCircle[] circles = new PaintCircle[10000];

@Override
public boolean onTouchEvent(MotionEvent event) {
    // When touched

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Paint blue = new Paint();
        blue.setColor(Color.BLUE);
        blue.setStyle(Style.FILL);
        circles[circles.length] = new PaintCircle(event.getX(),
                event.getY(), 20, blue);
        break;
    case MotionEvent.ACTION_MOVE:
        Paint green = new Paint();
        green.setColor(Color.GREEN);
        green.setStyle(Style.FILL);
        circles[circles.length] = new PaintCircle(event.getX(),
                event.getY(), 20, green);
        break;
    }

    return true;
}

@Override
public void draw(Canvas c) {
    // Draw to the canvas
    super.draw(c);
    c.drawARGB(255, 255, 255, 255);
    for (int i = 0; i < circles.length; i++) {
        c.drawCircle(circles[i].cx, circles[i].cy, circles[i].radius,
                circles[i].paint);
    }
}

public PaintView(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // When the surface view is created
    Canvas c = getHolder().lockCanvas();
    draw(c);
    getHolder().unlockCanvasAndPost(c);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    Canvas c = getHolder().lockCanvas();
    draw(c);
    getHolder().unlockCanvasAndPost(c);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

}

Here is my PaintCircle class:

package com.example.paint.views;

import android.graphics.Paint;

public class PaintCircle {
public Paint paint;
public float cx, cy;
public int radius;

public PaintCircle() {

}

public PaintCircle(float cx, float cy, int radius, Paint paint) {
    cx = this.cx;
    cy = this.cy;
    radius = this.radius;
    paint = this.paint;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:17:56+00:00Added an answer on June 3, 2026 at 6:17 pm

    circles[circles.length] is the 10001th element of your array since the length is static.

    Better use a List like an ArrayList that grows dynamically:

    List<PaintCircle> circles = new ArrayList<PaintCircle>();
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // When touched
    
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Paint blue = new Paint();
            blue.setColor(Color.BLUE);
            blue.setStyle(Style.FILL);
            circles.add(new PaintCircle(event.getX(),
                    event.getY(), 20, blue));
         // ...
    
    @Override
    public void draw(Canvas c) {
        // Draw to the canvas
        super.draw(c);
        c.drawARGB(255, 255, 255, 255);
        for (PaintCircle circle : circles) {
            c.drawCircle(circle.cx, circle.cy, circle.radius,
                    circle.paint);
        }
    }
    

    Another possibility would be to store a separate index of your array that tells you which is the last element of your array. When you add a circle increase the index and draw just those that are assigned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.