如何使用箭头键在屏幕上移动图形

如何使用箭头键在屏幕上移动图形

本文介绍了如何使用箭头键在屏幕上移动图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创造一个简单游戏的开始。我要做的第一件事是将图形导入我的代码并在屏幕上移动它。我能够在屏幕上画一个球然后移动它但是当我从文件导入图形时我无法移动它。我错过了什么或做错了什么?

I'm trying to create the beginning of a simple game. The first thing I am trying to do is import a graphic into my code and move it across the screen. I was able to draw a ball on the screen and move it around but when I import a graphic from a file I am unable to move it around. What am I missing or doing wrong?

import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game extends JPanel implements ActionListener, KeyListener {

    Timer t = new Timer(5, this);
    double x = 0, y = 0, velX = 0, velY = 0;
    private ImageIcon image;


    public Game(){
        setBackground(Color.WHITE);
        t.start();
        addKeyListener(this);
        this.setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        image = new ImageIcon ("ship.gif");

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        ImageIcon i = new ImageIcon("C:\\Users\\Bryan\\Pictures\\ship.gif");
        i.paintIcon(this, g, 0, 0);

    }

    public void actionPerformed(ActionEvent e){
        repaint();
        x += velX;
        y += velY;

        if(x<0){
            velX = 0;
            x = 0;
        }

        if(x>750){
            velX = 0;
            x = 750;
        }

        if(y<0);{
            velY = 0;
            y = 0;
        }

        if(y>550){
            velY = 0;
            y = 550;
        }
    }

    public void up(){
        velY = -1.5;
        velX = 0;
    }

    public void down(){
        velY = 1.5;
        velX = 0;
    }

    public void left(){
        velX = -1.5;
        velY = 0;
    }

    public void right(){
        velX = 1.5;
        velY = 0;
    }

    public void keyPressed(KeyEvent e){
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_UP){
            up();
        }

        if (code == KeyEvent.VK_DOWN){
            down();
        }

        if (code == KeyEvent.VK_LEFT){
            left();
        }

        if (code == KeyEvent.VK_RIGHT){
            right();
        }
    }

    public void keyTyped(KeyEvent e){}

    public void keyReleased(KeyEvent e){

//      velX = 0;
//      velY = 0;
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_UP){
            velY = 0;
        }
        if (code == KeyEvent.VK_DOWN){
            velY = 0;
        }
        if (code == KeyEvent.VK_LEFT){
            velX = 0;
        }
        if (code == KeyEvent.VK_RIGHT){
            velX = 0;
        }
    }

}

我的驱动程序在另一个类中如下:

My driver is in another class as follows:

import java.awt.Color;

import javax.swing.JFrame;

public class GameDriver {

    public static void main(String[] args) {

        JFrame f = new JFrame();
        Game  g = new Game();
        f.add(g);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(800,600);

    }
}


推荐答案

这里有两大问题:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    ImageIcon i = new ImageIcon("C:\\Users\\Bryan\\Pictures\\ship.gif");
    i.paintIcon(this, g, 0, 0);
}




  1. 您正在从内部读取文件的paintComponent(...)从不执行此操作,因为这会不必要地减慢您的绘图速度。可能在构造函数中读取一次图像,然后在绘图中使用存储的图像变量。 paintComponent方法应该用于绘制 ,它应该是精简,平均且 快速

  2. 你的绘图是0,0 总是。如果你想移动一些东西,在一个可变位置绘制,然后改变变量所持有的值并重新绘制。

  1. You're reading from a file from within paintComponent(...). Never do this as this will slow your drawing unnecessarily. Read the image once, perhaps in a constructor, and then use the stored image variable in drawing. The paintComponent method should be for painting only, and it should be lean, mean and fast.
  2. You're drawing at 0, 0 always. If you want to move something, draw at a variable position, and then change the values held by the variable and repaint.

另外:您应该使用键绑定来接受Swing应用程序中的击键,因为这有助于解决焦点问题。

Also: You should use Key Bindings to accept key strokes in a Swing application as this will help solve focus issues.

例如,请查看。

这篇关于如何使用箭头键在屏幕上移动图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:20