First, lets eliminate the test
and test2
variables, which are uneeded (we already have scaledstartx
and scaledstarty
). Second keep screenwidth/2
and screenheight/2
in variables to avoid recalculating it over and over (the compiler will already do this, but doing manually the code will become clearer). Third, to increase the readability a bit, lets avoid doing calculations in the parameters.
So, this is your code now:
public void render(Graphics g) {
int halfScreenWidth = screenwidth / 2;
int halfScreenHeight = screenheight / 2;
int scaledStartX = halfScreenWidth - (halfScreenWidth / scale);
int scaledStartY = halfScreenHeight - (halfScreenHeight / scale);
int scaledWidth = screenwidth - scaledStartX;
int scaledHeight = screenheight - scaledStartY;
int locationX = wherex + halfpwidth - halfScreenWidth;
int locationY = wherey + halfpheight - halfScreenHeight;
int spriteStartX = scaledStartX + locationX;
int spriteStartY = scaledStartY + locationY;
int spriteEndX = scaledWidth + locationX;
int spriteEndY = scaledHeight + locationY;
g.drawImage(DrawnLevel, 0, 0, screenwidth, screenheight, spriteStartX,
spriteStartY, spriteEndX, spriteEndY, null);
}
We can improve it by observing that the terms in the spriteblabla
variables are decomposed in terms that have halfScreenWidth
and halfScreenHeight
both adding and subtracting. So, we can achieve a simpler expression. To do so, lets replace the variables in the assignment to the spriteblabla
variables:
public void render(Graphics g) {
int halfScreenWidth = screenwidth / 2;
int halfScreenHeight = screenheight / 2;
int spriteStartX = halfScreenWidth - (halfScreenWidth / scale) + wherex + halfpwidth - halfScreenWidth;
int spriteStartY = halfScreenHeight - (halfScreenHeight / scale) + wherey + halfpheight - halfScreenHeight;
int spriteEndX = screenwidth - (halfScreenWidth - (halfScreenWidth / scale)) + wherex + halfpwidth - halfScreenWidth;
int spriteEndY = screenheight - (halfScreenHeight - (halfScreenHeight / scale)) + wherey + halfpheight - halfScreenHeight;
g.drawImage(DrawnLevel, 0, 0, screenwidth, screenheight, spriteStartX,
spriteStartY, spriteEndX, spriteEndY, null);
}
Now, lets simplify that, since (halfScreenWidth - halfScreenWidth
) and (screenwidth - halfScreenWidth - halfScreenWidth
) are zero:
public void render(Graphics g) {
int halfScreenWidth = screenwidth / 2;
int halfScreenHeight = screenheight / 2;
int spriteStartX = -(halfScreenWidth / scale) + wherex + halfpwidth;
int spriteStartY = -(halfScreenHeight / scale) + wherey + halfpheight;
int spriteEndX = (halfScreenWidth / scale) + wherex + halfpwidth;
int spriteEndY = (halfScreenHeight / scale) + wherey + halfpheight;
g.drawImage(DrawnLevel, 0, 0, screenwidth, screenheight, spriteStartX,
spriteStartY, spriteEndX, spriteEndY, null);
}
Now, moving some common subexpressions into variables:
public void render(Graphics g) {
int halfScaledWidth = screenwidth / 2 / scale;
int halfScaledHeight = screenheight / 2 / scale;
int spriteCenterX = wherex + halfpwidth;
int spriteCenterY = wherey + halfpheight;
int spriteStartX = spriteCenterX - halfScaledWidth;
int spriteStartY = spriteCenterY - halfScaledHeight;
int spriteEndX = spriteCenterX + halfScaledWidth;
int spriteEndY = spriteCenterY + halfScaledHeight;
g.drawImage(DrawnLevel, 0, 0, screenwidth, screenheight, spriteStartX,
spriteStartY, spriteEndX, spriteEndY, null);
}