enoze
enoze

Reputation: 35

Drawing multiple circles Android

I have been trying to draw multiple circles in Android but it won't work and I can't see what I'm doing wrong. One circle works just fine when I run it. I have tried to find similar questions but the ones I found seemed to be a little more advanced than what I want in this simple app.

public class DrawCharsActivity extends Activity {

    Paint[] paint;
    double lon = 30;
    double lat = 20;
    int scrWidth;
    int scrHeight;
    int x;
    int y;
    //String latitudeString[] = new String[]{"30", "20"};
    //String longitudeString[]= new String[]{"30", "20"};
    String name[] = new String[]{"joseph", "jj"};
    Double longitude[] = new Double[]{30.2, 30.2};
    Double latitude[] =new Double[]{30.2, 45.2};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(new Panel(this));


        //    x = (int) ((scrWidth / 360.0) * (180 + lon));
        //y = (int) ((scrHeight / 180.0) * (90 - lat));

        scrWidth = getWindowManager().getDefaultDisplay().getWidth();
        scrHeight = getWindowManager().getDefaultDisplay().getHeight();
    }

    class Panel extends View {

        public Panel(Context context) {
            super(context);
        }

        @Override
        public void onDraw(Canvas canvas) {
            for (int i = 0; i < name.length; i++) {
                paint = new Paint[i];
                paint[i].setColor(Color.BLACK);
                paint[i].setStrokeWidth(1);
                paint[i].setTextSize(20);

                //longitude[i] = Double.parseDouble(latitudeString[i]);
                //latitude[i] = Double.parseDouble(longitudeString[i]);
                x = (int) ((scrWidth / 360.0) * (180 + longitude[i]));
                y = (int) ((scrHeight / 180.0) * (90 - latitude[i]));

                canvas.drawColor(Color.WHITE);
                canvas.drawCircle(x, y, 3, paint[i]);

                System.out.println(x +  "x" + name[i]);
                System.out.println(y + "y" + name[i]);

            }


            // canvas.drawLine(80, 80, 80, 200, paint);
            // canvas.drawText(""+canvas.getWidth()+", "+canvas.getHeight(), 0,
            // 200,paint);
        }
    }

}

Upvotes: 0

Views: 3414

Answers (2)

enoze
enoze

Reputation: 35

I solved the problem, I set the class Panel as Panel panel in my DrawCharsActivity. Then instead of writing:

setContentView(new Panel(this)); 

I wrote:

panel = new Panel(this); 
panel.setBackgroundColor(Color.WHITE); 
setContentView(panel); 

Upvotes: 1

Alejandro Cumpa
Alejandro Cumpa

Reputation: 2363

how about this:

 // Create an array list
      ArrayList name = new ArrayList();
      // add elements to the array list
      name.add("Joseph");
      name.add("jj");
for (String s : name) {
paint = new Paint[i];
paint[i].setColor(Color.BLACK);
paint[i].setStrokeWidth(1);
paint[i].setTextSize(20);

        //longitude[i] = Double.parseDouble(latitudeString[i]);
        //latitude[i] = Double.parseDouble(longitudeString[i]);
        x = (int) ((scrWidth / 360.0) * (180 + longitude[i]));
        y = (int) ((scrHeight / 180.0) * (90 - latitude[i]));

        canvas.drawColor(Color.WHITE);
        canvas.drawCircle(x, y, 3, paint[i]);

    System.out.println(x +  "x" + name[i]);
    System.out.println(y + "y" + name[i]);

}

Upvotes: 0

Related Questions