Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以旋转在JPanel中绘制的多边形,但可以';t旋转嵌套在此JPanel内的JPanel中的多边形_Java_Paintcomponent - Fatal编程技术网

Java 可以旋转在JPanel中绘制的多边形,但可以';t旋转嵌套在此JPanel内的JPanel中的多边形

Java 可以旋转在JPanel中绘制的多边形,但可以';t旋转嵌套在此JPanel内的JPanel中的多边形,java,paintcomponent,Java,Paintcomponent,如果我在某个JPanel容器上绘制一个多边形,并在计时器上进行旋转,那么效果很好。但是,如果我在JPanel上绘制一个多边形,然后尝试旋转该多边形,它不会旋转。多边形坐标正在改变,JPanel(似乎)正在重新绘制,但它就是不旋转。这里有一些我的意思的示例代码。我知道还有一些bug需要解决,但我只关心如何让JPanel上的多边形旋转 package PolygonRotateTest; import java.awt.Color; import java.awt.Dimension; impor

如果我在某个JPanel容器上绘制一个多边形,并在计时器上进行旋转,那么效果很好。但是,如果我在JPanel上绘制一个多边形,然后尝试旋转该多边形,它不会旋转。多边形坐标正在改变,JPanel(似乎)正在重新绘制,但它就是不旋转。这里有一些我的意思的示例代码。我知道还有一些bug需要解决,但我只关心如何让JPanel上的多边形旋转

