谢谢您的帮助。

我想创建一个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点信誉,我无法发布图片

但是,当我尝试将实验代码添加到演示的原始代码中时
它没有显示所需的输出。我的原始代码如下:

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点信誉,我无法发布图片

最佳答案

Node类中,方法getParentgetXgetY是超类JButton中的重写方法。当布局管理器调用getXgetY时,将调用您的方法,这会使布局管理器感到困惑。将三种方法重命名为其他任何名称,例如nodeGetParentnodeGetXnodeGetY

在这里要学习的教训是,在扩展不会意外覆盖超类的方法的类时,必须小心。

08-03 18:03