Pariya
Pariya

Reputation: 223

i dont understand this code

I read Hello Android book and I don't understand some parts of the code.

public class PuzzleView extends View {
    private static final String TAG = "Sudoku" ;
    private final Game game;
    public PuzzleView(Context context) {
    super(context);
    this.game = (Game) context;
    setFocusable(true);
    setFocusableInTouchMode(true);
  }
 // ...
}

private float width; // width of one tile
private float height; // height of one tile
private int selX; // X index of selection
private int selY; // Y index of selection
private final Rect selRect = new Rect();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = w / 9f;
    height = h / 9f;
    getRect(selX, selY, selRect);
    Log.d(TAG, "onSizeChanged: width " + width + ", height "+ height);
    super.onSizeChanged(w, h, oldw, oldh);
 }

super(context); in this code, what does it mean and what does it do?

this.game = (Game) context; why we wrote this? what does it do?

The Android site says that onSizeChanged() function is used for: "This is called during layout when the size of this view has changed" This meant that if to rotate the phone, this function causes the view of program to display true. this is true?

getRect(selX,selY,selRect); what does it mean and what does it do?

Please help me. Cheers.

Upvotes: 1

Views: 236

Answers (2)

StuartLC
StuartLC

Reputation: 107387

For your first question, Subclasses can call super class constructors with a call to super()

i.e.

super(context);

calls the super class constructor:

public View(Context context)

In your second question, this.game = (Game) context; does 2 things. Firstly, it casts context to class Game and then it assigns it to the PuzzleView's game field (private final Game game;).

getRect is declared at the end of the PuzzleView code. It changes the variable rect passed to it.

   private void getRect(int x, int y, Rect rect) {
      rect.set((int) (x * width), (int) (y * height), (int) (x
            * width + width), (int) (y * height + height));

ATaylor has already addressed onSizeChanged. Basically, the code overrides the inherited method, and then 'hooks' in additional functionality, before calling the super method. This is standard practice for overriding virtual methods (i.e. polymorphic behaviour).

Upvotes: 2

ATaylor
ATaylor

Reputation: 2598

As was already stated:

super(context);

will call the function of the same name of the parent class.

Assuming you have this polymorphism: Class Animal

void MakeNoise() {
    System.out.println("Generic Noise");
}

Class Dog extends Animal

void MakeNoise() {
    super();
    System.out.println("Woof");
}

When you're calling MakeNoise from a Dog Object, you'll have two outputs.

Generic Noise (called by 'super') and 'Woof'.

this.game => Most certainly, you need to access the drawing context in that class, and for this you require a context of type 'Game' (I'm not familiar with Android, which is why I'm not sure, what type 'Game' is, but it seems to be compatible to context.

Whenever 'this.game' is accessed from that class, it will access the originally passed context and thereby draw on the surface of the device.

And yes, onSizeChanged would be fired, whenever the size of the view is changed.

Regarding the getRect: No clue, actually, since I'm lacking the prototype or function declaration. But from the looks of it, it will take arbitrary values (since the passed arguments have not been initialized, as far as I can see), and construct a 'Rect' structure from it (X/Y to W/Z)

Upvotes: 2

Related Questions