views:

67

answers:

3

Hi,

I am very new to android development and have been trying to draw a square comprised of multiple smaller rectangles of different colours... Like a Mosaic essentially.

Basically at the moment I am reading values from a file which assigns the colour to the smaller Rects. I am using a pair of nested for loops to try to draw the small Rects sequentially, line by line. However when the program finishes there is only one small Rect drawn which is the last one to be drawn and its colour corresponds to the first value read from the file.

Here is some of my code to show you what I mean:

public SnapshotDraw(Context context) {
    super(context);

    for(int a = 0; a < 63; a++){
        for(int b = 0; b < 63; b++){
            fileName = PREFIX + "2" + EXTENSION;

            try {
                bf = new BufferedReader(new FileReader(fileName));
                tokens = new StringTokenizer(bf.readLine(), " \n");
                weight = Byte.parseByte(tokens.nextToken());

                x_scalar = b*MAG;
                y_scalar = a*MAG;   

                mDrawable = new ShapeDrawable(new RectShape());
                mDrawable.getPaint().setColor(colour.getColour(weight));
                mDrawable.setBounds((X_OFFSET + x_scalar), (Y_OFFSET + y_scalar), ((MAG + X_OFFSET) + x_scalar), ((MAG + Y_OFFSET) + y_scalar));

            } catch (FileNotFoundException ex) {
                Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

protected void onDraw(Canvas canvas) {
    mDrawable.draw(canvas);
}

This except is from a class which extends View and is called inside an onCreate() method in an Activity.

I would appreciate any guidance in this and thanks in advance!!

Cheers.

+2  A: 

You are constructing the BufferedReader inside the loops, so bf.readLine() will always return the same line. Try moving bf and tokens (be aware that the use of StringTokenizer is discouraged) out of the loops.

Tedil
Hi Tedil,Thanks for the reply I never knew they were discouraged!! Why is that?Unfortunately I am just getting null pointer exceptions now...!
travega
@travega: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code." http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
trashgod
I see! That is good to know!! Thanks for the heads up.
travega
A: 

Ok I got it sorted! Here is what I did to solve it:

public SnapshotDraw(Context context) {
    super(context);
    setFocusable(true);

    mBitmap = Bitmap.createBitmap(475, 720, Bitmap.Config.ALPHA_8);
}

@Override 
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);

    Paint p = new Paint();
    float y = 10;

    try {
        fileName = PREFIX + "2" + EXTENSION;
        bf = new BufferedReader(new FileReader(fileName));

        for(int a = 0; a < 63; a++){
            tokens = new StringTokenizer(bf.readLine(), " \n");
            for(int b = 0; b < 63; b++){

                weight = Byte.parseByte(tokens.nextToken());

                x_scalar = b*MAG;
                y_scalar = a*MAG;   

                p.setColor(new Colour().getColour(weight));
                canvas.drawRect((X_OFFSET + x_scalar), (Y_OFFSET + y_scalar), ((MAG + X_OFFSET) + x_scalar), ((MAG + Y_OFFSET) + y_scalar), p);
            }
        } 
    } catch (FileNotFoundException ex) {
        Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(NetworkUtilities.class.getName()).log(Level.SEVERE, null, ex);
    }

    canvas.drawBitmap(mBitmap, 10, y, p);
}

Much the same as before but changed the way I draw to the Bitmap. It looks beautiful btw!!

travega
A: 

its not working yaaar

Peter