我正在嘗試創建一個節點大小取決于框架的高度/寬度大小的 2D 網格,但是每當我運行程式時,底行和右列都會被切斷。這是我的執行代碼:
public class PathfindingVisualizer {
public static void main(String[] args) {
Grid grid = new Grid(10,10, new Dimension(1000, 1000));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridFrame(grid);
}
});
}
}
這是網格類:
package gui;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
public class Grid extends JPanel {
private final int rows, cols;
private final Node[][] grid;
private Node start, end;
private final Dimension size;
private int nodeSize;
public Grid(int rows, int cols, Dimension size) {
this.rows = rows;
this.cols = cols;
this.grid = new Node[cols][rows];
this.size = size;
this.nodeSize = getWidth() / cols;
initializeGrid();
}
public void initializeGrid() {
// populate grid as a 2D array of Nodes
for (int col=0; col<cols; col ) {
for (int row=0; row<rows; row ) {
grid[col][row] = new Node(col, row);
}
}
// create neighbors for all the Nodes
for (int col=0; col<cols; col ) {
for (int row=0; row<rows; row ) {
setNeighbors(col, row);
}
}
// create start and end points for pathfinding
start = grid[0][0];
end = grid[cols-1][rows-1];
}
private void setNeighbors(int col, int row) {
int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (int[] direction : directions) {
int neighborCol = direction[0] col;
int neighborRow = direction[1] row;
if (neighborCol >= 0 && neighborCol < cols &&
neighborRow >= 0 && neighborRow < rows) {
if (Arrays.equals(direction, new int[]{1, 0})) {
grid[col][row].setDown(grid[neighborCol][neighborRow]);
}
if (Arrays.equals(direction, new int[]{-1, 0})) {
grid[col][row].setUp(grid[neighborCol][neighborRow]);
}
if (Arrays.equals(direction, new int[]{0, 1})) {
grid[col][row].setRight(grid[neighborCol][neighborRow]);
}
if (Arrays.equals(direction, new int[]{0, -1})) {
grid[col][row].setLeft(grid[neighborCol][neighborRow]);
}
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (int col=0; col<cols; col ) {
for (int row=0; row<rows; row ) {
g.drawRect(col*nodeSize, row*nodeSize, nodeSize, nodeSize);
}
}
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public Node[][] getGrid() {
return grid;
}
@Override
public Dimension getSize() {
return size;
}
public int getHeight(){
return (int) size.getHeight();
}
public int getWidth(){
return (int) size.getWidth();
}
}
這是 GridFrame 類:
package gui;
import javax.swing.*;
import java.awt.*;
public class GridFrame {
public GridFrame(Grid grid) {
JFrame frame = new JFrame("Pathfinding Visualizer");
frame.setPreferredSize(grid.getSize());
frame.setContentPane(grid);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我也一直在搞亂在 Node 類中具有繪圖功能的實作,但到目前為止還沒有任何運氣。
這里的目標是制作一個路徑可視化器。這是我的第一個 Java 專案,所以任何幫助都會有所幫助!
編輯:這里也是 Node 類:
package gui;
public class Node {
private final int row, col;
private Node up, down, left, right;
private boolean start, end, wall, visited;
public Node(int col, int row) {
this.col = col;
this.row = row;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public Node getUp() {
return up;
}
public void setUp(Node up) {
this.up = up;
}
public Node getDown() {
return down;
}
public void setDown(Node down) {
this.down = down;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public boolean isEnd() {
return end;
}
public void setEnd(boolean end) {
this.end = end;
}
public boolean isWall() {
return wall;
}
public void setWall(boolean wall) {
this.wall = wall;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
}
uj5u.com熱心網友回復:
讓我們從...
Grid grid = new Grid(10,10, new Dimension(1000, 1000));
public Grid(int rows, int cols, Dimension size) {
this.rows = rows;
this.cols = cols;
this.grid = new Node[cols][rows];
this.size = size;
this.nodeSize = getWidth() / cols;
initializeGrid();
}
像這樣將傳遞給Dimension建構式是一個壞主意,除了您完全忽略高度這一事實之外,您最終可能會得到一個整數截斷(getWidth() / cols將簡單地截斷十進制值并且不會應用任何型別的舍入,這可能會給你錯誤的結果)
接下來你做...
this.nodeSize = getWidth() / cols;
在正常情況下,getWidth此時會回傳0,除非你這樣做......
@Override
public Dimension getSize() {
return size;
}
public int getHeight() {
return (int) size.getHeight();
}
public int getWidth() {
return (int) size.getWidth();
}
這是非常值得懷疑的(根本不推薦)。還記得我談到“整數截斷”嗎?這可能意味著組件的大小大于/小于整個網格的實際區域。getSize也不建議覆寫,因為您正在弄亂布局管理系統,這可能會產生無休止的長期問題。
最后,這是您問題的關鍵,您確實...
frame.setPreferredSize(grid.getSize());
您似乎沒有認識到的是,可視內容大小是frame size- window decorations,這使得您的程式的可視區域 <1000x1000
那么,答案是什么?而是傳遞單個單元格的大小...
private int cellSize;
public Grid(int rows, int cols, int cellSize) {
this.rows = rows;
this.cols = cols;
this.grid = new String[cols][rows];
this.cellSize = cellSize;
initializeGrid();
}
并覆寫getPreferredSize(而不是getSize)...
@Override
public Dimension getPreferredSize() {
return new Dimension(cellSize * cols, cellSize * rows);
}
顯然paintComponent
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (int col = 0; col < cols; col ) {
for (int row = 0; row < rows; row ) {
g.drawRect(col * cellSize, row * cellSize, cellSize, cellSize);
}
}
}
并擺脫
frame.setPreferredSize(grid.getSize());
可運行的示例...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new Grid(10, 10, 100));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Grid extends JPanel {
private final int rows, cols;
private final String[][] grid;
private int cellSize;
public Grid(int rows, int cols, int cellSize) {
this.rows = rows;
this.cols = cols;
this.grid = new String[cols][rows];
this.cellSize = cellSize;
initializeGrid();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(cellSize * cols, cellSize * rows);
}
public void initializeGrid() {
// populate grid as a 2D array of Nodes
for (int col = 0; col < cols; col ) {
for (int row = 0; row < rows; row ) {
grid[col][row] = col "x" row;
}
}
// create neighbors for all the Nodes
for (int col = 0; col < cols; col ) {
for (int row = 0; row < rows; row ) {
setNeighbors(col, row);
}
}
// create start and end points for pathfinding
//start = grid[0][0];
//end = grid[cols - 1][rows - 1];
}
private void setNeighbors(int col, int row) {
//int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
//
//for (int[] direction : directions) {
// int neighborCol = direction[0] col;
// int neighborRow = direction[1] row;
//
// if (neighborCol >= 0 && neighborCol < cols
// && neighborRow >= 0 && neighborRow < rows) {
// if (Arrays.equals(direction, new int[]{1, 0})) {
// grid[col][row].setDown(grid[neighborCol][neighborRow]);
// }
// if (Arrays.equals(direction, new int[]{-1, 0})) {
// grid[col][row].setUp(grid[neighborCol][neighborRow]);
// }
// if (Arrays.equals(direction, new int[]{0, 1})) {
// grid[col][row].setRight(grid[neighborCol][neighborRow]);
// }
// if (Arrays.equals(direction, new int[]{0, -1})) {
// grid[col][row].setLeft(grid[neighborCol][neighborRow]);
// }
// }
//}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (int col = 0; col < cols; col ) {
for (int row = 0; row < rows; row ) {
g.drawRect(col * cellSize, row * cellSize, cellSize, cellSize);
}
}
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
//public Node[][] getGrid() {
// return grid;
//}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473171.html
