移动图像的关键监听器我有

时间:2014-06-25 01:17:09

标签: java swing keylistener

我在JFrame上添加了一个图像星。我想知道如何使用键侦听器移动它。 我很困惑,希望有人可以帮助我。

一段(来自希腊语段落,"写在旁边"或"写在&#34旁边;)(Khalil Milanes)是一个独立的单元,以书面形式处理特别的观点或想法。一个段落有5种类型(Br.Anton Heitman)。一段由一个或多个句子组成。[1] [2]虽然不需要任何语言的语法,但段落通常是正式写作的预期部分,用于组织更长的散文。 这是代码:

public class PicLoad extends JFrame {

    PicLoad(){
    add(new Board());


    pack();

    setLocationRelativeTo(null);        
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true);
}


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

以下是我需要帮助的部分:

class Board extends JPanel {


    int dx;
    int dy;
    int x = 100;
    int y = 100;
    Image star;
    Board(){
        // I figured here is the keylister, i need it to move the dx and dy around.
        addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent e){
            int key = e.getKeyCode();
            if(key == KeyEvent.VK_LEFT) dx -= 1;
            if(key == KeyEvent.VK_RIGHT) dx += 1;
            if(key == KeyEvent.VK_UP) dy += 1;
            if(key == KeyEvent.VK_DOWN) dy -= 1;
         }
        });
        initBoard();
        loadImage();

}
void loadImage(){
    ImageIcon ii = new ImageIcon("star.png");
    star = ii.getImage();        
}
void initBoard(){
    setBackground(Color.BLACK);
    setPreferredSize(new Dimension(500,500));

}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    drawStar(g);
}

private void drawStar(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(star, dx + x, dy+ y, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();    

}


// at the top is the drawStar method used to put it in the frame in which I want the keys to //move it around
}

1 个答案:

答案 0 :(得分:3)

if(key == KeyEvent.VK_DOWN) dy -= 1;
..

要使更改可见,应该是:

if(key == KeyEvent.VK_DOWN) dy -= 1;
repaint(); // requests the component paint itself
..

更新

代码中还存在其他问题。这是一个工作(除了up& down交换(BNI))版本作为MCVE。

enter image description here

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;

public class PicLoad extends JFrame {

PicLoad(){
    add(new Board());
    pack();

    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

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

class Board extends JPanel {

    int dx;
    int dy;
    int x = 100;
    int y = 10;
    Image star = new BufferedImage(40,40,BufferedImage.TYPE_INT_RGB);
Board() {
        // I figured here is the keylister, i need it to move the dx and dy around.
        addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent e){
            int key = e.getKeyCode();
            if(key == KeyEvent.VK_LEFT) dx -= 1;
            if(key == KeyEvent.VK_RIGHT) dx += 1;
            if(key == KeyEvent.VK_UP) dy += 1;
            if(key == KeyEvent.VK_DOWN) dy -= 1;
            repaint();
         }
        });
        initBoard();
}

void initBoard() {
    setBackground(Color.RED);
    /* See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods
    in Java Swing? http://stackoverflow.com/q/7229226/418556  (Yes.) */
    //setPreferredSize(new Dimension(500,500));
    setFocusable(true); // VERY IMPORTANT!
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    drawStar(g);
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(500,100);
}

private void drawStar(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(star, dx + x, dy+ y, this);
    //Toolkit.getDefaultToolkit().sync();  //  What were you thinking?!?
    //g.dispose(); // Don't dispose of a Graphics object unless you create it!
}
}