Spiel mit Swing-Komponenten rendern

Xerses

Mitglied
Hallöle

ich programmiere zu Übungszwecken ein Spiel im Stil von Ogame/Desert Operations,...zumindest versuche ich es. Jedoch verzweifel ich, weil ich nicht weiß wie an die Sache rangehen soll.
Ich habe mir das Tutorial von Quaxli runtergeladen und angeguckt. Habs jetzt erstmal auf die selbe Weise versucht. Jetzt bin ich an dem Punkt wo ich ein Loginscreen einfügen will, weis aber nicht wie ich das machen soll, damit das richtig gerendert wird.

Hier der bisherige Code:
Java:
public class MainFrame extends JFrame {
    
    GamePanel gp;
    
    public MainFrame() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("HugeWars 0.1");
        setResizable(false);        
        gp = new GamePanel();
        add(gp);
        pack();
        setLocationRelativeTo(null);
    }
    
    public static void main(String[] args) {
        new MainFrame().setVisible(true);
    }
}

Java:
public class GamePanel extends JPanel implements Runnable {
    
    private final int PWIDTH = 1024;
    private final int PHEIGHT = 600;
    private Thread gameThread = null;
    private boolean isRunning = false;
    private Image image = null;
    private Graphics graphics = null;
    private final long FPS = 50;
    private long startTime = 0;
    private long deltaTime = 0;
    private long sleepTime = 0;
    private long lastFps = 0;
    
    public GamePanel() {
        setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
        setFocusable(true);
        requestFocus();
        initGame();
        startGame();
    }
    
    private void initGame() {
        
    }
    
    private void startGame() {
        gameThread = new Thread(this);
        gameThread.start();
    }
    
    private void stopGame() {
        isRunning = false;
    }
        
    @Override
    public void run() {
        isRunning = true;
        
        while(isRunning) {
            startTime = System.currentTimeMillis();
            
            updateGame();
            renderGame();
            drawGame();   
            calculateFPS();
            
            try {
                Thread.sleep(sleepTime);
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    }
    
    private void updateGame() {
        
    }
    
    private void renderGame() {
        if(image == null) {
            image = createImage(PWIDTH, PHEIGHT);
            if(image == null) {
                return;
            } else {
                graphics = image.getGraphics();
                graphics.setColor(Color.BLACK);
                graphics.fillRect(0, 0, PWIDTH, PHEIGHT);
                graphics.setColor(Color.RED);
                graphics.drawString("FPS: " + lastFps, 0, 10);                
            }
        } 
    }
    
    private void drawGame() {
        Graphics g;
        
        if(image != null) {
            g = getGraphics();
            g.drawImage(image, 0, 0, null);
        }
    }
    
    private void calculateFPS() {
        deltaTime = System.currentTimeMillis() - startTime;
        sleepTime = (1000 / FPS) - deltaTime;
        
        if(sleepTime <= 0) {
            sleepTime = 5;
        }
        
        lastFps = 1000 / sleepTime;
    }
}

Java:
public class LoginPanel extends JPanel {
    
    private final int PWIDTH = 100;
    private final int PHEIGHT = 150;
    private JLabel nicknameLabel = new JLabel("Nickname");
    private JLabel passwordLabel = new JLabel("Passwort");
    private JTextField nicktext = new JTextField();
    private JPasswordField passtext = new JPasswordField();
    private JButton loginButton = new JButton("Login");
    private GridBagConstraints gbc;
    
    public LoginPanel() {
        setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
        setLayout(new GridBagLayout());
        nicktext.setPreferredSize(new Dimension(100, 20));
        passtext.setPreferredSize(new Dimension(100, 20));
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        add(nicknameLabel, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(nicktext, gbc);
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.gridx = 0;
        gbc.gridy = 2;
        add(passwordLabel, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        add(passtext, gbc);
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 0, 0, 0);
        gbc.gridx = 0;
        gbc.gridy = 4;
        add(loginButton, gbc);
    }
}

Wenn ich das LoginPanel mit
Code:
add(new LoginPanel())
hinzufüge kann ich das Panel nur mit
Code:
repaint()
updaten und das flackert.
 
Was meinst du genau mit geht nicht?

Deine LogginPanel Klasse erbt doch von JPanel, setz dort im Konstruktor setDoubleBuffered.
 
Warum machst Du den Login nicht einfach als JDialog mit modal==true? Das Panel müßtest Du ja auch wieder weg nehmen, sobald die Anmeldung fertig ist?

Das Konzept des Tutorials ist nicht unbedingt dazu gedacht, daß man noch andere Swing-Komponenten mal eben so "reinfrickelt". 😉 Es wird ja nur das Image regelmäßig neu gemalt.

Mal davon abgesehen, ist es nicht alles nach meinem Tutorial gebastelt.
Java:
        if(image != null) {
            g = getGraphics();
            g.drawImage(image, 0, 0, null);
        }

Ein Aufruf von getGraphics sollte in meinem Tutorial nicht zu finden sein. ;(;(;(
 

Neue Themen


Zurück
Oben