3

我正在尝试用下载的小程序制作游戏。我更改了所有内容,直到没有错误为止,但是当我运行它时,它显示一个空框架,我不知道出了什么问题!

a.java

package game;

import javax.swing.JFrame;

public class a {
public a() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.add(new b());
    frame.setVisible(true);
}

    public static void main(String [] args) {
    a a = new a();
    }
}

b.java

package game;

import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

public class b extends JPanel implements Runnable {

Thread thread;
static final int TILE_SIZE = 16;
static final int WALL_HEIGHT = 16;
static final int PROJECTIONPLANEWIDTH = 320;
static final int PROJECTIONPLANEHEIGHT = 200;
static final int ANGLE60 = PROJECTIONPLANEWIDTH;
static final int ANGLE30 = (ANGLE60 / 2);
static final int ANGLE15 = (ANGLE30 / 2);
static final int ANGLE90 = (ANGLE30 * 3);
static final int ANGLE180 = (ANGLE90 * 2);
static final int ANGLE270 = (ANGLE90 * 3);
static final int ANGLE360 = (ANGLE60 * 6);
static final int ANGLE0 = 0;
static final int ANGLE5 = (ANGLE30 / 6);
static final int ANGLE10 = (ANGLE5 * 2);
float fSinTable[];
float fISinTable[];
float fCosTable[];
float fICosTable[];
float fTanTable[];
float fITanTable[];
float fFishTable[];
float fXStepTable[];
float fYStepTable[];
Image fOffscreenImage;
Graphics fOffscreenGraphics;
int fPlayerX = 100;
int fPlayerY = 160;
int fPlayerArc = ANGLE0;
int fPlayerDistanceToTheProjectionPlane = 277;
int fPlayerHeight = 32;
int fPlayerSpeed = 8;
int fProjectionPlaneYCenter = PROJECTIONPLANEHEIGHT / 2;
boolean fKeyUp = false;
boolean fKeyDown = false;
boolean fKeyLeft = false;
boolean fKeyRight = false;
byte fMap[];
static final byte W = 1;                                // wall
static final byte O = 0;                                // opening
static final int MAP_WIDTH = 12;
static final int MAP_HEIGHT = 12;
private boolean run = true;

float arcToRad(float arcAngle) {
    return ((float) (arcAngle * Math.PI) / (float) ANGLE180);
}

public void createTables() {
    int i;
    float radian;
    fSinTable = new float[ANGLE360 + 1];
    fISinTable = new float[ANGLE360 + 1];
    fCosTable = new float[ANGLE360 + 1];
    fICosTable = new float[ANGLE360 + 1];
    fTanTable = new float[ANGLE360 + 1];
    fITanTable = new float[ANGLE360 + 1];
    fFishTable = new float[ANGLE60 + 1];
    fXStepTable = new float[ANGLE360 + 1];
    fYStepTable = new float[ANGLE360 + 1];

    for (i = 0; i <= ANGLE360; i++) {
        radian = arcToRad(i) + (float) (0.0001);
        fSinTable[i] = (float) Math.sin(radian);
        fISinTable[i] = (1.0F / (fSinTable[i]));
        fCosTable[i] = (float) Math.cos(radian);
        fICosTable[i] = (1.0F / (fCosTable[i]));
        fTanTable[i] = (float) Math.tan(radian);
        fITanTable[i] = (1.0F / fTanTable[i]);
        if (i >= ANGLE90 && i < ANGLE270) {
            fXStepTable[i] = (float) (TILE_SIZE / fTanTable[i]);
            if (fXStepTable[i] > 0) {
                fXStepTable[i] = -fXStepTable[i];
            }
        } else {
            fXStepTable[i] = (float) (TILE_SIZE / fTanTable[i]);
            if (fXStepTable[i] < 0) {
                fXStepTable[i] = -fXStepTable[i];
            }
        }
        if (i >= ANGLE0 && i < ANGLE180) {
            fYStepTable[i] = (float) (TILE_SIZE * fTanTable[i]);
            if (fYStepTable[i] < 0) {
                fYStepTable[i] = -fYStepTable[i];
            }
        } else {
            fYStepTable[i] = (float) (TILE_SIZE * fTanTable[i]);
            if (fYStepTable[i] > 0) {
                fYStepTable[i] = -fYStepTable[i];
            }
        }
    }

    for (i = -ANGLE30; i <= ANGLE30; i++) {
        radian = arcToRad(i);
        fFishTable[i + ANGLE30] = (float) (1.0F / Math.cos(radian));
    }

    byte[] map = {
        W, W, W, W, W, W, W, W, W, W, W, W,
        W, O, O, O, O, O, O, O, O, O, O, W,
        W, O, O, O, O, O, O, O, O, O, O, W,
        W, O, O, O, O, O, O, O, W, O, O, W,
        W, O, O, W, O, W, O, O, W, O, O, W,
        W, O, O, W, O, W, W, O, W, O, O, W,
        W, O, O, W, O, O, W, O, W, O, O, W,
        W, O, O, O, W, O, W, O, W, O, O, W,
        W, O, O, O, W, O, W, O, W, O, O, W,
        W, O, O, O, W, W, W, O, W, O, O, W,
        W, O, O, O, O, O, O, O, O, O, O, W,
        W, W, W, W, W, W, W, W, W, W, W, W
    };
    fMap = map;
}

public void start() {
    createTables();
    thread = new Thread(this);
    thread.start();
}

public void run() {
    start();
    fOffscreenImage = createImage(size().width, size().height);
    fOffscreenGraphics = fOffscreenImage.getGraphics();

    while (true) {
        if (fKeyLeft) {
            if ((fPlayerArc -= ANGLE10) < ANGLE0) {
                fPlayerArc += ANGLE360;
            }
        } else if (fKeyRight) {
            if ((fPlayerArc += ANGLE10) >= ANGLE360) {
                fPlayerArc -= ANGLE360;
            }
        }

        float playerXDir = fCosTable[fPlayerArc];
        float playerYDir = fSinTable[fPlayerArc];

        if (fKeyUp) {
            fPlayerX += (int) (playerXDir * fPlayerSpeed);
            fPlayerY += (int) (playerYDir * fPlayerSpeed);
        } else if (fKeyDown) {
            fPlayerX -= (int) (playerXDir * fPlayerSpeed);
            fPlayerY -= (int) (playerYDir * fPlayerSpeed);
        }

        render();
        try {
            Thread.sleep(50);
        } catch (Exception sleepProblem) {
            System.out.println("Sleep problem");
        }
    }
}

public void drawBackground() {
    int c = 25;
    int r;
    for (r = 0; r < PROJECTIONPLANEHEIGHT / 2; r += 10) {
        fOffscreenGraphics.setColor(new Color(c, 125, 225));
        fOffscreenGraphics.fillRect(0, r, PROJECTIONPLANEWIDTH, 10);
        c += 20;
    }
    c = 22;
    for (; r < PROJECTIONPLANEHEIGHT; r += 15) {
        fOffscreenGraphics.setColor(new Color(c, 20, 20));
        fOffscreenGraphics.fillRect(0, r, PROJECTIONPLANEWIDTH, 15);
        c += 15;
    }
}

public void render() {
    drawBackground();

    int verticalGrid;
    int horizontalGrid;
    int distToNextVerticalGrid;
    int distToNextHorizontalGrid;
    float xIntersection;
    float yIntersection;
    float distToNextXIntersection;
    float distToNextYIntersection;

    int xGridIndex;
    int yGridIndex;

    float distToVerticalGridBeingHit;
    float distToHorizontalGridBeingHit;

    int castArc, castColumn;

    castArc = fPlayerArc;
    castArc -= ANGLE30;
    if (castArc < 0) {
        castArc = ANGLE360 + castArc;
    }
    for (castColumn = 0; castColumn < PROJECTIONPLANEWIDTH; castColumn += 5) {
        if (castArc > ANGLE0 && castArc < ANGLE180) {
            horizontalGrid = (fPlayerY / TILE_SIZE) * TILE_SIZE + TILE_SIZE;
            distToNextHorizontalGrid = TILE_SIZE;
            float xtemp = fITanTable[castArc] * (horizontalGrid - fPlayerY);
            xIntersection = xtemp + fPlayerX;
        } else {
            horizontalGrid = (fPlayerY / TILE_SIZE) * TILE_SIZE;
            distToNextHorizontalGrid = -TILE_SIZE;

            float xtemp = fITanTable[castArc] * (horizontalGrid - fPlayerY);
            xIntersection = xtemp + fPlayerX;

            horizontalGrid--;
        }
        if (castArc == ANGLE0 || castArc == ANGLE180) {
            distToHorizontalGridBeingHit = 9999999F;
        } else {
            distToNextXIntersection = fXStepTable[castArc];
            while (true) {
                xGridIndex = (int) (xIntersection / TILE_SIZE);
                yGridIndex = (horizontalGrid / TILE_SIZE);
                if ((xGridIndex >= MAP_WIDTH)
                        || (yGridIndex >= MAP_HEIGHT)
                        || xGridIndex < 0 || yGridIndex < 0) {
                    distToHorizontalGridBeingHit = Float.MAX_VALUE;
                    break;
                } else if ((fMap[yGridIndex * MAP_WIDTH + xGridIndex]) != O) {
                    distToHorizontalGridBeingHit = (xIntersection - fPlayerX) * fICosTable[castArc];
                    break;
                }
                else {
                    xIntersection += distToNextXIntersection;
                    horizontalGrid += distToNextHorizontalGrid;
                }
            }
        }

        if (castArc < ANGLE90 || castArc > ANGLE270) {
            verticalGrid = TILE_SIZE + (fPlayerX / TILE_SIZE) * TILE_SIZE;
            distToNextVerticalGrid = TILE_SIZE;

            float ytemp = fTanTable[castArc] * (verticalGrid - fPlayerX);
            yIntersection = ytemp + fPlayerY;
        }
        else {
            verticalGrid = (fPlayerX / TILE_SIZE) * TILE_SIZE;
            distToNextVerticalGrid = -TILE_SIZE;

            float ytemp = fTanTable[castArc] * (verticalGrid - fPlayerX);
            yIntersection = ytemp + fPlayerY;

            verticalGrid--;
        }
        if (castArc == ANGLE90 || castArc == ANGLE270) {
            distToVerticalGridBeingHit = 9999999;
        } else {
            distToNextYIntersection = fYStepTable[castArc];
            while (true) {
                xGridIndex = (verticalGrid / TILE_SIZE);
                yGridIndex = (int) (yIntersection / TILE_SIZE);

                if ((xGridIndex >= MAP_WIDTH)
                        || (yGridIndex >= MAP_HEIGHT)
                        || xGridIndex < 0 || yGridIndex < 0) {
                    distToVerticalGridBeingHit = Float.MAX_VALUE;
                    break;
                } else if ((fMap[yGridIndex * MAP_WIDTH + xGridIndex]) != O) {
                    distToVerticalGridBeingHit = (yIntersection - fPlayerY) * fISinTable[castArc];
                    break;
                } else {
                    yIntersection += distToNextYIntersection;
                    verticalGrid += distToNextVerticalGrid;
                }
            }
        }

        float scaleFactor;
        float dist;
        int topOfWall;
        int bottomOfWall;
        if (distToHorizontalGridBeingHit < distToVerticalGridBeingHit) {
            dist = distToHorizontalGridBeingHit;
            fOffscreenGraphics.setColor(Color.gray);
        } else {
            dist = distToVerticalGridBeingHit;
            fOffscreenGraphics.setColor(Color.darkGray);
        }
        dist /= fFishTable[castColumn];
        int projectedWallHeight = (int) (WALL_HEIGHT * (float) fPlayerDistanceToTheProjectionPlane / dist);
        bottomOfWall = fProjectionPlaneYCenter + (int) (projectedWallHeight * 0.5F);
        topOfWall = PROJECTIONPLANEHEIGHT - bottomOfWall;
        if (bottomOfWall >= PROJECTIONPLANEHEIGHT) {
            bottomOfWall = PROJECTIONPLANEHEIGHT - 1;
        }
        fOffscreenGraphics.fillRect(castColumn, topOfWall, 5, projectedWallHeight);
        castArc += 5;
        if (castArc >= ANGLE360) {
            castArc -= ANGLE360;
        }
    }
    paint(getGraphics());
}

public void paint(Graphics g) {

    if (fOffscreenImage!=null) g.drawImage(fOffscreenImage, 0, 0, this);
}

