user2109250
user2109250

Reputation:

How to make an existing polygon move in java?

Hey dudes and dudettes. High-school Comp Sci student reporting in.

Right now we're doing some work with plotting and polygons, and one of the challenges is to make it move across the screen in a variety of ways. (left to right, etc.) See, this would be no issue if I knew the language or had the means to study it at a basic level. But our teacher just hands us some environment and some code to copy and paste and look off of, none of which really helps in the long run, except for the most basic stuff.

So, my friends, I can make a polygon through plotting points as shown here:

int xPoints[]={  xP*684/1000,xP*706/1000, xP*661/1000, xP*687/1000,
                 xP*735/1000,xP*760/1000, xP*723/1000, xP*713/1000,
                 xP*698/1000,xP*686/1000, xP*669/1000, xP*653/1000,
                 xP*639/1000,xP*639/1000, xP*641/1000, xP*648/1000};


int yPoints[]={ yP*354/1000, yP*354/1000, yP*472/1000, yP*472/1000,
                yP*354/1000, yP*354/1000, yP*452/1000, yP*471/1000,
                yP*487/1000, yP*498/1000, yP*503/1000, yP*504/1000,
                yP*492/1000, yP*473/1000, yP*455/1000, yP*440/1000};
int numPoints=16;

But I cannot figure out for the life of me what kind of specific input or code is required to make it move. (by that I mean translate across the the screen) Any hints to get me on the right track would be greatly appreciated. I don't exactly have the means of figuring this out on my own.

Edit: Whoops, yep it's Java.

Upvotes: 0

Views: 4644

Answers (4)

user949300
user949300

Reputation: 15729

Assuming that it's a java.awt.Polygon, have you looked at

Polygon.translate(int dx, int dy); ???

If it is some other kind of Polygon, check to see if it has a similar method.

Upvotes: 0

Navin
Navin

Reputation: 4107

Maybe a for loop?

for(int i=0;i<xPoints.length;i++)
    xPoints[i]+=1;// shift 1 unit
for(int i=0;i<yPoints.length;i++)
    yPoints[i]+=1;// shift 1 unit

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83527

Welcome to SO! You need to break this down into small parts that you need to figure out how to do. Some that come to mind are

  1. Create a window to draw in.

  2. Draw a line in that window.

  3. Draw a polygon in the window.

  4. Move a point from (x, y) in a given direction.

Each of these can likely be broken down even further. Often solving a programming problem starts with this kind of thinking. As you try to do each of these, and any others that you come up with, please feel free to come back and ask more questions.

Upvotes: 1

Jorge Bellon
Jorge Bellon

Reputation: 43

I don't know IBM VisualAge but if you have any draw method/loop, you should increase xP to move to right, decrease it to move left etc. with a for loop, for example.

Upvotes: 0

Related Questions