1,105,229 Community Members

method to change shapes not working

Member Avatar
Mi_99
Newbie Poster
3 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

he question is to get my shapes to change over time according to some criteria of my own design therefore i tried to make the shape change size as it hits certain coordinates but it would turn into line and keep moving as lines below is my move method to make my shapes move, rebound, and move horizontally and vertically.

public void move() {
    // Rebound X
    if (x <= 0 || x >= 400 - width) {
        moveX = moveX * -1;
    }

    // Rebound Y
    if (y <= 0 || y >= 400 - height) {
        moveY = moveY * -1;
    }

    // Wide shapes move up and down, narrow shapes move sideways
    if (width > 15) {
        y += moveY;
    } else {
        x += moveX;
    }

    // Change shape size
    if (y <= 100 || y >= 300 - height) {
        moveX = -moveX;
        height += height * 1 / 2;
    }

    //if {
    //    height -= height * 1/2;
    //    moveX = -moveX;
    //}

    //if (x < 20) {
    //    width -= width * 1/2;
    //    moveY = -moveY;
    //} else {
    //    width += width * 1/2;
    //    moveY = -moveY;
    //}

This is my shape class that all shapes extend:

package shapes;

import java.awt.*;
import java.util.Random;

public abstract class Shape {
    protected int x;
    protected int y;
    protected int width;
    protected int height;
    protected Color color;
    protected int moveX = 1;
    protected int moveY = 1;

    public Shape() {
        Random r = new Random();
        width = r.nextInt(30) + 10;
        height = width;
        x = r.nextInt(400 - width);
        y = r.nextInt(400 - height);
        color = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
    }

    // Returns random value within range (low < n <= high).
    public int randomRange(int low, int high) {
        Random generator = new Random();
        return generator.nextInt(high - low + 1) + low;
    }

    public abstract void display(Graphics page);
    public abstract void move();
}
Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

In line 13, once your shape has height greater than 15, it will grow in height (not width) non-stop until it looks like a line because line 20 will determine the growth???

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: