How do I move a triangle around the corners of the screen using java?

Asked

Viewed 107 times

1

I’m pretty new with java and I’m working on the project that I have to draw a triangle (which I have to call Alice) that is located in the upper left corner of the screen and it has to move in a straight line to the upper right corner, then move to the lower right, then to the lower left and back to the upper left starting point and it has to walk around the corners of the screen until I close and window.

I’m having trouble with my if structures. I don’t know why it leaves the starting point (upper right), goes up to the upper left and then, when it goes down to the lower left, it doesn’t stop at the edge of the screen, and from there I don’t know if it doesn’t continue its trajectory.

If anyone can help me I thank you from my heart.

My code is like this:

import processing.core.PApplet;

public class Alice{
    int colour;
    float xSpeed;
    PApplet parent;
    float x1Pos;
    float y1Pos;
    float x2Pos;
    float y2Pos;
    float x3Pos;
    float y3Pos;

    public Alice(PApplet p, int colour, float x1Pos, float y1Pos, float x2Pos, float y2Pos, float x3Pos, float y3Pos, float xSpeed){
    parent = p;
        this.colour = colour;
        this.x1Pos = x1Pos;
        this.y1Pos = y1Pos;
        this.x2Pos = x2Pos;
        this.y2Pos = y2Pos;
        this.x3Pos = x3Pos;
        this.y3Pos = y3Pos;
        this.xSpeed = xSpeed;
    }

    public void display(){
        parent.rectMode(parent.CENTER);
        parent.fill(colour);
        parent.triangle(x1Pos, y1Pos, x2Pos, y2Pos, x3Pos, y3Pos);
    }

    private void goLeft(){
        this.x1Pos = this.x1Pos - this.xSpeed;
        this.x2Pos = this.x2Pos - this.xSpeed;
        this.x3Pos = this.x3Pos - this.xSpeed;
    }

    private void goRight(){
        this.x1Pos = this.x1Pos + xSpeed;
        this.x2Pos = this.x2Pos + xSpeed;
        this.x3Pos = this.x3Pos + xSpeed;
    }

    private void goUp(){
        this.y1Pos = this.y1Pos - xSpeed;
        this.y2Pos = this.y2Pos - xSpeed;
        this.y3Pos = this.y3Pos - xSpeed;
    }

    private void goDown(){
        this.y1Pos = this.y1Pos + xSpeed;
        this.y2Pos = this.y2Pos + xSpeed;
        this.y3Pos = this.y3Pos + xSpeed;
    }

    public void driveCorner(){
        if(x1Pos <= parent.width - 30){
            goRight();
        }
        if(x1Pos >= parent.width - 30){
            goDown();
        }
        if(y1Pos <= parent.height - 20){
            goLeft();
        }
        if(x1Pos <= 0){ 
         goUp();
        }
    }

1 answer

1


I think tracking down variables on which of the edges the triangle is or isn’t is much easier. In addition, it is better if the three vertices are treated equally in this regard without it being necessary to know beforehand the width or height of the triangle (in its original code it was 20 and 30).

This way, the code looks like this:

import processing.core.PApplet;

public class Alice {
    private int colour;
    private float xSpeed;
    private PApplet parent;
    private float x1Pos;
    private float y1Pos;
    private float x2Pos;
    private float y2Pos;
    private float x3Pos;
    private float y3Pos;

    public Alice(PApplet p, int colour, float x1Pos, float y1Pos, float x2Pos, float y2Pos, float x3Pos, float y3Pos, float xSpeed) {
        parent = p;
        this.colour = colour;
        this.x1Pos = x1Pos;
        this.y1Pos = y1Pos;
        this.x2Pos = x2Pos;
        this.y2Pos = y2Pos;
        this.x3Pos = x3Pos;
        this.y3Pos = y3Pos;
        this.xSpeed = xSpeed;
    }

    public void display() {
        parent.rectMode(parent.CENTER);
        parent.fill(colour);
        parent.triangle(x1Pos, y1Pos, x2Pos, y2Pos, x3Pos, y3Pos);
    }

    private void goLeft() {
        this.x1Pos -= xSpeed;
        this.x2Pos -= xSpeed;
        this.x3Pos -= xSpeed;
    }

    private void goRight() {
        this.x1Pos += xSpeed;
        this.x2Pos += xSpeed;
        this.x3Pos += xSpeed;
    }

    private void goUp() {
        this.y1Pos -= xSpeed;
        this.y2Pos -= xSpeed;
        this.y3Pos -= xSpeed;
    }

    private void goDown() {
        this.y1Pos += xSpeed;
        this.y2Pos += xSpeed;
        this.y3Pos += xSpeed;
    }

    public void driveCorner() {
        boolean naBordaSuperior = y1Pos <= 0 || y2Pos <= 0 || y3Pos <= 0;
        boolean naBordaInferior = y1Pos >= parent.height - 1 || y2Pos >= parent.height - 1 || y3Pos >= parent.height - 1;
        boolean naBordaEsquerda = x1Pos <= 0 || x2Pos <= 0 || x3Pos <= 0;
        boolean naBordaDireita = x1Pos >= parent.width - 1 || x2Pos >= parent.width - 1 || x3Pos >= parent.width - 1;

        if (naBordaSuperior && !naBordaDireita) {
            goRight();
        } else if (naBordaDireita && !naBordaInferior) {
            goDown();
        } else if (naBordaInferior && !naBordaEsquerda) {
            goLeft();
        } else if (naBordaEsquerda && !naBordaSuperior) { 
            goUp();

        // No caso em que o triângulo está no meio e afastado de qualquer borda, vai para a direita.
        } else if (!naBordaSuperior && !naBordaInferior && !naBordaEsquerda && !naBordaDireita) {
            goRight();

        // Este caso só acontece se estiver nas quatro bordas.
        // Ou seja, ou o triângulo é muito grande, ou a tela muito pequena.
        // Portanto, não se move.
        } else {
            // ...
        }
    }
  • THANK YOU VERY MUCH. IS WORKING PERFECTLY

Browser other questions tagged

You are not signed in. Login or sign up in order to post.