Java problem z nasłuchiwaniem klawiatury(Focusable)

0

Witam
W klasie "panelWyswietlaniaGry" która jest rozszerzona o Jpanel znajduję się blok instrukcji który odpowiada za nasłuchiwanie klawiatury, ustawiam tam też "setFocusable(true)". Wszystko działa w momencie kiedy tworzę tą klasę na nowo poprzez zdarzenie z przycisku już działać przestaje, pomimo że w konstruktorze jest metoda "setFoc..(true)", wszystkie komponenty ustawiam na setfocusable(false).Bez tytułu.png

Nie rozumiem czy pomimo usuwania komponentu i stworzenia nowego focus jest ustawiony na poprzedni usunięty próbowałem też to i nie działa chyba że coś źle zapisałem.

0

package GraPlatformowa;

import javax.swing.;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.tools.Tool;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

public class Main {
public static void main(String[] args) {
Ramka r = new Ramka();
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
r.setVisible(true);
}
}

class Ramka extends JFrame {
private PanelPrzyciskow panelPrzyciskow;
private PanelGry panel;

public Ramka() {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    }
    catch (Exception e) { }
    setSize(700,500);
    setLocationRelativeTo(null);
    setFocusable(false);

    Container powZawartosci = getContentPane();

    panel = new PanelGry();
    powZawartosci.add(panel, BorderLayout.CENTER);

    panelPrzyciskow = new PanelPrzyciskow();
    powZawartosci.add(panelPrzyciskow, BorderLayout.NORTH);


    panelPrzyciskow.getB1().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(panel != null) {
                remove(panel);
            }

            panel = new PanelGry();
            powZawartosci.add(panel);
            panelPrzyciskow.setArrayRectangles(panel.getRectangles());
            revalidate();

        }
    });
}

}

class Timer extends Thread {
private ArrayList rectangles;
private JPanel panel;
private static int REFRESH = 100;

public Timer(ArrayList rectangles, JPanel panel) {
    this.rectangles = rectangles;
    this.panel = panel;
}
public static void setREFRESH(int refresh) {
    REFRESH = refresh;
}
@Override
public void run() {
    while(true ) {
        try {
            Player r = (Player) rectangles.get(0);
            r.update();

            panel.repaint();

            sleep(1000/REFRESH);
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }
    }

}

}

class PanelGry extends JPanel {
private UchwytKlawisza sluchacz;
private ArrayList rectangles;

private Timer t;

public PanelGry() {
    setSize(500,400);
    Rectangle.widthSreen = getWidth();
    Rectangle.heightSreen = getHeight();

    setBackground(Color.LIGHT_GRAY);
    setFocusable(true);

    sluchacz = new UchwytKlawisza();
    addKeyListener(sluchacz);

    rectangles = new ArrayList();

    rectangles.add(new Player(100,100,5,40*1.6,Color.RED, 40));
    rectangles.add(new Rectangle(10,10,100,20,Color.BLUE));
    rectangles.add(new Rectangle(10,220,100,20,Color.BLUE));
    rectangles.add(new Rectangle(120,60,100,20,Color.BLUE));
    rectangles.add(new Rectangle(250,20,100,20,Color.BLUE));

    Player p = (Player)rectangles.get(0);
    for(int i = 0; i < rectangles.size(); i++) {
        if(i != 0) {
            p.addObjectCollion((Rectangle) rectangles.get(i));
        }

    }
    t = new Timer(rectangles, this);
    t.start();
}
public ArrayList getRectangles() {
    return rectangles;
}
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    for(int i = 0; i < rectangles.size(); i++) {
        Rectangle r = (Rectangle) rectangles.get(i);
        g2.setColor(r.getColor());
        g2.fillRect((int)r.getX(), -(int)r.getY()+(int)Rectangle.heightSreen-(int)r.getHeight(), (int)r.getWidth(), (int)r.getHeight());

        g2.drawString("x:"+String.valueOf(r.getX()) + ",y:"+ String.valueOf((int)r.getY()), (int)r.getX(),-(int)r.getY()+(int)Rectangle.heightSreen-(int)r.getHeight());
    }
}

private static boolean keyPressed[] = new boolean[4];
private class UchwytKlawisza implements KeyListener {
    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyPressed(KeyEvent e) {
        Player p = (Player) rectangles.get(0);
        System.out.println("Kliknieto");
        if(e.getKeyCode()==KeyEvent.VK_UP) {
            keyPressed[0] = true;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN) {
            keyPressed[1] = true;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
            keyPressed[2] = true;
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT) {
            keyPressed[3] = true;
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_UP) {
            keyPressed[0] = false;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN) {
            keyPressed[1] = false;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
            keyPressed[2] = false;
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT) {
            keyPressed[3] = false;
        }
    }
}
public static boolean[] getKeys() {
    return keyPressed;
}

}

