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:
Woody Folsom
2012-03-16 13:31:15 -04:00
parent 7ec3ac5a9d
commit 1cd007e798
7 changed files with 78 additions and 304 deletions

View File

@@ -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;

View File

@@ -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;
}
}