Hey everyone, I am new to the site and to programming so please be gentle.

I am working on a simple project where I use pre-created classes that define shapes and call the methods within them to create a picture on a canvas.

I have most of this down, but my problem comes in when I want to animate the shapes. I would like to have all the shapes of a particular image to move at the same time, but instead I get each individual shapes moving one after another.

Here is an example of one of the methods I call to move the shapes:

public void slowMoveHorizontal(int distance)
    {
        int delta;

        if(distance < 0) 
        {
            delta = -1;
            distance = -distance;
        }
        else 
        {
            delta = 1;
        }

        for(int i = 0; i < distance; i++)
        {
            xPosition += delta;
            draw();
        }
    }

My goal was to create sets of images in different classes, then simply create an object from those classes so I didn't have to write 100+ line of code to make 20 different images.

I created this class to deal with one of the images.

public class Alien implements Runnable
{    
    Shape square1 = new Shape();
    Shape square2 = new Shape();
    Shape square3 = new Shape();
    Shape square4 = new Shape();
    Shape rectangle5 = new Shape();
    Shape rectangle6 = new Shape();
    Shape square7 = new Shape();
    Shape square8 = new Shape();
    Shape rectangle9 = new Shape();
    Shape rectangle10 = new Shape();
    Shape rectangle11 = new Shape();
    Shape rectangle12 = new Shape();
    Shape rectangle13 = new Shape();
    Shape rectangle14 = new Shape();
    Shape rectangle15 = new Shape();
    Shape rectangle16 = new Shape();
//xC is the xPosition of the center of the canvas, and yC is the yPosition of the center
// n is the scale variable.
    Canvas canvas = Canvas.getCanvas();         
    int windowWidth = canvas.getWidth();
    int windowHeight = canvas.getHeight();
    int xC = windowWidth/2;
    int yC = windowHeight/2;
    int n;
    
    public void build(int newX, int newY, int scale)
    {
        n = scale;
        int xN = newX+xC;
        int yN = newY+yC;
// Squares call the "s" method and take parameters (size, xPosition, yPosition, color)
        square1.s(n*1,(xN+(n*3)),(yN+(n*1)),"white");
        square2.s(n*1,(xN+(n*9)),(yN+(n*1)),"white");
        square3.s(n*1,(xN+(n*4)),(yN+(n*2)),"white");
        square4.s(n*1,(xN+(n*8)),(yN+(n*2)),"white");
// Rectangles call the "r" method and take parameters (width, height, xPos, yPos, color)
        rectangle5.r(n*7, n*1,(xN+(n*3)),(yN+(n*3)),"white");
        rectangle6.r(n*9, n*1,(xN+(n*2)),(yN+(n*4)),"white");
        square7.s(n*1,(xN+(n*8)),(yN+(n*4)),"black");
        square8.s(n*1,(xN+(n*4)),(yN+(n*4)),"black");
        rectangle9.r(n*11, n*1,(xN+(n*1)),(yN+(n*5)),"white");
        rectangle10.r(n*11, n*1,(xN+(n*1)),(yN+(n*6)),"white");
        rectangle11.r(n*11, n*1,(xN+(n*1)),(yN+(n*7)),"white");
        rectangle12.r(n*5, n*1,(xN+(n*4)),(yN+(n*8)),"white");
        rectangle13.r(n*1, n*2,(xN+(n*2)),(yN+(n*6)),"black");
        rectangle14.r(n*1, n*2,(xN+(n*10)),(yN+(n*6)),"black");
        rectangle15.r(n*1, n*1,(xN+(n*6)),(yN+(n*8)),"black");
        rectangle16.r(n*5, n*1,(xN+(n*4)),(yN+(n*7)),"black");
        draw();
    }
    
    public void move(int xMove, int yMove)
    {
        square1.slowMove(xMove, yMove);
        square2.slowMove(xMove, yMove);
        square3.slowMove(xMove, yMove);
        square4.slowMove(xMove, yMove);
        rectangle5.slowMove(xMove,yMove);
        rectangle6.slowMove(xMove,yMove);
        square7.slowMove(xMove, yMove);
        square8.slowMove(xMove, yMove);
        rectangle9.slowMove(xMove,yMove);
        rectangle10.slowMove(xMove,yMove);
        rectangle11.slowMove(xMove,yMove);
        rectangle12.slowMove(xMove,yMove);
        rectangle13.slowMove(xMove,yMove);
        rectangle14.slowMove(xMove,yMove);
        rectangle15.slowMove(xMove,yMove);
        rectangle16.slowMove(xMove,yMove);
    }
    
    public void draw()
    {
        square1.draw();
        square2.draw();
        square3.draw();
        square4.draw();
        rectangle5.draw();
        rectangle6.draw();
        square7.draw();
        square8.draw();
        rectangle9.draw();
        rectangle10.draw();
        rectangle11.draw();
        rectangle12.draw();
        rectangle13.draw();
        rectangle14.draw();
        rectangle15.draw();
        rectangle16.draw();
    }
    
    public void erase()
    {
        square1.erase();
        square2.erase();
        square3.erase();
        square4.erase();
        rectangle5.erase();
        rectangle6.erase();
        square7.erase();
        square8.erase();
        rectangle9.erase();
        rectangle10.erase();
        rectangle11.erase();
        rectangle12.erase();
        rectangle13.erase();
        rectangle14.erase();
        rectangle15.erase();
        rectangle16.erase();
    }
 
}

The all the images I create are assembled and displayed in the main class where method calls will set the scale, animate the image, draw the image, and erase the image.

Keep in mind that the instructions of the assignment are to NOT change the original shape classes, which means I can not re-write the "slowMoveHorizontal" method. I am free to make new classes and methods as long as they use the original shape classes.

I understand that I may need to use threads, but I am not sure how to implement them to the code I have already written. If anyone has any ideas please let me know.

If you need more information I am happy to add it asap.

Thanks!
-Hatt

The problem is most likely in the code where you call the individual animation methods. Can you post that code as well?
What thread do you run those calls on? If it's in an actionPerformed or anything like that you have blocked the Swing Event Dispatch Thread (EDT) - Google for more info

Sorry for the late reply. Just to reiterate, my goal is to move a number of primitive shapes all at once in order to create the illusion of an entire object moving across the screen.

My problem is simply adapting the code with threads, I have yet to write any thread code in.

Here is the code I use to call the final objects; it is my main class.

public class MyPicture
{
    /**
     * Constructor for objects of class MyPicture
     */
    static public Background background = new Background();
    static public Alien alien1 = new Alien();
    static public Alien alien2 = new Alien();
    static public Alien alien3 = new Alien();
    private static int scale;
    
    
    public MyPicture(int scale)
    {
        this.scale = scale;
    }
    
    public void createPicture()
    {
        erase();
        background.background(scale);
// the Alien shapes are adjusted with parameters (xPosition, yPosition, scale).
// they are moved relative to the center of the canvas.
        alien1.build(-30,-20,scale);
        alien2.build(100,50,scale);
        alien3.build(-200,150,scale);
    }
    
    public void animate()
    {
// the slowMoveHorizontal and slowMoveVertical are called en-mass on all primitive shapes in the Alien class with the parameters (xDistance, yDistance)
        alien1.move(30,30);
    }
    
    public void draw()
    {
        erase();
        reDraw();
    }
    
    
    private void reDraw()
    {
        alien1.draw();
        alien2.draw();
        alien3.draw();
    }
    
    public void erase()
    {
        alien3.erase();
        alien2.erase();
        alien1.erase();
    }
        
}

Thanks again
-Hatt

There has to be some kind of loop or timer to animate this. Where is it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.