Platform game rendering method This copies the level graphics but uses a scale parameter. If the scale is 1, it copies a screen size segment. If it's 2, it copies a half-screen size piece and scales it to the screen size, etc.

How can I optimise this game level code or achieve the same outcome but simpler?

 public void render(Graphics g){

    int test = (screenwidth/2)-((screenwidth/2)/scale); 
    int test2 = (screenheight/2)-((screenheight/2)/scale); 

    int scaledstartx=test;
    int scaledstarty=test2;
    int scaledwidth=screenwidth-test;
    int scaledheight=screenheight-test2;
    int locationx=(wherex)+halfpwidth-(screenwidth/2);
    int locationy=(wherey)+halfpheight-(screenheight/2);        

    g.drawImage(DrawnLevel,0,0,screenwidth,screenheight,
    scaledstartx+locationx,scaledstarty+locationy, scaledwidth+locationx,scaledheight+locationy, null );    
 }
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);
 }