Сбросить расположение ImageIcon в JPanel - PullRequest
0 голосов
/ 31 мая 2019

Я пытаюсь создать новый класс для врага, но у меня болит голова, и мне нужна помощь

Это моя главная, моя игра похожа на космических захватчиков, но проще, я хочупротивник, когда я ударил его лазером, чтобы заблудиться и появиться в случайном месте у правой стороны панели

public class GameMain extends JFrame  implements KeyListener , ActionListener{

 GamePanel panel;
 static int width = 1024 , height = 768;
 Timer t;

    public GameMain() {
        setTitle("Space Attack!!!");
        setLayout(null);
        setSize(width, height);
        setVisible(true);

        panel = new GamePanel();
        add(panel);
        panel.setBounds(1, 1 ,width, height);
        this.addKeyListener(this);

         Timer t = new Timer(40 , this);
            t.setInitialDelay(50);
            t.start(); 
    }

public static void main(String[] args) {
        GameMain frame = new GameMain();
    }

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();
    if (key == (KeyEvent.VK_UP) && GamePanel.y > 1){
        GamePanel.y -= 10;
        GamePanel.ship.setLocation(GamePanel.x , GamePanel.y);
        }
        else if (key == (KeyEvent.VK_DOWN) && GamePanel.y < (height-55)){
            GamePanel.y += 10;
            GamePanel.ship.setLocation(GamePanel.x , GamePanel.y);
        }
        else if (key == (KeyEvent.VK_RIGHT) && GamePanel.x < (width-55)){
            GamePanel.x += 10;
            GamePanel.ship.setLocation(GamePanel.x , GamePanel.y);
        }
        else if (key == (KeyEvent.VK_LEFT) && GamePanel.x > 1){
            GamePanel.x -= 10;
            GamePanel.ship.setLocation(GamePanel.x , GamePanel.y);
        }
        if (key == (KeyEvent.VK_SPACE) ) {
            if (GamePanel.laser_x <= GamePanel.x || GamePanel.laser_x >= width){
            GamePanel.laser.setLocation(GamePanel.x+10 , GamePanel.y+20);
            GamePanel.laser_x = GamePanel.x +10;
            }
        }

    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
            GamePanel.en_x -= 15;
            GamePanel.enemy.setLocation(GamePanel.en_x ,GamePanel.en_y);

            GamePanel.laser_x += GamePanel.x+10;
            GamePanel.laser.setLocation(GamePanel.laser_x, GamePanel.y+20);

2-го класса, который я использую, чтобы добавить свой JLabel

public class GamePanel extends JPanel {
    static JLabel space , ship ,enemy , laser;
    static int y = 384 , x = 80 , en_x = 950, en_y = 350, en_W = 9 , en_H = 1, laser_x ,laser_y;

GamePanel(){
        setLayout(null);

        setSize(GameMain.width , GameMain.height);
        setBackground(Color.GRAY);
        setVisible(true);
        this.requestFocusInWindow();
        setFocusable(true);
        this.setFocusTraversalKeysEnabled(false);

Laser
        ImageIcon icon4 = new ImageIcon("media/rsz_643x0w.jpg");
        laser = new JLabel(icon4);
        add(laser);
        laser.setBounds(laser_x, laser_y, en_W, en_H);


enemy
        ImageIcon icon3 = new ImageIcon("media/rsz_250px-enemy_esports_logo.png");
        enemy = new JLabel(icon3);
        add(enemy);
        enemy.setBounds(en_x, en_y, 63, 63);

Spaceship icon
        ImageIcon icon2 = new ImageIcon("media/asd.png");
        ship = new JLabel(icon2);
        add(ship);
        ship.setBounds(x, y, 50, 44);

        //Space icon
        ImageIcon icon1 = new ImageIcon("media/Wiki-background.jpg");
        space = new JLabel(icon1);
        add(space);
        space.setBounds(1, 1, GameMain.width, GameMain.height);

       }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...