Testing New GraphicsCompatibility

This commit is contained in:
Sagi Dayan 2016-01-31 11:14:15 +02:00
parent 57575947f4
commit 890b30f4aa
3 changed files with 44 additions and 4 deletions

View File

@ -28,6 +28,7 @@ public class GameEngine extends MouseAdapter {
private Random r;
private int score;
private BufferedImage sceneImage;
private int heighScore;
private Vector<SideScollerBackground> backgrounds; //Background elements
private Vector<AudioClip> successSounds;
@ -58,6 +59,7 @@ public class GameEngine extends MouseAdapter {
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
loadSuccessSounds();
startNewGame();
heighScore = 0;
}
@ -124,6 +126,8 @@ public class GameEngine extends MouseAdapter {
return this.gameOver;
}
public int getHeighScore() { return this.heighScore;}
/**
* Create a new Pipe on a random position.
@ -163,6 +167,8 @@ public class GameEngine extends MouseAdapter {
if (bird.getLocY() >= pHeight - ( bird.getSWidth() + backgrounds.lastElement().getsHeight())|| bird.getLocY() <= 0) {
pipeTimer.stop();
gameOver = true;
heighScore = score > heighScore ? score : heighScore;
}
//pipe out of screen
@ -176,6 +182,7 @@ public class GameEngine extends MouseAdapter {
if (!pipes.isEmpty() && CollisionUtil.collidesWith(bird, pipes.elementAt(0))){
gameOver = true;
pipeTimer.stop();
heighScore = score > heighScore ? score : heighScore;
}
//passed the pipe?
@ -183,6 +190,8 @@ public class GameEngine extends MouseAdapter {
score++;
canScore = false;
successSounds.elementAt(r.nextInt(4)).play();
heighScore = score > heighScore ? score : heighScore;
}
}

View File

@ -117,9 +117,12 @@ public class GamePanel extends JPanel implements Runnable{
g.drawImage(bg_image, 0, 0, this); //draw the background
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
//Update Score
lbl_score.setText("SCORE : " + engine.getScore());
lbl_score.setText("SCORE : " + engine.getScore() + "\tHIGH SCORE : " + engine.getHeighScore());
// g.dispose();
}

View File

@ -51,11 +51,10 @@ public abstract class Sprite {
{
Image tmp = bImage.getScaledInstance(sWidth, sHeight, Image.SCALE_SMOOTH);
BufferedImage bi = new BufferedImage(sWidth, sHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(tmp,0,0,null);
g2d.dispose();
bImage = bi;
g2d.dispose();
bImage = toCompatibleImage(bi);
}
/**
@ -166,4 +165,33 @@ public abstract class Sprite {
}
private BufferedImage toCompatibleImage(BufferedImage image)
{
// obtain the current system graphical settings
GraphicsConfiguration gfx_config = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().
getDefaultConfiguration();
/*
* if image is already compatible and optimized for current system
* settings, simply return it
*/
if (image.getColorModel().equals(gfx_config.getColorModel()))
return image;
// image is not optimized, so create a new image that is
BufferedImage new_image = gfx_config.createCompatibleImage(
image.getWidth(), image.getHeight(), image.getTransparency());
// get the graphics context of the new image to draw the old image on
Graphics2D g2d = (Graphics2D) new_image.getGraphics();
// actually draw the image and dispose of context no longer needed
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
// return the new optimized image
return new_image;
}
}