user1709713
user1709713

Reputation: 33

JFrame not displaying

I am trying to make a game out of a downloaded applet. I changed everything until I got no errors, but when I run it, it show an empty frame and I have no idea whats wrong!

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;
  }
}

Upvotes: 3

Views: 545

Answers (1)

Robin
Robin

Reputation: 36621

I did not go through all of the code, but there are some basic flaws in the classes you posted:

  • Creating a new instance of b is the same as creating an empty JPanel. Of course you see nothing as the panel is empty. Populate the panel first and then add it.
  • Having a separate Thread populating the b panel is a violation of the Swing threading rules. You should only access/modify/... Swing components on the Event Dispatch Thread. Consult the Swing concurrency guide for more information
  • Having a Thread.sleep on the EDT will block your UI. Never ever do that. If you want to have some sort of animation, use the javax.swing.Timer class instead. This class is designed to update your UI periodically.
  • Do not override the paint method, rather override the paintComponent method, and do not forget to call super

Upvotes: 3

Related Questions