2012-09-30 4 views
3

다운로드 한 애플릿에서 게임을 만들려고합니다. 나는 오류가 없을 때까지 모든 것을 바꿨지 만, 나는 그것을 실행할 때 빈 프레임을 보여 주며 나는 틀린 것을 모른다.JFrame이 표시되지 않습니다.

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

하나를 호출하는 것을 잊지 마세요. – trashgod

답변

3

내가 모든 코드를 통과하지 못했지만, 당신이 게시 클래스의 기본적인 결함이 있습니다 :

  • b의 새 인스턴스를 만드는 것은 비어있는를 만드는 것과 같습니다.. 물론 패널이 비어 있어도 아무 것도 볼 수 없습니다. 먼저 패널을 채우고 추가하십시오.
  • b 패널을 채우는 별도의 스윙 스레딩 규칙을 위반하는 것입니다. 이벤트 발송 스레드의 구성 요소를 스윙하는 것만 수정/수정해야합니다. 자세한 내용은 Swing concurrency guide을 참조하십시오.
  • EDT에 Thread.sleep이 있으면 UI가 차단됩니다. 절대로 그렇게하지 마십시오. 일종의 애니메이션을 원한다면 javax.swing.Timer 클래스를 대신 사용하십시오. 이 클래스는 주기적으로 UI를 업데이트하도록 설계되었습니다.
  • 오히려 paintComponent 메소드를 오버라이드 (override)의 paint 메소드를 오버라이드 (override)하지 말고, (http://sscce.org/) sscce]에 대한 super
+0

+1 거의 모든 중요한 스윙 규칙을 한 게시물에 넣었습니다. :) –

관련 문제