您如何移动方块以将其压扁 [英] How do you move the square to whitch it is bieng pressed

查看:63
本文介绍了您如何移动方块以将其压扁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个蛇游戏项目,并且在移动我创建的Snake Square时需要帮助.这是我创建正方形的步骤:

I'm currently working on a project for a snake game, and I need help on moving the Snake Square that I created. This is what I did to create the square:

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

public class ShapeTest extends JFrame{
     public ShapeTest(){
          setSize(300,400);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLocationRelativeTo(null);
          setVisible(true);
     }

     public static void main(String a[]){
         new ShapeTest();
     }

     public void paint(Graphics g){
          g.drawRect(80, 30, 100, 100); // FOR SQUARE
     }
}

因此,现在在我的主要Snake类上,我想像操作一样移动正方形.如果您能帮助我,我会非常爱你们!非常感谢!

So now on my main Snake Class I want to move the square sort of like an Action. I would love you all so much if you could help me! Thank you so much!

这是我的主要蛇类:

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

public class ShapeTest extends JFrame{
     public ShapeTest(){
          setSize(300,400);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLocationRelativeTo(null);
          setVisible(true);
     }

     public static void main(String a[]){
         new ShapeTest();
     }

     public void paint(Graphics g){
          g.drawRect(80, 30, 100, 100); // FOR SQUARE
     }
}

这是正确的路径吗?

b.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // do some action
    }
});

非常感谢,我现在明白了!爱你们!

Thankyou so much i understand now! Love you guys!

推荐答案

按照承诺,这是我的示例.最重要的事情:

As promised, here is my example. The most important things:

  1. 我取代了 JPanel
  2. paintComponent(),而不是 paint()
  3. Sprite 类具有变量 x y width height 和相应的吸气剂&二传手.
  4. Sprite 类具有方法 draw(Graphics g),该方法允许 panel 绘制 Sprite
  5. Sprite 类具有方法 moveLeft() moveRight() moveUp() moveDown(),每个都相应地更改 x / y 变量.
  6. 我添加了 键绑定 转到 panel ,以便在按下相应的箭头键时调用在 4.点提到的方法.请阅读链接以获取有关键绑定的进一步说明.
  1. I overrode paintComponent(), not paint(), of a JPanel
  2. The Sprite-class has the variables x, y, width, height and the corresponding getters & setters.
  3. The Sprite-class has the method draw(Graphics g) which allows the panel to draw the Sprite
  4. The Sprite-class has the methods moveLeft(), moveRight(), moveUp() and moveDown(), each changing the x/y variable accordingly.
  5. I added Key Bindings to the panel in order to call the methods mentioned at point 4. when the corresponding arrow-keys are pressed. Please read the link for a further explanation of Key Bindings.

代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Example {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }

    public Example() {

        JFrame frame = new JFrame("Example");

        Sprite sprite = new Sprite(50, 50, 10, 10);

        JPanel panel = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                sprite.draw(g);
            }
        };

        // Key Bindings
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");
        panel.getActionMap().put("left", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                sprite.moveLeft();
                panel.repaint();
            }
        });
        panel.getActionMap().put("right", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                sprite.moveRight();
                panel.repaint();
            }
        });
        panel.getActionMap().put("up", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                sprite.moveUp();
                panel.repaint();
            }
        });
        panel.getActionMap().put("down", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                sprite.moveDown();
                panel.repaint();
            }
        });

        frame.setContentPane(panel);

        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public class Sprite {

        private int x, y, width, height;

        protected Sprite(int x, int y, int width, int height) {
            setX(x);
            setY(y);
            setWidth(width);
            setHeight(height);
        }

        protected void draw(Graphics g) {
            g.setColor(Color.RED);
            g.fillRect(getX(), getY(), getWidth(), getHeight());
        }

        protected void moveLeft() {
            setX(getX() - 10);
        }

        protected void moveRight() {
            setX(getX() + 10);
        }

        protected void moveUp() {
            setY(getY() - 10);
        }

        protected void moveDown() {
            setY(getY() + 10);
        }

        protected int getX() {
            return x;
        }

        protected void setX(int x) {
            this.x = x;
        }

        protected int getY() {
            return y;
        }

        protected void setY(int y) {
            this.y = y;
        }

        protected int getWidth() {
            return width;
        }

        protected void setWidth(int width) {
            this.width = width;
        }

        protected int getHeight() {
            return height;
        }

        protected void setHeight(int height) {
            this.height = height;
        }
    }

}

这篇关于您如何移动方块以将其压扁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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