
Untitled
By: a guest on Feb 23rd, 2009 | syntax:
Java | size: 1.80 KB | hits: 68 | expires: Never
public void render()
{
//System.out.printf("Width: %d Height: %d\n", getWidth(), getHeight());
if (strategy == null || strategy.contentsLost())
{
// Create BufferStrategy for rendering/drawing
createBufferStrategy(2);
strategy = getBufferStrategy();
Graphics g
= strategy.
getDrawGraphics();
}
// Turn on anti-aliasing
// Render Background
this.
g2.
setColor(Color.
BLACK);
this.g2.fillRect(0, 0, getWidth(), getHeight());
// Render Game Objects
for(int i = 0; i < ballCount; i++)
{
balls[i].draw(this.g2);
}
Ball tempBall = currentBall;
if (tempBall != null) tempBall.draw(this.g2);
// Render Foreground (text, etc)
// Draw Power Arrow and Speed Text along arrow if we are launching a ball
Arrow tempArrow = powerArrow;
if (tempArrow != null)
{
tempArrow.draw(this.g2);
// Power Arrow Magnitude Text
this.
g2.
setColor(Color.
WHITE);
this.
g2.
drawString(String.
format("%.2f px/s", tempArrow.
getLength() * arrowScale, 2
),
(tempArrow.
getX2() + tempArrow.
getX1())/2,
(tempArrow.
getY1() + tempArrow.
getY2())/2
);
}
// Display Help Text in center if no balls
if (ballCount == 0 && currentBall == null)
{
String helpString
= "Click and drag your mouse to launch a ball.";
this.
g2.
setColor(Color.
WHITE);
this.g2.drawString(helpString, getWidth()/2 - (this.g2.getFontMetrics().stringWidth(helpString)/2), getHeight()/2);
}
// Draw our framerate and ball count
this.
g2.
setColor(Color.
WHITE);
this.g2.drawString("FPS: " + currentFrameRate + " Balls: " + ballCount, 15, 15);
if (!strategy.contentsLost()) strategy.show();
}