gpt4 book ai didi

java - 减少 JFrame 绘画中的延迟

转载 作者:行者123 更新时间:2023-12-02 08:04:48 26 4
gpt4 key购买 nike

我正在尝试在 JFrame 上绘制一个立方体。

听起来很简单,但滞后很多。第 7 行和第 8 行通常闪烁得非常糟糕。

这是代码:

http://pastebin.com/ncDasST6

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

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

另外,有什么方法可以将 Applet 添加到 JFrame 中吗?

我尝试这样做:add(new Rotational());//它所基于的 JApplet 的名称。

谢谢,火

最佳答案

这个变体是否符合您的预期?有许多更改我没有费心去记录(因为我只是“玩弄”代码)。做一个差异。揭示变化的程度和性质。

在 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);
}
}
<小时/>

Originally was for Applet, but i wanted it to execute through a .jar file.

将小程序转换为更合理的东西是个好主意,但请注意,小程序可以(并且通常应该)打包到 Jar 中。

Also, any way to add an Applet to a JFrame?

这是可能的,使用此代码相对容易(除非混合 Swing (JFrame) 和 AWT (Applet) 组件),但不是最好的方法。最好创建一个混合体,例如 subway applet/application .

通过将自定义渲染从框架移动到 JPanel,代码已部分转换为混合代码,因为面板可以添加到框架或小程序(或窗口或对话框,或另一个面板或..)。

关于java - 减少 JFrame 绘画中的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8350700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com