为移动方块添加边框
Posted
技术标签:
【中文标题】为移动方块添加边框【英文标题】:Putting a border for moving square 【发布时间】:2020-05-29 01:34:03 【问题描述】:所以基本上我只是为了不同的目的而玩弄运动系统,并且在边缘制作方形停止时遇到了麻烦。 Square 从屏幕的一侧移动了大约 50% 的正方形,我不知道为什么会这样。
package SnakeMovement;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class SnakeMovement extends Canvas implements ActionListener, KeyListener
Timer timer = new Timer(5, this);
int width, height;
int xSize = 50, ySize = 50;
int yPos = 0, xPos = 0, yVel = 0, xVel = 0;
public SnakeMovement(int w, int h)
timer.start();
width = w;
height = h;
addKeyListener(this);
setFocusTraversalKeysEnabled(false);
setFocusable(true);
public void paint(Graphics g)
super.paint(g);
g.fillRect(xPos, yPos, xSize, ySize);
public void actionPerformed(ActionEvent e)
xPos += xVel;
yPos += yVel;
if (xPos >= width - xSize)
xPos = width - xSize;
xVel = 0;
repaint();
public void keyPressed(KeyEvent e)
int key = e.getKeyCode();
if (key == KeyEvent.VK_W)
yVel = -10;
xVel = 0;
if (key == KeyEvent.VK_S)
yVel = 10;
xVel = 0;
if (key == KeyEvent.VK_A)
xVel = -10;
yVel = 0;
if (key == KeyEvent.VK_D)
xVel = 10;
yVel = 0;
public void keyReleased(KeyEvent e)
public void keyTyped(KeyEvent e)
我只希望在屏幕的每一侧都完美地在边缘停顿。
【问题讨论】:
如需更好的帮助,请edit添加minimal reproducible example或Short, Self Contained, Correct Example。 您不应将Canvas
与Swing
UI 混用。对于自定义组件,扩展 JComponent
。然后,覆盖 paintComponent
而不是 paint
。
@Holger 所以不是我使用画布,我应该在我的主类中制作,它是 1,它有 jframe 有 jpanel,然后在让我们说运动类中使用 JComponent 我得到你的建议正确吗?
我不知道你的主要课程,也没有说什么。这是你的类SnakeMovement
扩展Canvas
,我建议改为扩展JComponent
。请务必阅读this part of the tutorial...
我的主类只有窗口初始值设定项。所有代码都在 SnakeMovement 类中。
【参考方案1】:
试试:
private static final int BORDER_SIZE = 1;
private static final Color RECT_COLOR = Color.BLUE, BORDER_COLOR = Color.RED;
@Override
public void paint(Graphics g)
super.paint(g);
g.setColor(BORDER_COLOR);
g.fillRect(xPos, yPos, xSize, ySize);
g.setColor(RECT_COLOR);
g.fillRect(xPos+BORDER_SIZE, yPos+BORDER_SIZE, xSize-2*BORDER_SIZE, ySize-2*BORDER_SIZE);
这个想法是使用边框颜色绘制一个完整大小的矩形。 然后使用矩形颜色绘制一个较小的(小 2*BORDER_SIZE)。
以下是上述的mre:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class SnakeMovement extends Canvas implements ActionListener, KeyListener
private static final int BORDER_SIZE = 1, DELAY = 100;
private static final Color RECT_COLOR = Color.BLUE, BORDER_COLOR = Color.RED;
private final int xSize = 50, ySize = 50;
private int yPos = 0, xPos = 0, yVel = 5, xVel = 5;
private final Timer timer = new Timer(DELAY, this);
public SnakeMovement(int width, int height)
timer.start();
addKeyListener(this);
setFocusTraversalKeysEnabled(false);
setFocusable(true);
setPreferredSize(new Dimension(width, height));
@Override
public void paint(Graphics g)
super.paint(g);
g.setColor(BORDER_COLOR);
g.fillRect(xPos, yPos, xSize, ySize);
g.setColor(RECT_COLOR);
g.fillRect(xPos+BORDER_SIZE, yPos+BORDER_SIZE, xSize-2*BORDER_SIZE, ySize-2*BORDER_SIZE);
@Override
public void actionPerformed(ActionEvent e)
xPos += xVel;
yPos += yVel;
checkLimits();
repaint();
//when canvas edge is reached emerge from the opposite edge
void checkLimits()
//check x and y limits
if (xPos >= getWidth())
xPos = -xSize;
if (xPos < -xSize )
xPos = getWidth();
if (yPos >= getHeight() )
yPos = -ySize;
if (yPos < -ySize )
yPos = getHeight();
//two other behaviors when hitting a limit. uncomment the desired behavior
/*
//when canvas edge is reached change direction
void checkLimits()
//check x and y limits
if ( xPos >= getWidth() - xSize || xPos <= 0)
xVel = - xVel;
if ( yPos >= getHeight() - ySize || yPos <= 0)
yVel = - yVel;
*/
/*
//when canvas edge is reached stop movement
void checkLimits()
//check x and y limits
if ( xPos >= getWidth() - xSize || xPos <= 0 || yPos >= getHeight() - ySize || yPos <= 0)
timer.stop();
*/
@Override
public void keyPressed(KeyEvent e)
int key = e.getKeyCode();
if (key == KeyEvent.VK_W)
yVel = -10;
xVel = 0;
if (key == KeyEvent.VK_S)
yVel = 10;
xVel = 0;
if (key == KeyEvent.VK_A)
xVel = -10;
yVel = 0;
if (key == KeyEvent.VK_D)
xVel = 10;
yVel = 0;
@Override
public void keyReleased(KeyEvent e)
@Override
public void keyTyped(KeyEvent e)
public static void main(String[] args0)
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakeMovement(500, 500));
frame.pack();
frame.setVisible(true);
【讨论】:
你能不能再解释一下这是如何工作的,因为我真的不明白如何使用边框来不让正方形离开边缘,因为它仍然会发生。 查看我添加的附加信息。 “不要让正方形离开边缘,因为它仍然会发生”对不起,我不明白这个问题 添加了minimal reproducible example,以便您可以运行和测试它。 好的,所以基本上这是我使用此代码得到drive.google.com/open?id=1hLnA0f7kBFsax_SHbl59AR2U2HlFi7G8 的问题。 ``` if (xPos >= width + xSize) xPos = width - xSize; xVel = 0; ``` 正方形并没有完全脱离边缘,只是没有正确对齐,我很困惑。如果这有助于您了解我的问题是怎么回事。 请使用minimal reproducible example 版本的代码发布问题。 Als 添加所需的行为。以上是关于为移动方块添加边框的主要内容,如果未能解决你的问题,请参考以下文章