Hello, and thanks for reading this.
I’m pretty new to Java development and i caught myself here in this AWESOME ;D site were things seems to work better than trying every thing all alone.
I am trying to use a game base engine that i found here in the tutorial section but for some reason i can’t override any class. I create the base class and a Game main class to place the game code separately, than i extended the Game class to the base class this way i could override the update method and the render method. For some weird reason none of those are accepting my overrides :’( .
The code:
---------------------The (modified) Base class
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Base implements Runnable{
int WIDTH = 1024;
int HEIGHT = 768;
JFrame frame;
Canvas canvas;
BufferStrategy bufferStrategy;
private GraphicsDevice gd;
private DisplayMode defaultDM;
public static final DisplayMode DefaultDisplaySet[]={
//640x480
new DisplayMode(640,480,32,0),
new DisplayMode(640,480,16,0),
//800x600
new DisplayMode(800,600,32,0),
new DisplayMode(800,600,16,0),
//1024x768
new DisplayMode(1024,768,32,0),
new DisplayMode(1024,768,16,0),
//1440x900
new DisplayMode(1440,900,32,0),
new DisplayMode(1440,900,16,0)
};
public Base(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = env.getDefaultScreenDevice();
defaultDM = gd.getDisplayMode();
frame = new JFrame("Basic Game");
frame.setUndecorated(true);
gd.setFullScreenWindow(frame);
if (gd.isDisplayChangeSupported())
{
gd.setDisplayMode(checkCompatibleMode(DefaultDisplaySet[7]));
this.WIDTH = gd.getDisplayMode().getWidth();
this.HEIGHT = gd.getDisplayMode().getHeight();
}
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
panel.setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, WIDTH, HEIGHT);
canvas.setIgnoreRepaint(true);
panel.add(canvas);
canvas.addMouseListener(new MouseControl());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
initialize();
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
canvas.requestFocus();
}
//diplay mode handlers
public DisplayMode[] getCompatibleDisplayModes(){
return gd.getDisplayModes();
}
public DisplayMode checkCompatibleMode(DisplayMode mode){
DisplayMode goodModes[] = getCompatibleDisplayModes();
for(int j=0;j<goodModes.length;j++){
if (DisplayModesMatch(mode,goodModes[j])){
return mode;
}
}
return null;
}
//compara displaymodes
public boolean DisplayModesMatch(DisplayMode d1, DisplayMode d2){
if(d1.getWidth() != d2.getWidth() || d1.getHeight() != d2.getHeight()){
return false;
}
if(d1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && d2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && d1.getBitDepth() != d2.getBitDepth()){
return false;
}
if(d1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && d2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && d1.getRefreshRate() != d2.getRefreshRate()){
return false;
}
return true;
}
//initialize - override
protected void initialize(){}
//the keyInput for shutting down
private class KeyInputHandler extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==27)
{
gd.setDisplayMode(defaultDM);
gd.setFullScreenWindow(null);
System.exit(0);
}
}
public void keyReleased(KeyEvent e)
{
}
}
//the mouse listeners
private class MouseControl extends MouseAdapter{
}
long desiredFPS = 100;
long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
boolean running = true;
//the separate thread func
public void run(){
long beginLoopTime;
long endLoopTime;
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
long deltaLoop;
while(running){
beginLoopTime = System.nanoTime();
render();
lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();
update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;
if(deltaLoop > desiredDeltaLoop){
//Do nothing. We are already late.
}else{
try{
Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
}catch(InterruptedException e){
//Do nothing
}
}
}
}
//rendering
private void render() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, WIDTH, HEIGHT);
render(g);
g.dispose();
bufferStrategy.show();
}
int counter = 0;
//updating - overriding
public void update(int deltaTime){
}
//rendering - overriding
public void render(Graphics2D g){
g.setColor(Color.black);
g.fillRect(0, 0, this.WIDTH, this.HEIGHT);
}
//main thread func
public static void main(String [] args){
Base ex = new Base();
new Thread(ex).start();
}
}
---------------------The Main Game Class
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
public class Game extends Base{
BufferedImage bg = null;
int counter = 0;
protected void initialize(){
try {
bg = ImageIO.read(new File("bg.png"));
} catch (IOException e) {}
}
public void update(int deltaTime){
counter++;
if (counter > 100){
counter = 0;
JOptionPane.showMessageDialog(null, "UPDATE FUNCIONANDO."); //NOT WORKING
}
}
public void render(Graphics2D g){
g.setColor(Color.blue);
g.fillRect(0, 0, 1024, 768); //NOT WORKING EITHER
//g.drawImage(bg, 0, 0, canvas);
}
}
