-
카테고리
-
세부 분야
게임 프로그래밍
-
해결 여부
해결됨
Mac환경등에서 콘솔이 제대로 출력되지 않는 분들은 이 java 코드를 활용해보세요.
21.01.12 18:08 작성 조회수 361
0
제가 직접 java GUI 로 포팅한 코드입니다.
콘솔의 환경설정 문제 때문에 콘솔이 이상하게 나온다면 이번 강의와 이어지는 알고리즘 강의를 들을 때 이 코드를 활용해보세요.
다음 강의인 유니티 강의부터는 필요없어지겠지만 이번 강의는 들을 수 있을 것입니다. 혹시라도 저와 같은 문제를 겪게될 사람들을 위해 공유합니다.
이클립스 IDE나 JDK를 깔면 어떤 환경에서도 실행이 될 것입니다.
(java문법은 C#과 크게 다르지 않습니다. 틀만 그대로 활용하시면 됩니다. )
//<Program.java>
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Program extends JFrame {
private static final long serialVersionUID = 1L;
private final int BORDER_SIZE = 26;
private static Board board;
private static Player player;
public Program() {
initUI();
}
public static void main(String[] args) throws InterruptedException {
Program ex = new Program();
ex.setVisible(true);
final int FPS = 30; // 30프레임
final int WAIT_TICK = 1000 / FPS;
long lastTick = System.currentTimeMillis();
long currentTick = 0;
while (true) {
//// # 프레임 관리
currentTick = System.currentTimeMillis();
if (currentTick - lastTick < WAIT_TICK) {
Thread.sleep(WAIT_TICK - (currentTick - lastTick));
}
int deltaTick = (int) (System.currentTimeMillis() - lastTick);
lastTick = System.currentTimeMillis();
//// # 프레임 관리
// 입력
// 로직
// 렌더링
board.repaint();
}
}
private void initUI() {
JPanel content = new JPanel(new GridBagLayout());
content.setBackground(Color.BLACK);
content.setBorder(new EmptyBorder(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE));
board = new Board();
board.setBackground(Color.BLACK);
player = new Player();
board.Initialize(25);
player.Initialize(1, 1, board._size-2, board._size-2, board);
content.add(board);
add(content, BorderLayout.CENTER);
setResizable(false);
pack();
setTitle("미로 길찾기");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//<Board.java>
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Board extends JPanel {
private static final long serialVersionUID = 1L;
private final int CELL_SIZE = 20; // 픽셀 크기
public TileType[][] _tile; // 배열
public int _size;
public enum TileType {
Empty, Wall,
}
public void Initialize(int size) {
_size = size;
setPreferredSize(new Dimension(_size * CELL_SIZE, _size * CELL_SIZE));
_tile = new TileType[_size][_size];
// 원하는 보드를 생성한다.
for (int y = 0; y < _size; y++) {
for (int x = 0; x < _size; x++) {
if (x == 0 || x == _size - 1 || y == 0 || y == _size - 1) // 가장자리
_tile[y][x] = TileType.Wall;
else
_tile[y][x] = TileType.Empty;
}
}
}
@Override
public void paintComponent(Graphics g) {
for (int y = 0; y < _size; y++) {
for (int x = 0; x < _size; x++) {
g.setColor(GetTileColor(_tile[y][x]));
g.fillOval(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
Color GetTileColor(TileType type) {
switch (type) {
case Empty:
return Color.GREEN;
case Wall:
return Color.RED;
default:
return Color.GREEN;
}
}
}
//<Player.java>
import java.util.ArrayList;
import java.util.List;
class Pos {
public int Y;
public int X;
public Pos(int y, int x) {
Y = y;
X = x;
}
}
public class Player {
public int PosY;
public int PosX;
public int getPosY() {
return PosY;
}
private void setPosY(int posY) {
PosY = posY;
}
public int getPosX() {
return PosX;
}
private void setPosX(int posX) {
PosX = posX;
}
Board _board;
enum Dir {
Up, Left, Down, Right,
}
int _dir = Dir.Up.ordinal();
List<Pos> _points = new ArrayList<Pos>();
public void Initialize(int posY, int posX, int destY, int destX, Board board) {
PosY = posY;
PosX = posX;
_board = board;
}
}
답변을 작성해보세요.
0
답변 1