我正在尝试将多个JPanel添加到JFrame

时间:2017-10-24 05:38:24

标签: java swing jframe jpanel

我正在尝试打印迷宫。我是Java Swing图形的新手,我最初的想法是将每个单元格的JFrame个组件添加到CurrPos。运行此代码时,我得到垃圾结果。

有什么办法可以最小化这段代码,以便打印所有面板?请注意,WallUnvisitedVisitedJFramepainComponent的子类,它们实现import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class DrawMaze { public final int WIDTH = 500; public final int HEIGHT = 500; JFrame frame; public void initializeMaze(){ frame = new JFrame("Maze"); frame.getContentPane().setLayout(null); frame.setSize(WIDTH, HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void drawMaze(String[][] maze, int[] currPosArray) { //frame.removeAll(); addMazeContentsToList(maze, currPosArray); frame.setVisible(true); } private void addMazeContentsToList(String[][] maze, int[] currPosArray) { for (int i = 0; i<7; i++){ for (int j = 0; j<9; j++){ if ((i%2 == 1 && j%2 == 0) ||(i%2 == 0 && j%2 == 1) ){ Wall wall = drawWall(i, j); frame.add(wall); wall.repaint(); } else if (i%2 ==0 && j%2 ==0){ if (i==currPosArray[0] && j==currPosArray[1]){ CurrPos currPos = drawCurrPos(i/2, j/2); frame.add(currPos); currPos.repaint(); } else{ if (maze[i][j].equals("Unexplored")){ Unvisited unvisited = drawUnvisited(i/2,j/2); frame.add(unvisited); unvisited.repaint(); } else{ Visited visited = drawVisited(i/2,j/2); frame.add(visited); visited.repaint(); } } } } } } private Visited drawVisited(int i, int j) { int x = (int) Math.floor((i/4.0)*WIDTH); int y = (int) Math.floor((j/5.0)*HEIGHT); int width = (int) Math.floor(WIDTH/4.0); int height = (int) Math.floor(HEIGHT/5.0); return new Visited(x, y, width, height); } private Unvisited drawUnvisited(int i, int j) { int x = (int) Math.floor((i/4.0)*WIDTH); int y = (int) Math.floor((j/5.0)*HEIGHT); int width = (int) Math.floor(WIDTH/4.0); int height = (int) Math.floor(HEIGHT/5.0); //System.out.println(x); //System.out.println(y); System.out.println(width); System.out.println(height); return new Unvisited(x, y, width, height); } private CurrPos drawCurrPos(int i, int j) { int x = (int) Math.floor((i/4.0)*WIDTH); int y = (int) Math.floor((j/5.0)*HEIGHT); int width = (int) Math.floor(WIDTH/4.0); int height = (int) Math.floor(HEIGHT/5.0); return new CurrPos(x, y, width, height); } private Wall drawWall(int i, int j) { //vertical wall if (i%2 ==1){ int relativeX = (i+1)/2; int relativeY = j/2; int width = (int) Math.floor((1/4.0)*WIDTH*(0.2)); int height = (int) Math.floor((1/5.0)*HEIGHT); int x = (int) Math.floor(((relativeX/4.0)*WIDTH - width*0.5)); int y = (int) (Math.floor((relativeY/5.0)*HEIGHT)); return new Wall(x, y, width, height); } //horizontal wall else{ int relativeX = i/2; int relativeY = (j+2)/2; int width = (int) Math.floor((1/4.0)*WIDTH); int height = (int) Math.floor((1/5.0)*HEIGHT*(0.2)); int x = (int) (Math.floor((relativeY/4.0)*WIDTH)); int y = (int) Math.floor(((relativeX/5.0)*HEIGHT - height*0.5)); return new Wall(x, y, width, height); } } } 方法。

以下是代码:

public static function tzList()
{
    $tzMap = array();
    $zones = \DateTimeZone::listIdentifiers();
    foreach ($zones as $zone) {
        $tz = new \DateTimeZone($zone);
        $now = new \DateTime("utc", $tz);
        $diffInSeconds = $tz->getOffset($now);
        $hours = floor($diffInSeconds / 3600);
        $minutes = floor(($diffInSeconds % 3600) / 60);
        $tzMap[$zone] = sprintf("%+d", $hours) . ":" . sprintf("%02d", $minutes);
    }
    return $tzMap;
}

0 个答案:

没有答案