package PolygonRotateTest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class PolygonRotateTest {

    static ConvexPolygon polygon;
    static Timer t;
    static Model m1;
    static JPanel container;
    static JFrame frame;

    public static void main(String[] args) {
        //Small compiler issue with "no enclosing instance of type PolygonRotateTest is in scope"
        //because I lazily copy-pasted critical classes into one to make this more convenient.
        //Should still run fine if you ignore the warning.

        //ConvexPolygon makes a pentagon centered at (300,200) with a radius of 100
        polygon = new ConvexPolygon(new Coordinate(300, 200), 100, 5);

        //Model extends JPanel, accepts a polygon.
        m1 = new BallModel(polygon, Color.red);

        //Draws the polygon on the container
        container = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.fillPolygon(polygon);
            }
        };

        t = new Timer(1000 / 60, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                m1.rotate(Math.toRadians(1));
                container.repaint();
                m1.repaint();
                //Rotates the polygon 60 times per second.
                //Drawing the black polygon directly onto the container rotates correctly
                //However, the model (m1) does not redraw/rotate the red polygon
                //despite using the same polygon.

            }
        });
        t.start();

        container.setPreferredSize(new Dimension(800, 600));

        frame = new JFrame();
        frame.add(container);
        container.add(m1);

        frame.setVisible(true);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    ///Other stuff begins here///////////////
    public static abstract class ActorPolygon extends Polygon {

        private Coordinate center;

        public ActorPolygon(Coordinate center) {
            this.center = center;

        }

        public void rotate(double radianIncrement) {


            for (int i = 0; i < npoints; i++) {
                Coordinate rotate = rotatePoint(new Coordinate(xpoints[i], ypoints[i]), radianIncrement);

                xpoints[i] = rotate.getX();
                ypoints[i] = rotate.getY();

            }


        }

        public Coordinate rotatePoint(Coordinate c, double radianIncrement) {

            double xDist = c.getX() - getCenter().getX();
            double yDist = c.getY() - getCenter().getY();

            Coordinate rotated = new Coordinate(
                    (int) (0.5 + center.getX() + xDist * Math.cos(radianIncrement) - yDist * Math.sin(radianIncrement)),
                    (int) (0.5 + center.getY() + xDist * Math.sin(radianIncrement) + yDist * Math.cos(radianIncrement)));

            return rotated;
        }

        @Override
        public void translate(int dx, int dy) {
            super.translate(dx, dy);
            int x = center.getX() + dx;
            int y = center.getY() + dy;
            center = new Coordinate(x, y);
        }

        @Override
        public String toString() {
            String output = "";
            output += "Center at " + center + "\n";
            output += "contains " + npoints + " points" + "\n";
            for (int i = 0; i < npoints; i++) {
                output += i + " " + "(" + xpoints[i] + "," + ypoints[i] + ")" + "\n";
            }
            return output;
        }

        public Coordinate getCenter() {
            return center;
        }

        @Override
        public abstract ActorPolygon clone();
    }

    public static class ConvexPolygon extends ActorPolygon {

        private int radius;

        public ConvexPolygon(Coordinate center, int r, int n) {
            super(center);
            this.npoints = n;
            this.radius = r;
            xpoints = new int[npoints];
            ypoints = new int[npoints];
            double radianIncrement = 2 * Math.PI / npoints;
            double shift = Math.PI / npoints - 3 * Math.PI / 2;

            for (int i = 0; i < npoints; i++) {
                xpoints[i] = getCenter().getX() + (int) (radius * Math.cos(i * radianIncrement + shift));
                ypoints[i] = getCenter().getY() + (int) (radius * Math.sin(i * radianIncrement + shift));
            }

        }

        public int getRadius() {
            return radius;
        }

        @Override
        public ConvexPolygon clone() {
            return new ConvexPolygon(getCenter(), getRadius(), npoints);
        }
    }

    public abstract class Model extends JPanel {

        private ActorPolygon original;

        public Model(ActorPolygon polygon) {
            this.original = polygon;
            this.setPreferredSize(original.getBounds().getSize());
            Rectangle bounds = polygon.getBounds();
            this.setBounds(bounds);

        }

        public ActorPolygon getActorPolygon() {
            return original;
        }

        public void rotate(double increment) {
            original.rotate(increment);
        }

        public void drawModel(Graphics g) {

            Rectangle bounds = getActorPolygon().getBounds();
            this.setBounds(bounds);
            this.setPreferredSize(this.getBounds().getSize());
            ActorPolygon shiftedPolygon = getActorPolygon().clone();
            shiftedPolygon.translate(
                    -1 * getActorPolygon().getCenter().getX() + (int) bounds.getWidth() / 2,
                    -1 * getActorPolygon().getCenter().getY() + (int) bounds.getHeight() / 2);
            g.fillPolygon(shiftedPolygon);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            drawModel(g);
        }

        @Override
        public String toString() {
            String output = original + this.getSize().toString() + "\n" + this.getLocation() + "\n";
            return output;
        }
    }

    public static class BallModel extends Model {

        private Color color;

        public BallModel(ConvexPolygon p, Color c) {
            super(p);
            color = c;
        }

        @Override
        public void drawModel(Graphics g) {

            g.setColor(color);
            super.drawModel(g);


        }
    }

    public static class Coordinate {

        private int x;
        private int y;

        public Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }
    }
}
封装PolygonRotateTest;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Polygon;
导入java.awt.Rectangle;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
公共类PolygonRotateTest{
静态凸多边形;
静态定时器t;
静态模型m1;
静态JPanel容器;
静态JFrame;
公共静态void main(字符串[]args){
//编译器出现“范围内没有PolygonRotateTest类型的封闭实例”的小问题
//因为我懒散地将粘贴的关键类复制到一个类中,以使其更方便。
//如果忽略此警告,仍应正常运行。
//凸面多边形形成一个以(300200)为中心、半径为100的五角大楼
多边形=新的凸多边形(新坐标(300,200),100,5);
//模型扩展JPanel,接受多边形。
m1=新的球形模型(多边形,颜色为红色);
//在容器上绘制多边形
container=newjpanel(){
@凌驾
公共组件(图形g){
超级组件(g);
g、 填充多边形(多边形);
}
};
t=新计时器(1000/60,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
m1.旋转(数学托拉迪安(1));
container.repaint();
m1.重新喷漆();
//每秒旋转多边形60次。
//将黑色多边形直接绘制到容器上可以正确旋转
//但是,模型(m1)不会重新绘制/旋转红色多边形
//尽管使用相同的多边形。
}
});
t、 start();
容器.setPreferredSize(新尺寸(800600));
frame=新的JFrame();
框架。添加(容器);
容器。添加(m1);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
///其他事情从这里开始///////////////
公共静态抽象类ActorPolygon扩展多边形{
私人协调中心;
公共ActorPolygon(坐标中心){
this.center=center;
}
公共空间旋转(双弧度增量){
对于(int i=0;i