如何一边听在Java中的一个关键preSS使图像的举动。 [英] How to make an image move while listening to a keypress in Java.

查看:130
本文介绍了如何一边听在Java中的一个关键preSS使图像的举动。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Java编程,我认为这很酷,通过游戏开发学习java。我知道如何绘制图像,听一键preSS然后移动图像。但是有可能使图像来回移动到窗口而窗口是听一个关键preSS?例如像,当图像或物体(如太空船)是从左向右移动窗口,那么如果我preSS空格键,激光将火在屏幕的底部(酷吧:D)。但基本上,我只是想知道如何让左到右,而窗口正在听一键preSS形象的举动。

我想我会的关键侦听器添加到我的窗前,然后触发一个无限循环移动图像。或者,我需要了解线程使另一个线程会移动的对象?

请指教。

非常感谢。


解决方案

  

但基本上,我只是想知道如何让左到右,而窗口正在听一键preSS图像移动


您可以使用一个Swing计时器动画图像:

 进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类TimerAnimation扩展JLabel实现的ActionListener
{
    INT DELTAX = 2;
    INT DELTAY = 3;
    INT方向X = 1;
    INT directionY = 1;    公共TimerAnimation(
        INT运行startx,INT startY,
        INT DELTAX,INT DELTAY,
        INT方向X,诠释directionY,
        INT延迟)
    {
        this.deltaX = DELTAX;
        this.deltaY = DELTAY;
        this.directionX =方向X;
        this.directionY = directionY;        的setIcon(新的ImageIcon(dukewavered.gif));
的setIcon //(新的ImageIcon(copy16.gif));
        的setSize(GET preferredSize());
        的setLocation(运行startx,startY);
        新javax.swing.Timer中(延迟,这一点)。开始();
    }    公共无效的actionPerformed(ActionEvent的五)
    {
        容器父=的getParent();        //确定下一个X位置        INT nextX =的getLocation()X +(DELTAX *方向X)。        如果(nextX℃,)
        {
            nextX = 0;
            方向X * = -1;
        }        如果(nextX +的getSize()宽度方式>。parent.getSize()宽度)
        {
            nextX = parent.getSize()宽度 - 的getSize()宽度。
            方向X * = -1;
        }        //确定下一个Y位置        INT nextY =的getLocation()Y +(DELTAY * directionY)。        如果(nextY℃,)
        {
            nextY = 0;
            directionY * = -1;
        }        如果(nextY +的getSize()高度方式>。parent.getSize()高度)
        {
            nextY = parent.getSize()高度 - 的getSize()高度。
            directionY * = -1;
        }        //移动标签        的setLocation(nextX,nextY);
    }    公共静态无效的主要(字串[] args)
    {
        的JPanel面板=新JPanel();
        JFrame的帧=新的JFrame();        frame.setContentPane(面板);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane()的setLayout(空)。
// frame.getContentPane()添加(新TimerAnimation(10,10,2,3,1,1,10))。
        。frame.getContentPane()添加(新TimerAnimation(300,100,3,2,-1,1,20));
// frame.getContentPane()添加(新TimerAnimation(0,000,5,0,1,1,20))。
        frame.getContentPane()添加(新TimerAnimation(0,200,5,0,1,1,80))。
        frame.setSize(400,400);
        frame.setLocationRelativeTo(NULL);
        frame.setVisible(真);
// frame.getContentPane()添加(新TimerAnimation(10,10,2,3,1,1,10))。
// frame.getContentPane()添加(新TimerAnimation(10,10,3,0,1,1,10))。
    }
}

您可以将的KeyListener添加到面板上,然后将图像动画的独立运作。

I'm starting to learn java programming and I think it's cool to learn java through game development. I know how to draw image and listen to a keypress then move that image. But is it possible to make the image move back and forth to the window while the window is listening to a keypress? Like for example, while the image or object(like spaceship) is moving left to right in the window, then if I press space key, a laser will fire at the bottom of the screen( cool huh :D ). But basically I just want to know how to make the image move left to right while the window is listening to a keypress.

I'm thinking that I will add a key listener to my window then fire an infinite loop to move the image. Or do I need to learn about threading so that another thread will move the object?

Please advise.

Many thanks.

解决方案

But basically I just want to know how to make the image move left to right while the window is listening to a keypress

You can use a Swing Timer to animate an image:

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

public class TimerAnimation extends JLabel implements ActionListener
{
    int deltaX = 2;
    int deltaY = 3;
    int directionX = 1;
    int directionY = 1;

    public TimerAnimation(
        int startX, int startY,
        int deltaX, int deltaY,
        int directionX, int directionY,
        int delay)
    {
        this.deltaX = deltaX;
        this.deltaY = deltaY;
        this.directionX = directionX;
        this.directionY = directionY;

        setIcon( new ImageIcon("dukewavered.gif") );
//      setIcon( new ImageIcon("copy16.gif") );
        setSize( getPreferredSize() );
        setLocation(startX, startY);
        new javax.swing.Timer(delay, this).start();
    }

    public void actionPerformed(ActionEvent e)
    {
        Container parent = getParent();

        //  Determine next X position

        int nextX = getLocation().x + (deltaX * directionX);

        if (nextX < 0)
        {
            nextX = 0;
            directionX *= -1;
        }

        if ( nextX + getSize().width > parent.getSize().width)
        {
            nextX = parent.getSize().width - getSize().width;
            directionX *= -1;
        }

        //  Determine next Y position

        int nextY = getLocation().y + (deltaY * directionY);

        if (nextY < 0)
        {
            nextY = 0;
            directionY *= -1;
        }

        if ( nextY + getSize().height > parent.getSize().height)
        {
            nextY = parent.getSize().height - getSize().height;
            directionY *= -1;
        }

        //  Move the label

        setLocation(nextX, nextY);
    }

    public static void main(String[] args)
    {
        JPanel panel = new JPanel();
        JFrame frame = new JFrame();

        frame.setContentPane(panel);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().setLayout(null);
//      frame.getContentPane().add( new TimerAnimation(10, 10, 2, 3, 1, 1, 10) );
        frame.getContentPane().add( new TimerAnimation(300, 100, 3, 2, -1, 1, 20) );
//      frame.getContentPane().add( new TimerAnimation(0, 000, 5, 0, 1, 1, 20) );
        frame.getContentPane().add( new TimerAnimation(0, 200, 5, 0, 1, 1, 80) );
        frame.setSize(400, 400);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
//      frame.getContentPane().add( new TimerAnimation(10, 10, 2, 3, 1, 1, 10) );
//      frame.getContentPane().add( new TimerAnimation(10, 10, 3, 0, 1, 1, 10) );
    }
}

You can add a KeyListener to the panel and it will operate independently of the image animation.

这篇关于如何一边听在Java中的一个关键preSS使图像的举动。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