We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,571 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

method to change shapes not working

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();
}
2
Contributors
1
Reply
2 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
Mi_99
Newbie Poster
3 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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???

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 376
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0540 seconds using 2.68MB