2013-09-22 20 views
1

謝謝你試圖幫助我。請幫助:當使用GridLayout(20,20)時,JPanel中的JButtons網格顯示一個JButton而不是400.

我想創建一個20 * 20平方的網格。它用於我的A-Star算法演示。 對於實驗目的,我寫了下面的代碼:

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
class Node extends JButton { 
Node(){ 
    super(); 
    setSize(new Dimension(20,20)); 
} 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    g.setColor(Color.white); 
    g.fillRect(0,0,getHeight(),getWidth()); 
} 
} 
public class Map 
{ 
static final int n = 20; 
JPanel p; 
public JPanel init() { 
    p=new JPanel(); 
    p.setLayout(new GridLayout(n, n)); 
    p.setBackground(Color.black); 
    p.setFont(new Font("SansSerif", Font.BOLD, 24)); 
    for(int i = 0; i < n; i++) { 
     for(int j = 0; j < n; j++) { 
       p.add(new Node()); 
     } 
    } 
    return p; 
} 
} 
public class Display extends JApplet{ 
    public void init(){ 
    Map m=new Map(); 
    add(m.init()); 

} 
}` 

我得到了以下的輸出: 因爲我缺乏信譽10

但是請加載代碼& check.I不能發佈圖片,當我我試圖添加實驗代碼與我的原代碼演示 它沒有顯示所需的輸出。我的原始代碼如下:

import java.util.ArrayList; 
import javax.swing.*; 
import java.awt.*; 
public class Node extends JButton implements Comparable<Node> { 
/* Nodes that this is connected to */ 
private Node north; 
private Node east; 
private Node south; 
private Node west; 
private ArrayList<Node> neighbourList; 
private boolean visited; 
private float g; 
private float h; 
private Node parent; 
private int x; 
private int y; 
private boolean isObstacle; 
private boolean isStart; 
private boolean isGoal; 

Node(int x, int y) { 
    super(); 
    neighbourList = new ArrayList<Node>(); 
    this.x = x; 
    this.y = y; 
    this.visited = false; 
    this.g = Integer.MAX_VALUE; 
    this.isObstacle = false; 
    this.isStart = false; 
    this.isGoal = false; 
} 

public void setNorth(Node north) { 
    //replace the old Node with the new one in the neighbourList 
    if (neighbourList.contains(this.north)) 
     neighbourList.remove(this.north); 
    neighbourList.add(north); 
    //set the new Node 
    this.north = north; 
} 

public void setEast(Node east) { 
    //replace the old Node with the new one in the neighbourList 
    if (neighbourList.contains(this.east)) 
     neighbourList.remove(this.east); 
    neighbourList.add(east); 
    //set the new Node 
    this.east = east; 
} 

public void setSouth(Node south) { 
    //replace the old Node with the new one in the neighbourList 
    if (neighbourList.contains(this.south)) 
     neighbourList.remove(this.south); 
    neighbourList.add(south); 
    //set the new Node 
    this.south = south; 
} 

public void setWest(Node west) { 
    //replace the old Node with the new one in the neighbourList 
    if (neighbourList.contains(this.west)) 
     neighbourList.remove(this.west); 
    neighbourList.add(west); 
    //set the new Node 
    this.west = west; 
} 

public ArrayList<Node> getneighbourList() { 
    return neighbourList; 
} 

public boolean isVisited() { 
    return visited; 
} 

public void setVisited(boolean visited) { 
    this.visited = visited; 
} 

public float getG() { 
    return g; 
} 

public void setG(float f) { 
    this.g = f; 
} 

public Node getParent() { 
    return parent; 
} 

public void setParent(Node parent) { 
    this.parent = parent; 
} 

public float getH() { 
    return h; 
} 

public void setH(float h) { 
    this.h = h; 
} 

public int getX() { 
    return x; 
} 

public int getY() { 
    return y; 
} 

public boolean isObstacle() { 
    return isObstacle; 
} 

public void setObstacle(boolean isObstacle) { 
    this.isObstacle = isObstacle; 
} 

public boolean isStart() { 
    return isStart; 
} 

public void setStart(boolean isStart) { 
    this.isStart = isStart; 
} 

public boolean isGoal() { 
    return isGoal; 
} 

public void setGoal(boolean isGoal) { 
    this.isGoal = isGoal; 
} 

public boolean equals(Node node) { 
    return (node.x == x) && (node.y == y); 
} 

public int compareTo(Node otherNode) { 
    float thisTotalDistanceFromGoal = h + g; 
    float otherTotalDistanceFromGoal = otherNode.getH() + otherNode.getG(); 
    if (thisTotalDistanceFromGoal < otherTotalDistanceFromGoal) 
     return -1; 
    else if (thisTotalDistanceFromGoal > otherTotalDistanceFromGoal) 
     return 1; 
    else 
     return 0; 
} 

protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    g.setColor(Color.white); 
    g.fillRect(0,0,getHeight()-1,getWidth()-1); 
} 
} 

public class Map { 
private int mapWidth; 
private int mapHeight; 
private ArrayList<ArrayList<Node>> map; 
private int startLocationX = 0; 
private int startLocationY = 0; 
private int goalLocationX = 0; 
private int goalLocationY = 0; 
private int[][] obstacleMap; 
private JPanel p; 
private Logger log = new Logger(); 
Image buffer; 

Map(int mapWidth, int mapHeight, int[][] obstacleMap) { 
    this.mapWidth = mapWidth; 
    this.mapHeight = mapHeight; 
    this.obstacleMap = obstacleMap; 
    createMap(); 
    log.addToLog("\tMap Created"); 
    registerEdges(); 
    log.addToLog("\tMap Node edges registered"); 
} 

private void createMap() { 
    Node node; 
    map = new ArrayList<ArrayList<Node>>(); 
    for (int x=0; x<mapWidth; x++) { 
     map.add(new ArrayList<Node>()); 
     for (int y=0; y<mapHeight; y++) { 
      node = new Node(x,y); 
      if (obstacleMap[x][y] == 1) 
       node.setObstacle(true); 
      map.get(x).add(node); 
     } 
    } 
} 

/** 
* Registers the nodes edges (connections to its neighbours). 
*/ 
private void registerEdges() { 
    for (int x = 0; x < mapWidth-1; x++) { 
     for (int y = 0; y < mapHeight-1; y++) { 
      Node node = map.get(x).get(y); 
      if (!node.isObstacle()){ 
       if (!(y==0)) 
        node.setNorth(map.get(x).get(y-1)); 
       if (!(x==mapWidth)) 
        node.setEast(map.get(x+1).get(y)); 
       if (!(y==mapHeight)) 
        node.setSouth(map.get(x).get(y+1)); 
       if (!(x==0)) 
        node.setWest(map.get(x-1).get(y)); 
      } 
     } 
    } 
} 

public ArrayList<ArrayList<Node>> getNodes() { 
    return map; 
} 

public void setObstacle(int x, int y, boolean isObstacle) { 
    map.get(x).get(y).setObstacle(isObstacle); 
} 

public Node getNode(int x, int y) { 
    return map.get(x).get(y); 
} 

public void setStartLocation(Node start) { 
    map.get(startLocationX).get(startLocationY).setStart(false); 
    map.get(start.getX()).get(start.getY()).setStart(true); 
    startLocationX = start.getX(); 
    startLocationY = start.getY(); 
} 

public void setStartLocation(int x, int y) { 
    map.get(startLocationX).get(startLocationY).setStart(false); 
    map.get(x).get(y).setStart(true); 
    startLocationX = x; 
    startLocationY = y; 
} 

public void setGoalLocation(Node goal) { 
    map.get(goalLocationX).get(goalLocationY).setGoal(false); 
    map.get(goal.getX()).get(goal.getY()).setGoal(true); 
    goalLocationX = goal.getX(); 
    goalLocationY = goal.getY(); 
} 

public void setGoalLocation(int x, int y) { 
    map.get(goalLocationX).get(goalLocationY).setGoal(false); 
    map.get(x).get(y).setGoal(true); 
    goalLocationX = x; 
    goalLocationY = y; 
} 

public int getStartLocationX() { 
    return startLocationX; 
} 

public int getStartLocationY() { 
    return startLocationY; 
} 

public Node getStartNode() { 
    return map.get(startLocationX).get(startLocationY); 
} 

public int getGoalLocationX() { 
    return goalLocationX; 
} 

public int getGoalLocationY() { 
    return goalLocationY; 
} 

public Node getGoalLocation() { 
    return map.get(goalLocationX).get(goalLocationY); 
} 

public float getDistanceBetween(Node node1, Node node2) { 
    //if the nodes are on top or next to each other, return 1 
    if (node1.getX() == node2.getX() || node1.getY() == node2.getY()){ 
     return 1; 
    } else { //if they are diagonal to each other return diagonal distance: sqrt(1^2+1^2) 
     return (float) 1.4; 
    } 
} 

public int getMapWidth() { 
    return mapWidth; 
} 

public int getMapHeight() { 
    return mapHeight; 
} 

public JPanel init(){ 
    int n=20; 
    p=new JPanel(); 
    p.setLayout(new GridLayout(n, n)); 
    p.setSize(400,400); 
    p.setFont(new Font("SansSerif", Font.BOLD, 24)); 
    for(int i = 0; i < n; i++) { 
     for(int j = 0; j < n; j++) { 
       p.add(map.get(j).get(i)); 
     } 
    } 
    return p; 
} 

public void clear() { 
    startLocationX = 0; 
    startLocationY = 0; 
    goalLocationX = 0; 
    goalLocationY = 0; 
    createMap(); 
    registerEdges(); 
} 

} 

public class Display extends JApplet 
{ 
private static int[][] M = {{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0}, 
     {0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0}, 
     {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0}, 
     {0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0}, 
     {0,1,1,1,1,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0}, 
     {0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0}, 
     {1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0}, 
     {0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0}, 
     {0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; 

public void init() 
{ 
    JRootPane rootPane = this.getRootPane();  
    rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); 
    Map m=new Map(20,20,M); 
    add(m.init()); 
} 
} 

這是我從上面的代碼得到的輸出: 因爲我缺乏信譽10

+1

'擴展JApplet'爲什麼(哦,爲什麼)一個小程序?開發和調試應用程序要容易得多,即使我正在開發一個applet,我也會先在應用程序中使用它。如果這是由於規格。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 –

+1

我花了幾分鐘試圖讓第二個代碼編譯,然後我停了下來。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

1

Node類請加載代碼& check.I不能發佈圖片,方法getParentgetXgetY是超類JButton中的重寫方法。佈局管理器調用getXgetY時,會調用您的方法,導致佈局管理器混淆。將您的三種方法重命名爲其他任何方法,例如nodeGetParentnodeGetXnodeGetY

這裏要學習的一點是,在擴展一個你不會無意中重載超類的方法的類時,你必須小心。

+0

謝謝@Barzee下次我會小心翼翼地看到'javadoc' –

相關問題