Frage von RuheLp, 15

Java Problem :( Hilfe?

es geht hauptsächlich um die main ich weiß echt nicht woran der fehler liegt sry hier mal der code: (main) package egame;

import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame;

public class Egame {

public static void main(String[] args) {
    
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(800, 600);
            //f.setUndecorated(true);
    f.setVisible(true);
    f.setResizable(false);
            f.setLocationRelativeTo(null);
    
            while(true){
                
                if(f.getJump())f.f_posy-=5; //fehler
                
                
                f.repaintScreen(); //fehler
                
        try {
            Thread.sleep(15);
        } catch (InterruptedException ex) {
            Logger.getLogger(Egame.class.getName()).log(Level.SEVERE, null, ex);
        }
            }
            
}

}

(Frame)

package egame;

import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JLabel;

public class Frame extends JFrame{

private Screen screen;

float f_posx = 300;
float f_posy = 300;
private int f_size = 50;

boolean jump = false;

public Frame(){
    super("Move Test");
      screen = new Screen();
      screen.setBounds(0, 0, 800, 600);
      add(screen);
      addKeyListener(new KeyHandler());
                                    
}

private class Screen extends JLabel{

    protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.RED);
        g.fillRect((int)f_posx, (int)f_posy, f_size, f_size);
                    
    }

}

public boolean getJump(){
    return jump;
}


public void repaintScreen(){
    screen.repaint();
}


private class KeyHandler implements KeyListener{

    @Override
    public void keyTyped(KeyEvent e) {
        //Unnötig
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE)jump = true;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE)jump = false;
    }
    
    
    
}

}

Keine passende Antwort gefunden?

Fragen Sie die Community

Weitere Fragen mit Antworten