class PanelPrzyciskow extends JPanel {
private JButton b1;
private ArrayList rectangles;

private JSlider slider, sliderTime;
private JLabel labelPretkoscPoczatkowa;

public PanelPrzyciskow() {
    setBackground(Color.gray);
    setFocusable(false);

    b1 = new JButton("New Game");
    b1.setFocusable(false);
    add(b1);

    slider = new JSlider(0, 50,5);
    slider.setMinorTickSpacing(2);
    slider.setMajorTickSpacing(1);
    slider.setValue(100);
    slider.setFocusable(false);
    add(slider);

    labelPretkoscPoczatkowa = new JLabel("v0: "+String.valueOf(slider.getValue()) );
    labelPretkoscPoczatkowa.setBackground(Color.LIGHT_GRAY);
    add(labelPretkoscPoczatkowa);

    slider.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
           Player p = (Player) rectangles.get(0);
            labelPretkoscPoczatkowa.setText("v0: "+String.valueOf(slider.getValue()));

        }
    });

    sliderTime = new JSlider(0, 100,5);
    sliderTime.setMinorTickSpacing(2);
    sliderTime.setMajorTickSpacing(1);
    sliderTime.setValue(100);
    sliderTime.setFocusable(false);
    add(sliderTime);

    JLabel labelTime = new JLabel();
    add(labelTime);
    labelTime.setText(String.valueOf(sliderTime.getValue()));

    sliderTime.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            Player p = (Player) rectangles.get(0);
            labelTime.setText(String.valueOf(sliderTime.getValue()));
        }
    });
}
public JButton getB1() {
    return b1;
}
public void setArrayRectangles(ArrayList rectangles) {
    this.rectangles = rectangles;
}
public void setAllFocusEnabled() {
    b1.setFocusable(false);
    slider.setFocusable(false);
    sliderTime.setFocusable(false);
    labelPretkoscPoczatkowa.setFocusable(false);
}

}

class Rectangle {
public static double widthSreen, heightSreen;
private double x, y;
private double width, height;
private Color c;

public Rectangle(double x, double y, double width, double height, Color c) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.c = c;
}

public void setX(double x) {
    this.x =x;
}
public void setY(double y) {
    this.y = y;
}
public double getX() {
    return x;
}
public double getY() {
    return y;
}

public void setWidth(double width) {
    this.width = width;
}
public void setHeight(double height) {
    this.height = height;
}
public double getWidth() {
    return width;
}
public double getHeight() {
    return height;
}

public void setColor(Color c) {
    this.c = c;
}
public Color getColor() {
    return c;
}

}

class Player extends Rectangle {
private double pretkoscPoczatkowa;
private double g = 9.81;

private boolean jump;
private ArrayList objectCollion;

public Player(double x, double y, double width, double height, Color c, double pretkoscPoczatkowa) {
    super(x, y, width, height, c);
    objectCollion = new ArrayList();
    this.pretkoscPoczatkowa = pretkoscPoczatkowa;
}
public void setPretkoscPoczatkowa(double pretkoscPoczatkowa) {
    this.pretkoscPoczatkowa = pretkoscPoczatkowa;
}
public void setTime(double t) {this.t = t;}
public void addObjectCollion(Rectangle r) {
    objectCollion.add(r);
}
public void moveVertical(double moveAbout) {
    if(!collision(getX() + moveAbout, getY(), getWidth(), getHeight())){
        setX(getX()+moveAbout);
    }
    if(!collision(getX(), getY()-1, getWidth(), getHeight())){
        if(!dropFromCollion && !jump) {
            poziomWyskoku = getY();
            t = 0;
            dropFromCollion = true;
            dropFall = true;
            jump = true;
        }
    }
}

private double t;
private double h;
private double poziomWyskoku;
private double krancowaWartoscY;
private boolean dropFall;
private boolean dropFromCollion;

private double halfOfHeight = getHeight()/2;

public void jump(int vector) {
    if(!jump && vector >= 1) {
        poziomWyskoku = getY();
        t = 0;
        jump = true;
    }
}
public void update() {
    refreshKeys();
    if(jump && !dropFall) {
        h = pretkoscPoczatkowa * t - (g * Math.pow(t, 2)) / 2;
        if (!collision(getX(), poziomWyskoku + (int)h, getWidth(), getHeight())) {
            setY((int)h + poziomWyskoku);
            t += 0.15;
        }else {
            jump = false;
            if(pretkoscPoczatkowa / g >=t){
                dropFall = true;
                poziomWyskoku = getY();
                t = 0;
                jump = true;
                dropFromCollion = false;
            }else {
                setY(krancowaWartoscY);
            }
        }
    }
    if(dropFall) {
        h = -(g * Math.pow(t, 2) /2);
        if (!collision(getX(), poziomWyskoku + (int)h, getWidth(), getHeight())) {
            setY((int)h + poziomWyskoku);
            t += 0.15;
        }else {
            dropFall = false;
            jump = false;
            dropFromCollion = false;
            setY(krancowaWartoscY);
        }
    }

}
public boolean collision(double x, double y, double width, double height) {
    for(int i = 0; i < objectCollion.size(); i++) {
        Rectangle r = (Rectangle) objectCollion.get(i);
        if (x + width >= r.getX() && x <= r.getX() + r.getWidth() && y + height >= r.getY() && y <= r.getY() + r.getHeight()) {
            Random p = new Random();
            r.setColor(new Color(p.nextInt(255), p.nextInt(255), p.nextInt(255)));
            krancowaWartoscY = r.getY() + r.getHeight() + 1;
            return true;
        }
    }
    return false;
}
public void refreshKeys() {
    if(PanelGry.getKeys()[0]) jump(1);
    else if(PanelGry.getKeys()[1]) jump(-1);
    if(PanelGry.getKeys()[2]) moveVertical(1);
    else if(PanelGry.getKeys()[3]) moveVertical(-1);
}

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1