Java 减少JFrame喷漆中的延迟

Java 减少JFrame喷漆中的延迟,java,swing,jframe,3d,paint,Java,Swing,Jframe,3d,Paint,我试图在一个框架上画一个立方体 听起来很简单,但却滞后了很多。第七行和第八行通常闪烁得很糟糕 代码如下: 如果有人能给我一两个关于如何阻止这种延迟发生的提示,那就太好了:D 最初用于Applet,但我希望它通过.jar文件执行 还有,有没有办法将小程序添加到JFrame 我试过这样做:添加(新的旋转())//它是基于JApplet的名字 谢谢,Fire此变体是否符合您的期望?有许多更改我都懒得记录下来(因为我只是在“玩”代码)。做一个差异,以揭示变化的程度和性质 在700x700时,它不会显示

我试图在一个框架上画一个立方体

听起来很简单,但却滞后了很多。第七行和第八行通常闪烁得很糟糕

代码如下:

如果有人能给我一两个关于如何阻止这种延迟发生的提示,那就太好了:D

最初用于Applet,但我希望它通过.jar文件执行

还有,有没有办法将小程序添加到JFrame

我试过这样做:添加(新的旋转())//它是基于JApplet的名字


谢谢,Fire

此变体是否符合您的期望?有许多更改我都懒得记录下来(因为我只是在“玩”代码)。做一个差异,以揭示变化的程度和性质

在700x700时,它不会显示延迟或渲染瑕疵

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EmptyBorder;

