Putting a border for moving square

WinterPenguin :

So basically im just playing around with movement systems for different purposes and having trouble making square stop right at the edge. Square moves off the side of screen by around 50% of the square it self and i cannot figure out why it is like that.

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) {
    }


}

I just want so square stops at each side of screen perfectly on the edge.

c0der :

Try:

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);
}

The idea is to paint a full size rectangular using the border color.
Then painting a smaller one (smaller by 2*BORDER_SIZE) using the rectangular color.


The following is mre of the above:

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);
    }
}

enter image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416872&siteId=1