Dexkill

LD29

My Base Java Window Code

“Base code and personal code libraries are allowed, but should be declared and shared with the community prior to beginning your entry. To do this, make a blog post.” – LD Rules

The rules told my to do that.

My Basic Java Gamewindow (which i would use for every Game):


Main.java:
public class Main {

public static void main(String[] args) {

Game.initiate();

}

}


Game.java:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{

private static final long serialVersionUID = 1L;

private JFrame jframe;

private boolean paused = false;
private boolean running;
private int FPS = 0;
private int TICKS = 0;

public static final int WIDTH = 800;
public static final int HEIGHT = WIDTH/16*9;
public static final int SCALE = 1;
public static final String NAME = “Game Name”;
public static final String VERSION = “0.0_1 pre-alpha”;

public static Game gameInstance;

public static Game getGame(){
return gameInstance;
}

public static Game initiate(){

gameInstance = new Game();
gameInstance.start();

return gameInstance;
}

public Game(){

Canvas canvas = this;

canvas.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
canvas.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
canvas.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

//canvas.addKeyListener(inputListener);
//canvas.addMouseListener(inputListener);
//canvas.addMouseMotionListener(inputListener);
//canvas.addMouseWheelListener(mainMouseListener);

this.jframe = new JFrame(NAME + ” ” + VERSION);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLayout(new BorderLayout());
jframe.add(canvas, BorderLayout.CENTER);

//jframe.setUndecorated(true);

//jframe.setExtendedState(JFrame.MAXIMIZED_BOTH);

jframe.setResizable(false);

jframe.setVisible(true);

jframe.pack();

jframe.setLocationRelativeTo(null);
}

public int tickCount = 0;

private boolean sysStats = true;

public boolean isSysStats() {
return sysStats;
}

public void setSysStats(boolean sysStats) {
this.sysStats = sysStats;
}

public synchronized void start(){

this.running = true;
new Thread(this).start();
}

public synchronized void stop(){
this.running = false;
}

@Override
public void run() {

long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60; // 60 = MaxFrames

int ticks = 0;
int frames = 0;

long lastTimer = System.currentTimeMillis();
double delta= 0;

init();

while(this.running){
long now = System.nanoTime();

delta += (now – lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;

while (delta >= 1){

if(!paused){

ticks++;
tick();

}

delta -= 1;
shouldRender = true;
}

try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}

if(shouldRender){
frames++;
render();
}

if(System.currentTimeMillis() – lastTimer >= 1000){
lastTimer += 1000;
this.FPS = frames;
this.TICKS = ticks;
frames = 0;
ticks = 0;
}

}

System.exit(0);
}

private void init() {

//GamePlayer player = new GamePlayer(“Powerman3540”);
//Handlers.getHandler().playerHandler.setActivePlayer(player);
//Handlers.getHandler().ingamePlayerTeamHandler.registerPlayerTeam(new PlayerTeam(1, player, “Black Label”));
}

public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}

Graphics2D g = (Graphics2D) bs.getDrawGraphics();

g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());

//float scale = (getWidth()*getHeight())/(2052000F/2);

g.scale(1, 1);

draw(g);

if(this.sysStats)drawSysStats(g);

g.dispose();
bs.show();

}

private void drawSysStats(Graphics2D g){

g.setColor(Color.BLACK);
g.drawString(“FPS: ” + this.FPS + ” | TICK/PS ” + this.TICKS, 11, 11);
g.setColor(Color.WHITE);
g.drawString(“FPS: ” + this.FPS + ” | TICK/PS ” + this.TICKS, 10, 10);

}

public void tick(){
this.tickCount ++;

}

private void draw(Graphics2D g) {
//Background
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, this.getWidth(), this.getHeight());

}

public BufferedImage getImage(Class classx, String path){
BufferedImage img;

try {
img = ImageIO.read(new File(path));
} catch (IOException e) {
img = null;
System.out.println(“‘” + classx.getName() + “‘ : ” + path + ” – Do not exist!”);
}

return img;

}

public Rectangle getScreenRect(){
return new Rectangle(0, 0, WIDTH, HEIGHT);
}

}


 

LD30

Own Java Framework *_*

Hey all,
Wrote in the last 2 month a Java-Framweork called HoodJE, i’ll work for the next 24 on it to make everything work and then i will write my final post :)For everybody who is interrested, here the Github project:> GitHub Repositorie

Here is the plan:  – Stand up at 6am (German time)  – See the Theme  – Go into my village to by some energie drinks *~*  – Come back and freakin program the sh*t out of my life 😀 I will use:  – Java with my Framework witch is based on pure Java 

– Gimp or Photoshop – Windows 8 PC


 

And I’ll stream! -> Twitch.tv/xShootCraftx – so follow me to dont miss it 😉

( I will take some guys to Skype with me while programm and Stream o_O )


Hope I will have fun, hope u will have some too and the best would be if we would have some together on the Stream 😀

*~* STREAM!!!

I’ll Stream the hole time I programm, so if you are bored or just wanna have some fun, visit my

link:

Stream

Hope to see u!

I’m so done…

I’m done for today, it was a disapoining one.. we have currently 00:35 (a.m.) in Germany and LD’s Theme will be anounced in 2h

At Sunnday I have to help my Handball-Team out a bit and to handle everything will be hard.. so I’ll be back in 6.30 (3h after anouncement).

I’m Single .. YEAA!! …..

If I’m think on tomorrow / ludumdare I get  a wired nice feeling, hope I can sleep 😉

HAVE FUN!

Btw, i will use my own Framework, so if you also want to use it here is a GitHub repos:

HoodJE

See u, ur Dexkill/xShootCraftx

ARE U READY??! *-*

Checklist:

– Everything done? [CHECK ]

OKAY LET’S GO!

My Developement Room:

DSC_0594

patrick-are-you-ready-to-party-tonight

Visit my Stream! 😉