java - How can I rotate an Area object on its axis while also moving it along the y axis? -
so have area object have created , made 4 star polygon. want rotate object while simultaneously having translate along y axis. code points use create object , center of star xloc , yloc paint function below that:
double rots = 0.0; int xpoints[] = { 45, 48, 56, 48, 45, 42, 34, 42 }; int ypoints[] = { 56, 47, 45, 43, 34, 43, 45, 47 }; double xloc = 150, yloc = 250;
public void paint(graphics g) { graphics2d g2d = (graphics2d) g; renderinghints rh = new renderinghints( renderinghints.key_antialiasing, renderinghints.value_antialias_on); rh.put(renderinghints.key_rendering, renderinghints.value_render_quality); g2d.setrenderinghints(rh); g2d.translate(xloc, yloc); g2d.rotate(rots); g2d.fill(star); //yloc += .01; rots += .001; repaint(); }
the problem i'm having in paint function. getrotateinstance spins along axis of xloc , yloc. when numbers incremented, star's rotation widens out origin encompass whole screen. solution can think of change xpoints[] , ypoints[] haven't been successful far. there way make work or way fix problem i'm having?
edit: made progress! took advice given , i'm close. replace code affinetransform trans = affinetransform.getrotateinstance
this:
affinetransform trans = new affinetransform(); trans.settotranslation(xloc, yloc); trans.rotate(rots);
now rotates , moves across screen though doesn't rotate around increasingly large field, still rotates around point can't seem make out. i'm guess has xloc , yloc points are. based on tests before, xloc , yloc should centered @ origin should spinning correctly. suggestions?
edit 2: received advice saying should use graphics2d functions translation , rotation. changed affinetransform statements this:
g2d.translate(xloc, yloc); g2d.rotate(rots);
the problem having while star translate , rotate, seems making additional rotation. rotate @ origin , rotates around additional point can't make out while translating along y axis. have better better understanding of looks like, imagine planet rotating night , day, circling star , moving in straight line. have updated code above reflect changes. how can fix this?
by changing double xloc = 150, yloc = 250;
int xloc = 150, yloc = 250;
fixed problem. however, moving fast added timer slow things down , it.
my guess ints not playing doubles. shapes had in int making variables double , applying tranformation, messing up.
Comments
Post a Comment