  public boolean keyDown(Event evt, int key)
  {
    switch (key)
    {
      case Event.ESCAPE:
        System.exit(0);
      case Event.UP:
        fKeyUp=true;
        break;
      case Event.DOWN:
        fKeyDown=true;
        break;
      case Event.LEFT:
        fKeyLeft=true;
        break;
      case Event.RIGHT:
        fKeyRight=true;
        break;
      default:
    }
    return true;
  }

  public boolean keyUp(Event evt, int key)
  {
    switch (key)
    {
      case Event.UP:
          fKeyUp=false;
          break;
      case Event.DOWN:
          fKeyDown=false;
          break;
      case Event.LEFT:
         fKeyLeft=false;
         break;
      case Event.RIGHT:
         fKeyRight=false;
         break;
      default:
    }
    return true;
  }
}
4

1 回答 1

3

我没有仔细阅读所有代码,但是您发布的课程中有一些基本缺陷:

  • 创建 的新实例b与创建空的JPanel. 当然,您什么也看不到,因为面板是空的。首先填充面板,然后添加它。
  • 单独Thread填充b面板违反了 Swing 线程规则。您应该只访问/修改/...事件调度线程上的 Swing 组件。有关更多信息,请参阅Swing 并发指南
  • 在 EDT 上有一个Thread.sleep会阻止你的 UI。永远不要那样做。如果您想要某种动画,请改用javax.swing.Timer该类。此类旨在定期更新您的 UI。
  • 不要重写paint方法,而是重写paintComponent方法,不要忘记调用super
于 2012-09-30T12:25:52.057 回答