为移动正方形设置边界

vfhzx4xs  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(686)

所以基本上我只是为了不同的目的玩弄运动系统,很难让正方形停在边缘。正方形从屏幕一侧移动了大约50%的正方形,我不明白为什么会这样。

  1. package SnakeMovement;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. @SuppressWarnings("serial")
  6. public class SnakeMovement extends Canvas implements ActionListener, KeyListener {
  7. Timer timer = new Timer(5, this);
  8. int width, height;
  9. int xSize = 50, ySize = 50;
  10. int yPos = 0, xPos = 0, yVel = 0, xVel = 0;
  11. public SnakeMovement(int w, int h) {
  12. timer.start();
  13. width = w;
  14. height = h;
  15. addKeyListener(this);
  16. setFocusTraversalKeysEnabled(false);
  17. setFocusable(true);
  18. }
  19. public void paint(Graphics g) {
  20. super.paint(g);
  21. g.fillRect(xPos, yPos, xSize, ySize);
  22. }
  23. public void actionPerformed(ActionEvent e) {
  24. xPos += xVel;
  25. yPos += yVel;
  26. if (xPos >= width - xSize) {
  27. xPos = width - xSize;
  28. xVel = 0;
  29. }
  30. repaint();
  31. }
  32. public void keyPressed(KeyEvent e) {
  33. int key = e.getKeyCode();
  34. if (key == KeyEvent.VK_W) {
  35. yVel = -10;
  36. xVel = 0;
  37. }
  38. if (key == KeyEvent.VK_S) {
  39. yVel = 10;
  40. xVel = 0;
  41. }
  42. if (key == KeyEvent.VK_A) {
  43. xVel = -10;
  44. yVel = 0;
  45. }
  46. if (key == KeyEvent.VK_D) {
  47. xVel = 10;
  48. yVel = 0;
  49. }
  50. }
  51. public void keyReleased(KeyEvent e) {
  52. }
  53. public void keyTyped(KeyEvent e) {
  54. }
  55. }

我只想在屏幕的每一边都有一个正方形的站在边缘。

v2g6jxz6

v2g6jxz61#

尝试:

  1. private static final int BORDER_SIZE = 1;
  2. private static final Color RECT_COLOR = Color.BLUE, BORDER_COLOR = Color.RED;
  3. @Override
  4. public void paint(Graphics g) {
  5. super.paint(g);
  6. g.setColor(BORDER_COLOR);
  7. g.fillRect(xPos, yPos, xSize, ySize);
  8. g.setColor(RECT_COLOR);
  9. g.fillRect(xPos+BORDER_SIZE, yPos+BORDER_SIZE, xSize-2*BORDER_SIZE, ySize-2*BORDER_SIZE);
  10. }

其思想是使用边框颜色绘制一个全尺寸的矩形。
然后用长方形的颜色画一个小的(2*border\u大小)。
以下是上述的mre:

  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import javax.swing.JFrame;
  10. import javax.swing.Timer;
  11. public class SnakeMovement extends Canvas implements ActionListener, KeyListener {
  12. private static final int BORDER_SIZE = 1, DELAY = 100;
  13. private static final Color RECT_COLOR = Color.BLUE, BORDER_COLOR = Color.RED;
  14. private final int xSize = 50, ySize = 50;
  15. private int yPos = 0, xPos = 0, yVel = 5, xVel = 5;
  16. private final Timer timer = new Timer(DELAY, this);
  17. public SnakeMovement(int width, int height) {
  18. timer.start();
  19. addKeyListener(this);
  20. setFocusTraversalKeysEnabled(false);
  21. setFocusable(true);
  22. setPreferredSize(new Dimension(width, height));
  23. }
  24. @Override
  25. public void paint(Graphics g) {
  26. super.paint(g);
  27. g.setColor(BORDER_COLOR);
  28. g.fillRect(xPos, yPos, xSize, ySize);
  29. g.setColor(RECT_COLOR);
  30. g.fillRect(xPos+BORDER_SIZE, yPos+BORDER_SIZE, xSize-2*BORDER_SIZE, ySize-2*BORDER_SIZE);
  31. }
  32. @Override
  33. public void actionPerformed(ActionEvent e) {
  34. xPos += xVel;
  35. yPos += yVel;
  36. checkLimits();
  37. repaint();
  38. }
  39. //when canvas edge is reached emerge from the opposite edge
  40. void checkLimits(){
  41. //check x and y limits
  42. if (xPos >= getWidth()) {
  43. xPos = -xSize;
  44. }
  45. if (xPos < -xSize ) {
  46. xPos = getWidth();
  47. }
  48. if (yPos >= getHeight() ) {
  49. yPos = -ySize;
  50. }
  51. if (yPos < -ySize ) {
  52. yPos = getHeight();
  53. }
  54. }
  55. //two other behaviors when hitting a limit. uncomment the desired behavior
  56. /*
  57. //when canvas edge is reached change direction
  58. void checkLimits(){
  59. //check x and y limits
  60. if ( xPos >= getWidth() - xSize || xPos <= 0) {
  61. xVel = - xVel;
  62. }
  63. if ( yPos >= getHeight() - ySize || yPos <= 0) {
  64. yVel = - yVel;
  65. }
  66. }
  67. */
  68. /*
  69. //when canvas edge is reached stop movement
  70. void checkLimits(){
  71. //check x and y limits
  72. if ( xPos >= getWidth() - xSize || xPos <= 0 || yPos >= getHeight() - ySize || yPos <= 0) {
  73. timer.stop();
  74. }
  75. }
  76. */
  77. @Override
  78. public void keyPressed(KeyEvent e) {
  79. int key = e.getKeyCode();
  80. if (key == KeyEvent.VK_W) {
  81. yVel = -10;
  82. xVel = 0;
  83. }
  84. if (key == KeyEvent.VK_S) {
  85. yVel = 10;
  86. xVel = 0;
  87. }
  88. if (key == KeyEvent.VK_A) {
  89. xVel = -10;
  90. yVel = 0;
  91. }
  92. if (key == KeyEvent.VK_D) {
  93. xVel = 10;
  94. yVel = 0;
  95. }
  96. }
  97. @Override
  98. public void keyReleased(KeyEvent e) {
  99. }
  100. @Override
  101. public void keyTyped(KeyEvent e) {
  102. }
  103. public static void main(String[] args0) {
  104. JFrame frame = new JFrame();
  105. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  106. frame.add(new SnakeMovement(500, 500));
  107. frame.pack();
  108. frame.setVisible(true);
  109. }
  110. }

展开查看全部

相关问题