Hallo,
Ich versuche eine Schrift zum bewegen zu bringen, zusammen mit ein youtube Tutorial. Die Schrift, die in der GameScreen Klasse in der Methode printComponent() implementiert wurde, was sichtbar sein sollte, wird überhaupt nicht sichtbar.
Obwohl ich alles genauso abgeschrieben habe wie im Tutorial und mehrmals den Code kontrolliert habe.. Was ist falsch?
(bin ein Mac User und programmiere mit NetBeans)
Das ist der Link zum Tutorial (5 Episoden) :
https ://www.youtube.com/watch?v=3ElBMn_hxA0
Ich versuche eine Schrift zum bewegen zu bringen, zusammen mit ein youtube Tutorial. Die Schrift, die in der GameScreen Klasse in der Methode printComponent() implementiert wurde, was sichtbar sein sollte, wird überhaupt nicht sichtbar.
Obwohl ich alles genauso abgeschrieben habe wie im Tutorial und mehrmals den Code kontrolliert habe.. Was ist falsch?
(bin ein Mac User und programmiere mit NetBeans)
Das ist der Link zum Tutorial (5 Episoden) :
https ://www.youtube.com/watch?v=3ElBMn_hxA0
Java:
package common;
import gui.GameFrame;
public class Main {
public static GameFrame frame;
public static void main(String[] args) {
gameInit();
gameLoop();
gameClose();
}
private static long timer = System.currentTimeMillis();
private static void gameLoop(){
while (true){
if(timer <= System.currentTimeMillis()){
frame.updateTick();
timer = System.currentTimeMillis() + 50;
}
frame.screen.repaint();
}
}
private static void gameInit(){
frame = new GameFrame("Neues Spiel");
}
private static void gameClose(){
}
}
Java:
package gui;
import javax.swing.JFrame;
public class GameFrame extends JFrame {
private static final long serialVersionUID = 1L;
public GameScreen screen;
public KeyInputHandler gameSetting;
public GameFrame(String title){
super(title);
this.setBounds(0, 0, 1280, 720);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setFocusable(true);
this.setVisible(true);
gameSetting = new KeyInputHandler();
this.addKeyListener(gameSetting);
screen = new GameScreen();
screen.setSize(this.getWidth(), this.getHeight());
this.add(screen);
}
public void updateTick(){
if(gameSetting.keyBindForward) screen.y --;
if(gameSetting.keyBindBack) screen.x ++;
if(gameSetting.keyBindLeft) screen.x --;
if(gameSetting.keyBindRight) screen.y ++;
}
}
Java:
package gui;
import java.awt.Graphics;
import javax.swing.JLabel;
public class GameScreen extends JLabel {
private static final long serialVersionUID = 1L;
public int x = this.getWidth() / 2;
public int y = this.getHeight() / 2;
public GameScreen(){
this.setLocation(0, 0);
}
@Override
public void printComponent(Graphics g) {
super.paintComponents(g);
g.drawString("Hallo User! ", x, y);
}
}
Java:
package gui;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyInputHandler implements KeyListener {
public boolean keyBindForward = false;
public boolean keyBindBack = false;
public boolean keyBindLeft = false;
public boolean keyBindRight = false;
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_W: keyBindForward = true;
case KeyEvent.VK_S: keyBindBack = true;
case KeyEvent.VK_A: keyBindLeft = true;
case KeyEvent.VK_D: keyBindRight = true;
default: break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_W: keyBindForward = false;
case KeyEvent.VK_S: keyBindBack = false;
case KeyEvent.VK_A: keyBindLeft = false;
case KeyEvent.VK_D: keyBindRight = false;
default: break;
}
}
}