如何在Java中创建墙障或无法通行的墙?

时间:2016-05-22 04:24:51

标签: java swing keyevent

我试图在屏幕边缘(JFrame)上制作无法通行的墙壁。因此,当我将图像向左移动并触及框架的左侧时,会强制图像不移动。我尝试了各种各样的东西,但我似乎无法为它找到合适的代码,所以我想知道如何根据我的代码来完成它。

import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.net.URL;

public class MovingImages extends JPanel implements KeyListener, ActionListener
{
  Timer t = new Timer(5, this);
  int x = 0, y = 0; //coordinates for the image
  int imageScaleX = 100, imageScaleY = 100; //scale the size of the image 
  int velX = 0, velY = 0;  
//--------------------------------------------------------------------------------------- DISPLAYING IMAGE

  public MovingImages()
  {   
    t.start();
    addKeyListener(this); //enables the KeyListener so keys can be pressed
    setFocusable(true);
  }

  /** This code is only used for importing the image and runs the program even when there is no image
    * @param path is a String that is used to represent the the name or where your file is
    * @return is the tempImage which is the image that the program found
    */  
  public Image getImage(String path)
  {
    Image tempImage = null; 
    try
    {
      URL imageURL = MovingImages.class.getResource(path); //finds where the image is
      tempImage = Toolkit.getDefaultToolkit().getImage(imageURL); //loads image from file
    }
    catch (Exception e)
    {
    }
    return tempImage;
  } 

  /** This code is used to display the image in specified coordinates
    * @param g is a variable that uses the Graphics method
    */
  public void paint(Graphics g)
  {
    Image image = getImage("sprite.png"); //choose the file for your image    
    super.paintComponent(g); //everytime the image moves, it clears the previous image    
    Graphics2D g2 = (Graphics2D) g; //converts graphics into 2D
    g2.drawImage(image, x, y, imageScaleX, imageScaleY, this); //draws image in specific coordinates
  }
//--------------------------------------------------------------------------------------- KEYBOARD FUNCTIONS 

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

  public void up()
  {
    velY = -2; 
  }

  public void down()
  { 
    velY = 2;
  }

  public void left()
  {   
    velX = -2;  
  }

  public void right()
  {   
    velX = 2;
  }

  public void keyPressed(KeyEvent e)
  {
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_UP)
    {
      up();
    }
    if (keyCode == KeyEvent.VK_DOWN)
    {
      down();
    }
    if (keyCode == KeyEvent.VK_LEFT)
    {
      left();
    }
    if (keyCode == KeyEvent.VK_RIGHT)
    {
      right();
    }
  }

  public void keyTyped(KeyEvent e)
  {
  }

  public void keyReleased(KeyEvent e)
  {
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_UP)
    {
      velY = 0;
    }
    if (keyCode == KeyEvent.VK_DOWN)
    {
      velY = 0;
    }
    if (keyCode == KeyEvent.VK_LEFT)
    {
      velX = 0;
    }
    if (keyCode == KeyEvent.VK_RIGHT)
    {
      velX = 0;
    }
  } 
//--------------------------------------------------------------------------------------- MAIN 

  public static void main(String args[])
  {
    MovingImages s = new MovingImages();
    JFrame f = new JFrame();
    f.add(s);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(1280, 720);
  }
}

1 个答案:

答案 0 :(得分:2)

以下答案解释了强制对象留在容器内的一般原则。在程序的事件循环中,您可以直接响应键盘输入或基于保存的速度在计时器循环中更新对象的x和y坐标。在任何一种情况下,基本原则都是相同的:

  

检测对象的边缘是否碰到容器的边界,并且不对对象坐标应用任何更改,以便移动它以使其部分或完全位于容器外部。 / p>

以下伪代码描述了每次更新对象位置时必须发生的事情。我只显示水平移动的代码,垂直移动留作练习。我假设对象"位置"是其边界框左下角的坐标。

    int left_edge  = pos_x;
    int right_edge = pox_x + width;
    if (velocity_x < 0)
        pos_x += left_edge > 0 ? velocity_x : 0;
    else if (velocity_x > 0)
        pos_x += right_edge < container_width ? velocity_x : 0;

我没有提到的一些问题是作为练习留下的:

  1. 垂直移动
  2. 当物体碰撞墙壁时,速度会发生什么。 (a)对象是否继续&#34;尝试&#34;移动或(b)该方向的速度是否降至零?第一个选项(a)可能适用于例如在容器中间有某种障碍的情况。物体可能撞到它并在垂直移动时停止水平移动,最后垂直清除障碍物,然后继续水平移动。
  3. 如果velocity > 1以上代码导致部分地在容器外部结束(即您从x==1开始velocity==-2)。您需要增强此案例的代码,并牢记您对上述第2项的回答。