Added screen-capture functionality (press D). Added safeguards to prevent NPE when player.txt, DetailedInfo.txt do not exist. Removed these files from source control.
This commit is contained in:
@@ -94,7 +94,8 @@ public class GamePlay implements Serializable {
|
||||
gp = (GamePlay) in.readObject();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
//e.printStackTrace();
|
||||
System.out.println("Unable to read from GamePlay file: " + fileName + ", initializing a new GamePlay instance.");
|
||||
}
|
||||
return gp;
|
||||
}
|
||||
|
||||
@@ -4,15 +4,21 @@ import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.GraphicsConfiguration;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.VolatileImage;
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.swing.JComponent;
|
||||
|
||||
@@ -36,6 +42,7 @@ public class MarioComponent extends JComponent implements Runnable,
|
||||
public static final int GAME_VERSION = 4;
|
||||
|
||||
private boolean running = false;
|
||||
private boolean screenshotTaken = false;
|
||||
private int width, height;
|
||||
private GraphicsConfiguration graphicsConfiguration;
|
||||
private Scene scene;
|
||||
@@ -45,7 +52,8 @@ public class MarioComponent extends JComponent implements Runnable,
|
||||
private LevelGenerator levelGenerator;
|
||||
private Scale2x scale2x = new Scale2x(320, 240);
|
||||
|
||||
public MarioComponent(int width, int height, boolean isCustomized, LevelGenerator levelGenerator) {
|
||||
public MarioComponent(int width, int height, boolean isCustomized,
|
||||
LevelGenerator levelGenerator) {
|
||||
addFocusListener(this);
|
||||
addMouseListener(this);
|
||||
addKeyListener(this);
|
||||
@@ -100,6 +108,14 @@ public class MarioComponent extends JComponent implements Runnable,
|
||||
if (isPressed && keyCode == KeyEvent.VK_F1) {
|
||||
useScale2x = !useScale2x;
|
||||
}
|
||||
if (keyCode == KeyEvent.VK_D) {
|
||||
if (!isPressed) {
|
||||
screenshotTaken = false;
|
||||
} else if (!screenshotTaken) {
|
||||
screenshotTaken = true;
|
||||
takeScreenShot();
|
||||
}
|
||||
}
|
||||
|
||||
if (isPressed && keyCode == KeyEvent.VK_ESCAPE) {
|
||||
try {
|
||||
@@ -110,6 +126,25 @@ public class MarioComponent extends JComponent implements Runnable,
|
||||
}
|
||||
}
|
||||
|
||||
private void takeScreenShot() {
|
||||
System.out.println("Taking screenshot.");
|
||||
Point loc = getLocationOnScreen();
|
||||
|
||||
Rectangle screenRect = new Rectangle(loc.x, loc.y, getWidth(), getHeight());
|
||||
|
||||
try {
|
||||
|
||||
Robot robot = new Robot();
|
||||
BufferedImage bufferedImg = robot.createScreenCapture(screenRect);
|
||||
File tempFile = File.createTempFile("screenshot", ".png", new File("."));
|
||||
ImageIO.write(bufferedImg, "png",
|
||||
tempFile);
|
||||
System.out.println("Screeshot saved to current working directory: " + tempFile.getName());
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace(System.out);
|
||||
}
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
}
|
||||
@@ -148,8 +183,9 @@ public class MarioComponent extends JComponent implements Runnable,
|
||||
float averagePassedTime = 0;
|
||||
|
||||
boolean naiveTiming = true;
|
||||
|
||||
///Not sure I understand the weird dichotomy between LevelScene and LevelInterface...
|
||||
|
||||
// /Not sure I understand the weird dichotomy between LevelScene and
|
||||
// LevelInterface...
|
||||
randomLevel = new LevelSceneCustom(graphicsConfiguration, this,
|
||||
new Random().nextLong(), 0, 0, isCustom, levelGenerator);
|
||||
|
||||
@@ -161,8 +197,8 @@ public class MarioComponent extends JComponent implements Runnable,
|
||||
randomLevel.init();
|
||||
randomLevel.setSound(sound);
|
||||
scene = randomLevel;
|
||||
///
|
||||
|
||||
// /
|
||||
|
||||
while (running) {
|
||||
float lastTime = time;
|
||||
time = (System.nanoTime() - startTime) / 1000000000f;
|
||||
|
||||
@@ -5,33 +5,27 @@ import java.io.FileReader;
|
||||
|
||||
public class FileHandler {
|
||||
|
||||
public static String readFile(String fileName){
|
||||
public static String readFile(String fileName) {
|
||||
String info = "";
|
||||
try {
|
||||
FileReader input = new FileReader(fileName);
|
||||
BufferedReader bufRead = new BufferedReader(input);
|
||||
|
||||
String line;
|
||||
|
||||
int count = 0;
|
||||
line = bufRead.readLine();
|
||||
info = line +"\n";
|
||||
count++;
|
||||
|
||||
|
||||
while (line != null){
|
||||
// System.out.println(count+": "+line);
|
||||
line = bufRead.readLine();
|
||||
info += line + "\n";
|
||||
count++;
|
||||
}
|
||||
|
||||
bufRead.close();
|
||||
|
||||
}catch (Exception e){
|
||||
// If another exception is generated, print a stack trace
|
||||
e.printStackTrace();
|
||||
}
|
||||
return info;
|
||||
|
||||
String line;
|
||||
|
||||
line = bufRead.readLine();
|
||||
info = line + "\n";
|
||||
|
||||
while (line != null) {
|
||||
line = bufRead.readLine();
|
||||
info += line + "\n";
|
||||
}
|
||||
|
||||
bufRead.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Unable to read from file: " + fileName +", returning empty String.");
|
||||
}
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,19 +44,12 @@ public class LevelSceneCustom extends LevelScene {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
//if (level == null)
|
||||
// if (isCustom) {
|
||||
GamePlay gp = GamePlay.read("player.txt");
|
||||
currentLevel = (Level) clg.generateLevel(gp);
|
||||
GamePlay gp = GamePlay.read("player.txt");
|
||||
currentLevel = (Level) clg.generateLevel(gp);
|
||||
|
||||
// You can use the following commands if you want to benefit
|
||||
// from
|
||||
// the interface containing detailed information
|
||||
String detailedInfo = FileHandler.readFile("DetailedInfo.txt");
|
||||
System.out.println("DetailedInfo: " + detailedInfo);
|
||||
// } else
|
||||
// currentLevel = new RandomLevel(320, 15, levelSeed,
|
||||
// levelDifficulty, levelType);
|
||||
String detailedInfo = FileHandler.readFile("DetailedInfo.txt");
|
||||
//TODO parse DetailedInfo
|
||||
System.out.println("DetailedInfo: " + detailedInfo);
|
||||
|
||||
try {
|
||||
level = currentLevel.clone();
|
||||
@@ -64,8 +57,9 @@ public class LevelSceneCustom extends LevelScene {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//TODO change this
|
||||
// level is always overground
|
||||
Art.startMusic(1);
|
||||
Art.startMusic(2);
|
||||
|
||||
paused = false;
|
||||
Sprite.spriteContext = this;
|
||||
|
||||
Reference in New Issue
Block a user