/    Sign up×
Community /Pin to ProfileBookmark

java drawing help.

basically im fairly new to coding in java and im trying to animate some shapes in a drawing window. everything is fine with the code so far in terms of shapes, sizes etc but all i need to know is how to stop my final else if statement going off the edge of the window..ive tried changing the counter but it doesnt seem to be working. cheers

import element.*;
import java.awt.Color;
public class …
{

public static void main(String args[])

{

final int Radius = 40;
DrawingWindow d = new DrawingWindow(500,500); // window is 500 by 500 pixels

int Xpos = 250, Ypos = 250, counter = 0; // size of initial shape
float rect_corner = (float)Radius*2;

while (true)
{
d.hold();
d.clearRect(0,0,500,500);

Rect rect = new Rect(Xpos-Radius,Ypos-Radius,Radius*2,Radius*2);
d.fill(new RoundRect(rect,(int)rect_corner,(int)rect_corner));

// to middle right
if (counter < 125)
{
d.setForeground(Color.red); //change the colour of the shape
Xpos += 1;
}

// to bottom right
else if (counter < 250)
{
d.setForeground(Color.cyan);
Ypos += 1;
}

// to bottom left
else if (counter < 500)
{
if (rect_corner > 0) rect_corner -= 0.5; // changes the shape into a square by moving out the corners

d.setForeground(Color.yellow);
Xpos -= 1;

}

// to top left
else if (counter > 500)
{
if (rect_corner < 0) rect_corner += 1.0;
d.setForeground(Color.GREEN);
Ypos -= 1;
}
counter++;

d.release();

}
}

}

to post a comment
Java

0Be the first to comment 😎

×

Success!

Help @jones999 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...