简体   繁体   English

如何在沿y轴移动的同时沿其轴旋转Area对象?

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

So I have an Area object that I have created and made into a 4 star polygon. 因此,我创建了一个Area对象并将其制成4星多边形。 What I want to do is rotate this object while simultaneously having it translate along the y axis. 我想做的是旋转该对象,同时使其沿y轴平移。 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: 这是我用于创建对象的点的代码,星星的中心是xloc和yloc,其绘画功能位于其下方:

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. 使用getRotateInstance时,它沿xloc和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. 我能想到的唯一解决方案是更改xPoints []和yPoints [],但到目前为止我还没有成功。 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 = AffineTransform.getRotateInstance替换为:

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. 我猜想这与xloc和yloc点的位置有关。 Based on my tests before, the xloc and yloc should be centered at its origin so it should be spinning correctly. 根据我之前的测试,xloc和yloc应该以其原点为中心,以便正确旋转。 Any suggestions? 有什么建议么?

EDIT 2: I received advice saying I should just use the Graphics2D functions for translation and rotation. 编辑2:我收到建议说我应该只使用Graphics2D函数进行平移和旋转。 I changed the AffineTransform statements with this: 我用以下方法更改了AffineTransform语句:

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. 因此,它现在要做的是在其原点旋转,并绕一个我无法确定的附加点旋转,同时还要沿y轴平移。 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? 我怎样才能解决这个问题?

By changing double xloc = 150, yloc = 250; 通过更改double xloc = 150, yloc = 250; to int xloc = 150, yloc = 250; 转换为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. 形状必须为int形式,因此通过将变量加倍并将其应用于变换,会弄乱它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM