如何避免形状超过Java帧的边界 [英] How to avoid shape exceeding the border of frame in java

查看:177
本文介绍了如何避免形状超过Java帧的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个计划,当pressing箭头键它超过了框架。不过,我希望形状超过了边界时反弹回来。任何人都可以帮助我在此。

  {
    JFrame的帧=新的JFrame(吃豆子);
    PacManObject面板=新PacManObject();
    panel.setFocusable(真);
    panel.setRequestFocusEnabled(真);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600600);
    frame.setVisible(真);    frame.add(面板);
    panel.addKeyListener(面板);
}公共静态无效的主要(字串[] args){
    可运行可运行=新的Runnable(){        @覆盖
        公共无效的run(){
            新的吃豆();
        }    }; EventQueue.invokeLater(可运行);
}类PacManObject继承JPanel实现的KeyListener {    私人INT xLocation = 100;
    私人INT yLocation = 100;
    私人INT口= 280;
    私人INT角= 45;
    私人INT xrandomLocation = 300;
    私人INT yrandomLocation = 400;
    私人INT xLocationEyes = 115;
    私人INT yLocationEyes = 115;

我在哪里我的画造型

  @覆盖
    公共无效的paintComponent(图形图像)
    {
        super.paintComponent方法(图形);        //吃豆子
        graphics.setColor(Color.yellow);
        graphics.fillArc(xLocation,yLocation,100,100,角嘴);        //眼
        graphics.setColor(Color.BLACK);
        graphics.fillOval(xLocationEyes,yLocationEyes,20,20);
        食品(图形);
    }

在pressing箭头键

  @覆盖
    公共无效键pressed(KeyEvent的键盘){        INT键盘preSS = keyboard.getKey code();
         如果(键盘preSS == KeyEvent.VK_RIGHT){
             xLocation + = 30;
             xLocationEyes + = 30;
             角= 45;
             重绘();
         }
         否则,如果(键盘preSS == KeyEvent.VK_LEFT){             角= -145;
             xLocation - = 30;
             xLocationEyes - = 30;
             重绘();
         }
     }


解决方案

在一个范围检查将它使用类似

 如果(xLocation + 100〕=的getWidth()){
    xLocation =的getWidth() - 100;
}否则如果(xLocation℃,){
    xLocation = 0;
}

您应该做同样的事情 yLocation

一般来说,你应该有一个主/游戏循环负责更新游戏的当前状态(根据输入和其他要求的当前状态(如鬼魅的AI))和时间表更新到UI。

这样的话,你会设置一个标志在你的键盘处理,其中主/游戏循环会检查,更新播放器和时间表重绘的状态。这克服了第一密钥preSS事件和重复事件之间的延迟的固有问题。

我也考虑使用的键绑定API超过的KeyListener

也许,看看如何使用按键绑定 如何使用Swing定时器和<一个href=\"http://stackoverflow.com/questions/24856918/how-to-eliminate-delay-in-key$p$pss/24857086#24857086\">How消除关键preSS延迟?的一些更多的想法。

I have this program that when pressing arrow keys it exceeds the frame. However I want the shape to bounce back when exceeding that border. Can anyone help me on this.

  {
    JFrame frame = new JFrame("PacMan");
    PacManObject panel = new PacManObject();
    panel.setFocusable(true);
    panel.setRequestFocusEnabled(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,600);
    frame.setVisible(true);

    frame.add(panel); 
    panel.addKeyListener(panel);


}

public static void main(String[] args) {
    Runnable runnable = new Runnable(){

        @Override
        public void run() {
            new PacMan();
        }

    };EventQueue.invokeLater(runnable);
}

class PacManObject extends JPanel implements KeyListener{

    private int xLocation = 100;
    private int yLocation = 100;
    private int mouth = 280;
    private int angle = 45;
    private int xrandomLocation = 300;
    private int yrandomLocation = 400;
    private int xLocationEyes = 115;
    private int yLocationEyes = 115;

Where I draw my shapes

    @Override
    public void paintComponent(Graphics graphics)
    {
        super.paintComponent(graphics);

        //pacman
        graphics.setColor(Color.yellow);
        graphics.fillArc(xLocation, yLocation, 100, 100, angle, mouth);

        //eyes
        graphics.setColor(Color.BLACK);
        graphics.fillOval(xLocationEyes, yLocationEyes, 20, 20);
        food(graphics);
    }

When pressing the arrow keys

    @Override
    public void keyPressed(KeyEvent keyboard) {

        int keyboardPress = keyboard.getKeyCode();
         if(keyboardPress == KeyEvent.VK_RIGHT){
             xLocation += 30;
             xLocationEyes += 30;
             angle = 45;
             repaint();
         }
         else if(keyboardPress == KeyEvent.VK_LEFT){

             angle = -145;
             xLocation -= 30;
             xLocationEyes -= 30;
             repaint();
         }
     }

解决方案

Put in a range check which uses something like

if (xLocation + 100 >= getWidth()) { 
    xLocation = getWidth() - 100;
} else if (xLocation < 0) {
    xLocation = 0;
}

You should do the same thing for yLocation

Generally speaking, you should have a main/game loop which is responsible for updating the current state of the game (based on the current state of inputs and other requirements (like the AI of the ghosts)) and schedules updates to the UI.

This way, you would set a flag in your key handler, which the main/game loop would check, update the state of the player and the schedule the repaint. This overcomes the inherent issue of the delay between the first key press event and the repeated events.

I'd also consider using the key bindings API over KeyListener.

Perhaps, have a look at How to Use Key Bindings, How to use Swing Timers and How to eliminate delay in keyPress? for some more ideas

这篇关于如何避免形状超过Java帧的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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