我的伪 3D 程序正在绘制一个点而不是一条线
My pseudo-3D program is drawing a dot instead of a line
我的 class 伪 3D 程序在 0,0 处制作一个点而不是一条线。我看不出它应该这样做的原因。我对 java 比较陌生。这也是第一次发帖提问。如果我的代码很乱,那就是原因。
我设想此代码能够通过使用 Calculate3D(x, y, z) 方法输入新位置来制作可以在 x、y 和 z 轴上移动的伪 3D。感谢您的帮助!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class threeD extends JPanel implements ActionListener {
public static int WIDTH = 400;
public static int HEIGHT = 400;
int counter = 0;
int x1, y1, x2, y2, FOV = 150, xpo = 0, ypo = 0, zpo = 80, fx1 = 20, fy1 = 20;
private JFrame window;
private Timer timer;
public static void main(String[] args) {
new threeD().start();
}
public void start() {
window = new JFrame();
window.setLayout(new BorderLayout());
window.add(this, BorderLayout.CENTER);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
timer = new Timer(1000 / 60, this);
}
public void Calculate3D(int x, int y, int z) {
x1 = fx1;
y1 = fy1;
x2 = FOV * ((x + xpo) / (z + zpo));
y2 = FOV * ((y + ypo) / (z + zpo));
fx1 = x2;
fy1 = y2;
counter++;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
for (int i = 0; i < counter; i++) {
g.drawLine(x1, y1, x2, y2);
}
}
@Override
public void actionPerformed(ActionEvent e) {
Calculate3D(20, 20, 0);
repaint();
}
}
好像是你的actionPerformed
,所以没有调用Calculate3D
。如果您在例如start
,你会看到线条画好了。如果您打算在单击按钮等时画线,请参阅此 Tutorial 了解更多信息。
我的 class 伪 3D 程序在 0,0 处制作一个点而不是一条线。我看不出它应该这样做的原因。我对 java 比较陌生。这也是第一次发帖提问。如果我的代码很乱,那就是原因。 我设想此代码能够通过使用 Calculate3D(x, y, z) 方法输入新位置来制作可以在 x、y 和 z 轴上移动的伪 3D。感谢您的帮助!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class threeD extends JPanel implements ActionListener {
public static int WIDTH = 400;
public static int HEIGHT = 400;
int counter = 0;
int x1, y1, x2, y2, FOV = 150, xpo = 0, ypo = 0, zpo = 80, fx1 = 20, fy1 = 20;
private JFrame window;
private Timer timer;
public static void main(String[] args) {
new threeD().start();
}
public void start() {
window = new JFrame();
window.setLayout(new BorderLayout());
window.add(this, BorderLayout.CENTER);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
timer = new Timer(1000 / 60, this);
}
public void Calculate3D(int x, int y, int z) {
x1 = fx1;
y1 = fy1;
x2 = FOV * ((x + xpo) / (z + zpo));
y2 = FOV * ((y + ypo) / (z + zpo));
fx1 = x2;
fy1 = y2;
counter++;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
for (int i = 0; i < counter; i++) {
g.drawLine(x1, y1, x2, y2);
}
}
@Override
public void actionPerformed(ActionEvent e) {
Calculate3D(20, 20, 0);
repaint();
}
}
好像是你的actionPerformed
,所以没有调用Calculate3D
。如果您在例如start
,你会看到线条画好了。如果您打算在单击按钮等时画线,请参阅此 Tutorial 了解更多信息。