Testing New GraphicsCompatibility
This commit is contained in:
parent
57575947f4
commit
890b30f4aa
|
@ -28,6 +28,7 @@ public class GameEngine extends MouseAdapter {
|
||||||
private Random r;
|
private Random r;
|
||||||
private int score;
|
private int score;
|
||||||
private BufferedImage sceneImage;
|
private BufferedImage sceneImage;
|
||||||
|
private int heighScore;
|
||||||
|
|
||||||
private Vector<SideScollerBackground> backgrounds; //Background elements
|
private Vector<SideScollerBackground> backgrounds; //Background elements
|
||||||
private Vector<AudioClip> successSounds;
|
private Vector<AudioClip> successSounds;
|
||||||
|
@ -58,6 +59,7 @@ public class GameEngine extends MouseAdapter {
|
||||||
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
|
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
|
||||||
loadSuccessSounds();
|
loadSuccessSounds();
|
||||||
startNewGame();
|
startNewGame();
|
||||||
|
heighScore = 0;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -124,6 +126,8 @@ public class GameEngine extends MouseAdapter {
|
||||||
return this.gameOver;
|
return this.gameOver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getHeighScore() { return this.heighScore;}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Pipe on a random position.
|
* 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) {
|
if (bird.getLocY() >= pHeight - ( bird.getSWidth() + backgrounds.lastElement().getsHeight())|| bird.getLocY() <= 0) {
|
||||||
pipeTimer.stop();
|
pipeTimer.stop();
|
||||||
gameOver = true;
|
gameOver = true;
|
||||||
|
heighScore = score > heighScore ? score : heighScore;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//pipe out of screen
|
//pipe out of screen
|
||||||
|
@ -176,6 +182,7 @@ public class GameEngine extends MouseAdapter {
|
||||||
if (!pipes.isEmpty() && CollisionUtil.collidesWith(bird, pipes.elementAt(0))){
|
if (!pipes.isEmpty() && CollisionUtil.collidesWith(bird, pipes.elementAt(0))){
|
||||||
gameOver = true;
|
gameOver = true;
|
||||||
pipeTimer.stop();
|
pipeTimer.stop();
|
||||||
|
heighScore = score > heighScore ? score : heighScore;
|
||||||
}
|
}
|
||||||
|
|
||||||
//passed the pipe?
|
//passed the pipe?
|
||||||
|
@ -183,6 +190,8 @@ public class GameEngine extends MouseAdapter {
|
||||||
score++;
|
score++;
|
||||||
canScore = false;
|
canScore = false;
|
||||||
successSounds.elementAt(r.nextInt(4)).play();
|
successSounds.elementAt(r.nextInt(4)).play();
|
||||||
|
heighScore = score > heighScore ? score : heighScore;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,8 +118,11 @@ public class GamePanel extends JPanel implements Runnable{
|
||||||
|
|
||||||
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
|
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Update Score
|
//Update Score
|
||||||
lbl_score.setText("SCORE : " + engine.getScore());
|
lbl_score.setText("SCORE : " + engine.getScore() + "\tHIGH SCORE : " + engine.getHeighScore());
|
||||||
|
// g.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -51,11 +51,10 @@ public abstract class Sprite {
|
||||||
{
|
{
|
||||||
Image tmp = bImage.getScaledInstance(sWidth, sHeight, Image.SCALE_SMOOTH);
|
Image tmp = bImage.getScaledInstance(sWidth, sHeight, Image.SCALE_SMOOTH);
|
||||||
BufferedImage bi = new BufferedImage(sWidth, sHeight, BufferedImage.TYPE_INT_ARGB);
|
BufferedImage bi = new BufferedImage(sWidth, sHeight, BufferedImage.TYPE_INT_ARGB);
|
||||||
|
|
||||||
Graphics2D g2d = bi.createGraphics();
|
Graphics2D g2d = bi.createGraphics();
|
||||||
g2d.drawImage(tmp,0,0,null);
|
g2d.drawImage(tmp,0,0,null);
|
||||||
g2d.dispose();
|
g2d.dispose();
|
||||||
bImage = bi;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue