我正在使用 Java Graphics 开发一个简单的 3D 渲染器,它目前可以渲染一堆组成一个简单平面的多边形。问题是,边缘上的一些线条在渲染几秒钟后消失了。怎么了?
package com.zove.renderer;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public final class Frame extends JPanel implements KeyListener{
volatile int x1 = 0;
volatile int x2 = 0;
volatile int y1 = 0;
volatile int y2 = 550;
volatile int x3 = 0;
volatile int y3 = 0;
volatile int x4 = 800;
volatile int y4 = 0;
volatile int angle = 0;
volatile int i;
public JFrame f;
public Frame(){
f = new JFrame();
f.setVisible(true);
f.add(this);
f.setResizable(false);
f.setSize(800,550);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addKeyListener(this);
}
public void paint(Graphics g){
super.paint(g);
for(i=0; i<2000;){
if(x1+i+angle<2600){
//Vertical
g.drawLine(x1+i+angle, y1,x2+i-angle*2,y2);
}
else{
g.drawLine(x1+i+angle, y1-i/200,x2+i-angle,y2+angle);
}
//Horizontal
g.drawLine(x3, y3+i-angle/2,x4,y4+i+angle/2);
i +=10*angle/100;
}
}
public static void main(String[] args){
Thread s = new Thread(new repainter());
s.start();
}
@Override
public void keyPressed(KeyEvent arg0) {
i -=20;
angle -=20;
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
class repainter implements Runnable{
public void run() {
Frame f = new Frame();
while(true){
f.repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(f.x1+f.i+f.angle);
f.angle +=10;
}
}
}