为什么图像不移动?

时间:2018-01-29 16:03:41

标签: java swing

也许有人可能会暗示我在代码中做错了什么?我希望播放器.png图像在鼠标移动到板上时改变x位置,但似乎图像根本不显示...我正在关注this 教程,但董事会只打开黑色背景,没有别的......

主要课程:

package com.alexz;

import com.alexz.interfaces.Commons;

import javax.swing.JFrame;
import java.awt.EventQueue;

public class Main extends JFrame implements Commons{
public Main(){
    initUI();
}

private void initUI(){
    add(new Board());

    setTitle("->>FireUP<<-");
    setSize(BWIDTH, BHEIGHT);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    EventQueue.invokeLater(()->{
        Main app = new Main();
        app.setVisible(true);
    });
}
}

董事会成员:

package com.alexz;

import com.alexz.interfaces.Commons;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Board extends JPanel implements Commons, Runnable{

    private STATE state = STATE.IN_GAME;
    private Thread animator;
    private Player player;

    public Board(){
        initBoard();
    }

    private void initBoard(){
        addMouseMotionListener(new MAdapter());
        setFocusable(true);
        setBackground(Color.BLACK);

        gameInit();
        setDoubleBuffered(true);
    }

    private void gameInit(){
        player = new Player();

        if(animator==null || state != STATE.IN_GAME){
            animator = new Thread();
            animator.start();
        }
    }


    @Override
    public void addNotify(){
        super.addNotify();
        gameInit();
    }

    private void drawPlayer(Graphics g){
        if(player.isVisible()){
            g.drawImage(player.getImage(), player.getX(), player.getY(), this);
        }else{
            state = STATE.DEAD;
            player.setVisible(false);
            //game over function call
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (state == STATE.IN_GAME){
            drawPlayer(g);
        }

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    private void animationCycle(){
        player.move();
    }

    @Override
    public void run(){
        long before, diff, sleep;
        before = System.currentTimeMillis();
        while(state == STATE.IN_GAME){

            animationCycle();

            diff = System.currentTimeMillis() - before;
            sleep = DELAY - diff;
             if (sleep < 0)
                 sleep = 2;

             try{
                 Thread.sleep(sleep);
             }catch(InterruptedException e){
                 System.out.println("ERROR: Interrupted Exception");
             }

             before = System.currentTimeMillis();
             repaint();
        }

    }

    private class MAdapter extends MouseAdapter{

        @Override
        public void mouseMoved(MouseEvent e) {
            super.mouseMoved(e);
            player.mouseMoved(e);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

if ($form->isSubmitted() && $form->isValid()) { // $file stores the uploaded PDF file /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ $file = $product->getBrochure(); // do whatever you want to do with the content of the file without saving the file in the server //something like $contents = file_get_contents($file->getPathname()); } 中的条件永远不会成立,因为您将状态设置为启动线程之前不得具有的值。