Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java游戏:鼠标控制和边框(矩形)问题_Java - Fatal编程技术网

Java游戏:鼠标控制和边框(矩形)问题

Java游戏:鼠标控制和边框(矩形)问题,java,Java,未显示20 x 20框,包括运动侦听器和无错误。另一个类是设置JFrame和游戏开始的窗口。下面是一个名为javagame9的包的代码 问题是你有两个画布:游戏和鼠标 假设窗口类为以下类型,则可以找到它: 我们看到添加到JFrame的画布是游戏画布,因此它将是可见的画布 但是你只是在鼠标画布上画画,所以你不会看到任何东西 您可以将绘制逻辑从一个鼠标移动到另一个游戏,并且仅将鼠标用于MouseMotionListener功能 注释中突出显示的修改后的游戏类差异: public class Game

未显示20 x 20框,包括运动侦听器和无错误。另一个类是设置JFrame和游戏开始的窗口。下面是一个名为javagame9的包的代码


问题是你有两个画布:游戏和鼠标

假设窗口类为以下类型,则可以找到它:

我们看到添加到JFrame的画布是游戏画布,因此它将是可见的画布

但是你只是在鼠标画布上画画,所以你不会看到任何东西

您可以将绘制逻辑从一个鼠标移动到另一个游戏,并且仅将鼠标用于MouseMotionListener功能

注释中突出显示的修改后的游戏类差异:

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID = -2713820159854096116L;
    public static final int WIDTH = 640, HEIGHT = 700;
    private Thread thread;
    private boolean running = false;
    public static boolean paused = false;

    // fields previously in Mouse moved here:
    private Image dbImage;
    private Graphics dbg;

    // mouse field so we can reuse it
    private Mouse mouse;

    public Game() {
        // we create an instance of mouse and use it as MouseMotionListener
        mouse = new Mouse();
        this.addMouseMotionListener(mouse);
        new Window(WIDTH, HEIGHT, "A Game", this);
    }

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        this.requestFocus();
        long LastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while (running) {
            long now = System.nanoTime();
            delta += (now - LastTime) / ns;
            LastTime = now;
            while (delta >= 1) {
                tick();
                delta--;
            }
            if (running)
                render();
            frames++;
            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
        }
        stop();
    }

    private void tick() {

    }

    private void render() {

    }

    public static void main(String args[]) {
        new Game();
    }

    // paint methods previously in Mouse moved here:
    @Override
    public void paint(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent(Graphics g) {
        if (mouse.mouseDragged) {
            g.setColor(Color.DARK_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(mouse.mx, mouse.my, 20, 20);
        }
        else {
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.DARK_GRAY);
            g.fillRect(mouse.mx, mouse.my, 20, 20);
        }
        repaint();
    }
}
现在,您可以从鼠标类中删除用于绘制的所有方法/字段,而且它不再扩展画布:


使用这些类,您现在应该能够看到正在绘制的内容。

窗口最初是不可见的。您试过将窗口设置为可见吗?”窗口=新窗口;window.setVisibletrue;'
public class Mouse extends Canvas implements MouseMotionListener {


    private static final long serialVersionUID = 7986961236445581989L;

    private Image dbImage;    //Mouse - class
    private Graphics dbg;

    int mx, my;
    boolean mouseDragged;


    public void paint(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent(Graphics g) {

        if (mouseDragged) {
            g.setColor(Color.DARK_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(mx, my, 20, 20);
        } else {
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.DARK_GRAY);
            g.fillRect(mx, my, 20, 20);
        }
        repaint();
    }


    @Override
    public void mouseDragged(MouseEvent e) {
        mx = e.getX() - 10;
        my = e.getY() - 10;

        mouseDragged = true;

        e.consume();
    }

    public void mouseMoved(MouseEvent e) {
        mx = e.getX() - 10;
        my = e.getY() - 10;

        mouseDragged = false;
        e.consume();
    }

}
public class Window extends Canvas {

    public Window(int width, int height, String title, Game game) {
        JFrame frame = new JFrame(title);

        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.add(game);
        frame.setVisible(true);

        game.start();
    }

}
public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID = -2713820159854096116L;
    public static final int WIDTH = 640, HEIGHT = 700;
    private Thread thread;
    private boolean running = false;
    public static boolean paused = false;

    // fields previously in Mouse moved here:
    private Image dbImage;
    private Graphics dbg;

    // mouse field so we can reuse it
    private Mouse mouse;

    public Game() {
        // we create an instance of mouse and use it as MouseMotionListener
        mouse = new Mouse();
        this.addMouseMotionListener(mouse);
        new Window(WIDTH, HEIGHT, "A Game", this);
    }

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        this.requestFocus();
        long LastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while (running) {
            long now = System.nanoTime();
            delta += (now - LastTime) / ns;
            LastTime = now;
            while (delta >= 1) {
                tick();
                delta--;
            }
            if (running)
                render();
            frames++;
            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
        }
        stop();
    }

    private void tick() {

    }

    private void render() {

    }

    public static void main(String args[]) {
        new Game();
    }

    // paint methods previously in Mouse moved here:
    @Override
    public void paint(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent(Graphics g) {
        if (mouse.mouseDragged) {
            g.setColor(Color.DARK_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(mouse.mx, mouse.my, 20, 20);
        }
        else {
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.DARK_GRAY);
            g.fillRect(mouse.mx, mouse.my, 20, 20);
        }
        repaint();
    }
}
public class Mouse implements MouseMotionListener {

    private static final long serialVersionUID = 7986961236445581989L;

    int mx, my;
    boolean mouseDragged;

    @Override
    public void mouseDragged(MouseEvent e) {
        mx = e.getX() - 10;
        my = e.getY() - 10;

        mouseDragged = true;

        e.consume();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mx = e.getX() - 10;
        my = e.getY() - 10;

        mouseDragged = false;
        e.consume();
    }

}