public class Square extends JPanel implements MouseListener,
        MouseMotionListener {

    private static final long serialVersionUID = 1L;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame f = new JFrame("Cube Rotational");
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    Square square = new Square();
                    square.setBorder(new EmptyBorder(5,5,5,5));
                    f.setContentPane(square);
                    f.pack();
                    f.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Square() {
        init();
        setPreferredSize(new Dimension(700,700));
    }

    class Point3D {
        public int x, y, z;

        public Point3D(int X, int Y, int Z) {
            x = X;
            y = Y;
            z = Z;
        }
    }

    class Edge {
        public int a, b;

        public Edge(int A, int B) {
            a = A;
            b = B;
        }
    }

    static int width, height;
    static int mx, my;

    static int azimuth = 45, elevation = 45;

    static Point3D[] vertices;
    static Edge[] edges;

    public void init() {

        width = 500;
        height = 500;

        vertices = new Point3D[8];
        vertices[0] = new Point3D(-1, -1, -1);
        vertices[1] = new Point3D(-1, -1, 1);
        vertices[2] = new Point3D(-1, 1, -1);
        vertices[3] = new Point3D(-1, 1, 1);
        vertices[4] = new Point3D(1, 1, -1);
        vertices[5] = new Point3D(1, 1, 1);
        vertices[6] = new Point3D(1, -1, -1);
        vertices[7] = new Point3D(1, -1, 1);
        edges = new Edge[12];
        edges[0] = new Edge(0, 1);
        edges[1] = new Edge(0, 2);
        edges[2] = new Edge(0, 6);
        edges[3] = new Edge(1, 3);
        edges[4] = new Edge(1, 7);
        edges[5] = new Edge(2, 3);
        edges[6] = new Edge(2, 4);
        edges[7] = new Edge(3, 5);
        edges[8] = new Edge(4, 5);
        edges[9] = new Edge(4, 6);
        edges[10] = new Edge(5, 7);
        edges[11] = new Edge(6, 7);

        setCursor(new Cursor(Cursor.HAND_CURSOR));
        addMouseListener(this);
        addMouseMotionListener(this);
        setVisible(true);
    }

    void drawWireframe(Graphics g) {
        double theta = Math.PI * azimuth / 180.0;
        double phi = Math.PI * elevation / 180.0;
        float cosT = (float) Math.cos(theta);
        float sinT = (float) Math.sin(theta);
        float cosP = (float) Math.cos(phi);
        float sinP = (float) Math.sin(phi);
        float cosTcosP = cosT * cosP;
        float cosTsinP = cosT * sinP;
        float sinTcosP = sinT * cosP;
        float sinTsinP = sinT * sinP;
        Point[] points;
        points = new Point[vertices.length];
        float scaleFactor = (getWidth() + getHeight()) / 8;
        float near = (float) 6;
        float nearToObj = 1.5f;
        for (int j = 0; j < vertices.length; ++j) {
            int x0 = vertices[j].x;
            int y0 = vertices[j].y;
            int z0 = vertices[j].z;
            float x1 = cosT * x0 + sinT * z0;
            float y1 = -sinTsinP * x0 + cosP * y0 + cosTsinP * z0;
            float z1 = cosTcosP * z0 - sinTcosP * x0 - sinP * y0;
            x1 = x1 * near / (z1 + near + nearToObj);
            y1 = y1 * near / (z1 + near + nearToObj);
            points[j] = new Point(
                    (int) (getWidth() / 2 + scaleFactor * x1 + 0.5),
                    (int) (getHeight() / 2 - scaleFactor * y1 + 0.5));
        }
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.white);
        for (int j = 0; j < edges.length; ++j) {
            int x1 = points[edges[j].a].x;
            int x2 = points[edges[j].b].x;
            int y1 = points[edges[j].a].y;
            int y2 = points[edges[j].b].y;
            ((Graphics2D) g).setStroke(new BasicStroke(5));
            g.drawLine(x1, y1, x2, y2);
        }
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        mx = e.getX();
        my = e.getY();
        e.consume();
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        int new_mx = e.getX();
        int new_my = e.getY();
        azimuth -= new_mx - mx;
        azimuth %= 360;
        elevation += new_my - my;
        elevation %= 360;
        repaint();
        mx = new_mx;
        my = new_my;

        repaint();
        e.consume();
    }

    @Override
    public void paintComponent(Graphics g) {
        drawWireframe(g);
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.GroupLayout.Alignment;
导入javax.swing.border.EmptyBorder;
公共类Square扩展JPanel实现MouseListener,
MouseMotionListener{
私有静态最终长serialVersionUID=1L;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
JFrame f=新JFrame(“立方体旋转”);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
正方形=新正方形();
平方阶(新的空阶(5,5,5,5));
f、 设置内容窗格(方形);
f、 包装();
f、 setVisible(真);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公众广场(){
init();
设置首选尺寸(新尺寸(700700));
}
类Point3D{
公共整数x,y,z;
公共点3D(整数X,整数Y,整数Z){
x=x;
y=y;
z=z;
}
}
阶级边缘{
公共INTA、b;
公共边缘(内部A、内部B){
a=a;
b=b;
}
}
静态int宽度、高度;
静态int-mx,my;
静态int方位角=45,仰角=45;
静态点3D[]顶点;
静态边[]边;
公共void init(){
宽度=500;
高度=500;
顶点=新点3D[8];
顶点[0]=新点3D(-1,-1,-1);
顶点[1]=新点3D(-1,-1,1);
顶点[2]=新点3D(-1,1,-1);
顶点[3]=新点3D(-1,1,1);
顶点[4]=新点3D(1,1,-1);
顶点[5]=新点3D(1,1,1);
顶点[6]=新点3D(1,-1,-1);
顶点[7]=新点3D(1,-1,1);
边=新边[12];
边[0]=新边(0,1);
边[1]=新边(0,2);
边[2]=新边(0,6);
边[3]=新边(1,3);
边[4]=新边(1,7);
边[5]=新边(2,3);
边[6]=新边(2,4);
边[7]=新边(3,5);
边[8]=新边(4,5);
边[9]=新边(4,6);
边[10]=新边(5,7);
边[11]=新边(6,7);
setCursor(新光标(Cursor.HAND_光标));
addMouseListener(这个);
addMouseMotionListener(此);
setVisible(真);
}
空心拉线框(图g){
双θ=Math.PI*方位角/180.0;
双φ=数学π*标高/180.0;
浮动成本=(浮动)数学cos(θ);
浮点sinT=(浮点)数学sin(θ);
float cosP=(float)Math.cos(phi);
float sinP=(float)数学sin(phi);
浮动成本成本成本=成本*成本成本成本;
浮动成本sinP=成本*sinP;
浮点数sinTcosP=sinT*cosP;
浮点数sinT=sinT*sinP;
点[]点;
点=新点[顶点.长度];
浮点比例因子=(getWidth()+getHeight())/8;
浮动近=(浮动)6;
在Toobj附近浮动=1.5f;
对于(int j=0;j