How can I rotate an Area object on its axis while also moving it along the y axis?

180 views Asked by At

So I have an Area object that I have created and made into a 4 star polygon. What I want to do is rotate this object while simultaneously having it translate along the y axis. This is my code for the points I use to create the object and the center of the star is the xloc and yloc with the 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 is in the paint function. With getRotateInstance it spins along the axis of the xloc and yloc. When those numbers are incremented, the star's rotation widens out from its origin to encompass the whole screen. The only solution I can think of would be to change the xPoints[] and yPoints[] but I haven't been successful so far. Is there another way to make this work or a way to fix the problem I'm currently having?

EDIT: So I made some progress! I took the advice given and I'm close. I replace the code AffineTransform trans = AffineTransform.getRotateInstance with this:

AffineTransform trans = new AffineTransform(); trans.setToTranslation(xloc, yloc); trans.rotate(rots);

Now it rotates and moves across the screen but though it doesn't rotate around an increasingly large field, it still rotates around a point that I can't seem to make out. I'm guess it has to do with where the xloc and yloc points are. Based on my tests before, the xloc and yloc should be centered at its origin so it should be spinning correctly. Any suggestions?

EDIT 2: I received advice saying I should just use the Graphics2D functions for translation and rotation. I changed the AffineTransform statements with this:

g2d.translate(xloc, yloc); g2d.rotate(rots);

The problem I am having now is that while the star does translate and rotate, it also seems to be making an additional rotation. So what it does now is rotate at its origin and rotates around an additional point that I can't make out while also translating along the y axis. To have a better better understanding of what this looks like, imagine a planet rotating for night and day, also circling a star and then moving in a somewhat straight line. I have updated the code above to reflect my changes. How can I fix this?

1

There are 1 answers

0
VicDid On BEST ANSWER

By changing double xloc = 150, yloc = 250; to int xloc = 150, yloc = 250; it fixed the problem. However, everything was moving too fast so I added a timer to slow things down and that was it.

My guess was that the ints were not playing well with the doubles. The shapes had to be in int so by making variables double and applying it to the tranformation, it was messing it up.