I need to make a game that demonstates that my logger works, theres 3 different
ID: 3661776 • Letter: I
Question
I need to make a game that demonstates that my logger works, theres 3 different classes.GameManager, Game Engine, and Sound.The GameManager class will have 3 methods Initialize(), PlayGame() and ShutDown(). GameEngine will also have 3 classes Initialize(), Draw() and ShutDown().Sound with also have 3 classes Initialize() PlaySound(), andShutDown(). In the main it will call the GameManager and do gamemanger.Play(), gamemanger.Initialize() and gamemanger.shutdown. The GameManager class initializes the Sound and Game Engine classes, which both just log that they have been initialized, and the the GameManger will aslo call GameEngine.Draw() and Sound.PlaySound() a random numbe of times between 2 and 10. Each of those methods will log that they are called and then once all the rounds are complete it will call the Shutdown() for both of the other classes, which will log that they were shut down.
And here is my logger if needed
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.IllegalFormatConversionException;
import java.util.MissingFormatArgumentException;
import com.sun.jmx.snmp.Timestamp;
enum MessageLevel{Info, FatalError, Warning, Error,NewLevel }
public class Logger {
static BufferedWriter bw;
static{
String logFileName ="LogFile.log";
File file = new File(logFileName);
FileWriter fw = null;
try {
fw = new FileWriter(file.getAbsoluteFile());
} catch (IOException e) {
e.printStackTrace();
}
bw = new BufferedWriter(fw);
}
public void WriteLog(String msg) {
java.util.Date date = new java.util.Date();
System.out.print(msg);
System.out.print(new Timestamp(date.getTime()));
try {
bw.write(msg);
bw.write(new Timestamp(date.getTime()).toString());
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(new Timestamp(date.getTime()));
}
public String GetMsgLevelString(MessageLevel level) {
switch (level) {
case Info:
return "Info: ";
case FatalError:
return "Fatal Error: ";
case Warning:
return "Warning: ";
case Error:
return "Error: ";
}
return "Programmer ERROR: No message";
}
public void Log(MessageLevel level, String msg) {
Logger log = new Logger();
log.WriteLog((String.format(log.GetMsgLevelString(level), msg)));
}
public void Log(MessageLevel level, String msg, Object... value) {
Logger logger = new Logger();
try {
logger.WriteLog(String.format(msg, value) + GetMsgLevelString(level));
} catch (IllegalFormatConversionException e) {
logger.WriteLog(String.format("Programmer Error: Bad Format ")+e.getMessage());
} catch (MissingFormatArgumentException e) {
logger.WriteLog(String.format("Programmer Error: Missing ")+e.getMessage());
}
}
}
Explanation / Answer
// Game Program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math.*;
class GameManager extends JPanel
{
void initialize()
{
setTitle("Game Starts");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
void playGame()
{
initialize();
while(isRunning)
{
long time = System.currentTimeMillis();
update();
draw();
// delay for each frame - time it took for one frame
time = (1000 / fps) - (System.currentTimeMillis() - time);
if (time > 0)
{
try
{
Thread.sleep(time);
}
catch(Exception e){}
}
}
setVisible(false);
}
void shutDown()
{
System.exit(0);
}
}
class Engine extends GameManager
{
void initialize()
{
super.initialize();
}
void draw()
{
Graphics g1 = getGraphics();
Graphics bg = backBuffer.getGraphics();
bg.setColor(Color.WHITE);
bg.fillRect(0, 0, windowWidth, windowHeight);
bg.setColor(Color.BLACK);
bg.drawOval(x, 10, 20, 20);
g1.drawImage(backBuffer, insets.left, insets.top, this);
}
void shutDown()
{
super.shutDown();
}
}
class Sound extends Gamemanager
{
void initialize()
{
super.initialize();
}
void playSound(InputStream source)
{
// use a short, 100ms (1/10th sec) buffer for real-time
// change to the sound stream
int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10);
byte[] buffer = new byte[bufferSize];
// create a line to play to SourceDataLine line;
try {
DataLine.Info info =
new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine)AudioSystem.getLine(info);
line.open(format, bufferSize);
}
catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
// start the line
line.start();
// copy data to the line
try {
int numBytesRead = 0;
while (numBytesRead != -1) {
numBytesRead =
source.read(buffer, 0, buffer.length);
if (numBytesRead != -1) {
line.write(buffer, 0, numBytesRead);
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
// wait until all data is played, then close the line
line.drain();
line.close();
}
}
void shutDown()
{
super.shutDown();
}
}
// Main Class
class TestGame
{
public static void main(String args[])
{
System.out.println("Here, our Game Starts");
GameManager gamemanager=new GameManager();
gamemanager.intialize();
gamemanager.playGame();
// will call the playSound() random number of times between 2 and 10
int x = (int)(Math.random()*10+2);
// load a sound
while(x)
{
Sound s = new Sound("../sounds/voice.wav");
// create the stream to play
InputStream stream =new ByteArrayInputStream(sound.getSamples());
// play the sound
s.playSound(stream);
}
gamemanager.shutDown();
}
}