我们正在尝试使用2d数组绘制一块瓷砖,但是每次运行我们的代码时,它都会通过paintcomponent 2到3(每次都不同),而我们只尝试通过它一次。
这是我们的代码
Board.java:
public class Board extends JComponent {
public Board() {
}
public static String[] gameElements = new String[100];
private String[][] map = new String[10][10];
private int positionX = 50;
private int positionY = 50;
private String currentLevel = "1";
public String[][] getMap(){
return map;
}
public void setMap(String[][] map){
this.map = map;
}
@Override
public void paintComponent(Graphics g) {
loadLevel();
int i = 0;
int positionX = 50;
int positionY = 50;
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map.length; x++) {
new Tile(x, y).paintComponent(g);
map[y][x] = gameElements[i];
g.drawString(gameElements[i], positionY, positionX);
positionY += 50;
System.out.print("[" + map[y][x] + "]");
i++;
}
positionY = 50;
positionX += 50;
System.out.println();
}
}
public void readTextFile(String fileName) {
try {
FileReader fileReader = new FileReader(fileName + ".txt");
BufferedReader buffer = new BufferedReader(fileReader);
String splitBy = ",";
String line = buffer.readLine();
for (int i = 0; i < gameElements.length; i++) {
gameElements = line.split(splitBy);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void loadLevel() {
readTextFile(currentLevel);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("SleutelBarricade");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent chart = new Board();
frame.add(chart);
frame.setVisible(true);
}
}
Tile.java:
public class Tile extends Board {
public Tile() {
}
final private static int CELL_WIDTH = 50;
final private static int CELL_HEIGHT = 50;
final private static int BOARD_X_OFFSET = 25;
final private static int BOARD_Y_OFFSET = 25;
private int x;
private int y;
private int getScreenX(int x, int y) {
return BOARD_X_OFFSET + x * CELL_WIDTH;
}
private int getScreenY(int x, int y) {
return BOARD_Y_OFFSET + y * CELL_HEIGHT;
}
public Tile(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(
getScreenX(x, y),
getScreenY(x, y),
CELL_WIDTH,
CELL_HEIGHT);
}
}
结果:
"C:\Program Files\Java\jdk1.8.0_77\bin\java" -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_77\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_77\jre\lib\rt.jar;C:\Users\Darryl Soerdjpal\Desktop\TestBoard\out\production\TestBoard;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main.GameBoard.Board
[E][W][B][E][E][E][E][B][B][B]
[E][E][E][E][K][K][E][B][B][B]
[E][E][B][E][E][E][E][B][B][K]
[E][W][B][E][E][E][E][B][B][B]
[E][W][B][B][W][W][W][B][B][B]
[E][B][B][E][E][E][W][B][B][E]
[E][W][B][W][W][B][W][W][E][E]
[E][W][B][B][B][B][E][E][E][E]
[K][W][B][B][E][E][E][W][E][E]
[E][W][B][B][E][E][E][W][E][F]
[E][W][B][E][E][E][E][B][B][B]
[E][E][E][E][K][K][E][B][B][B]
[E][E][B][E][E][E][E][B][B][K]
[E][W][B][E][E][E][E][B][B][B]
[E][W][B][B][W][W][W][B][B][B]
[E][B][B][E][E][E][W][B][B][E]
[E][W][B][W][W][B][W][W][E][E]
[E][W][B][B][B][B][E][E][E][E]
[K][W][B][B][E][E][E][W][E][E]
[E][W][B][B][E][E][E][W][E][F]
Process finished with exit code 0
答案 0 :(得分:1)
您错误地使用面向对象编程。 Tile
不应扩展Board
。其次,Tile
应该处理绘图本身。
此外,不要每次复飞都要继续创建新的Tile
个对象(每次调用repaint()
时)。读取文件后,创建一次。现在可以删除gameElements
,因为您可以在文件读取期间创建Tile
数组。
注意:下面,我通过InputStream
(Eclipse IDE)将文件加载到Scanner中:
ClassLoader loader = Board.class.getClassLoader();
String filename = "resources/" + level + ".txt";
InputStream stream = loader.getResourceAsStream(filename);
Scanner scan = new Scanner(stream);
相反,您可以将File
直接加载到Scanner
构造函数中:
String filename = "path/to/file/" + level + ".txt";
File file = new File(filename);
Scanner scan = new Scanner(file);
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
JComponent chart = new Board("1");
frame.setSize(600, 600);
frame.setTitle("SleutelBarricade");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(chart);
frame.setVisible(true);
}
});
}
}
import java.awt.Graphics;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
public class Board extends JComponent {
private static final long serialVersionUID = 3669634976765884468L;
public String[] gameElements = new String[100];
private Tile[] tiles = new Tile[100];
private String[][] map = new String[10][10];
private String currentLevel;
private boolean isInitialized;
public Board(String level) {
isInitialized = false;
loadLevel(level);
currentLevel = level;
isInitialized = true;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (isInitialized) {
int i = 0;
g.clearRect(0, 0, this.getWidth(), this.getHeight());
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map.length; x++) {
tiles[i].paintComponent(g);
map[y][x] = tiles[i].getLabel();
System.out.printf("[%s]", map[y][x]);
i++;
}
System.out.println();
}
}
}
public void readTextFile(String level) {
ClassLoader loader = Board.class.getClassLoader();
String filename = "resources/" + level + ".txt";
InputStream stream = null;
Scanner scan = null;
try {
stream = loader.getResourceAsStream(filename);
scan = new Scanner(stream);
scan.useDelimiter("[,]([\r\n]+)?");
int i = 0;
while (scan.hasNext()) {
gameElements[i] = scan.next().trim();
i++;
}
scan.close();
stream.close();
} catch (IOException e) {
Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, e);
}
}
private void generateTiles() {
int i = 0;
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map.length; x++) {
tiles[i] = new Tile(x, y, gameElements[i]);
i++;
}
}
}
public void loadLevel(String level) {
readTextFile(level);
generateTiles();
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
public class Tile {
final private static int CELL_WIDTH = 50;
final private static int CELL_HEIGHT = 50;
final private static int BOARD_X_OFFSET = 25;
final private static int BOARD_Y_OFFSET = 25;
private int x;
private int y;
private String label;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public Tile() {
this(0, 0, "?");
}
public Tile(int x, int y, String label) {
this.x = getScreenX(x);
this.y = getScreenY(y);
this.label = label;
}
private int getScreenX(int x) {
return BOARD_X_OFFSET + (x * CELL_WIDTH);
}
private int getScreenY(int y) {
return BOARD_Y_OFFSET + (y * CELL_HEIGHT);
}
public void paintComponent(Graphics g) {
Font font = new Font("Arial", Font.BOLD, 16);
FontMetrics metrics = g.getFontMetrics(font);
int fontWidth = metrics.stringWidth(label);
int fontHeight = metrics.getHeight();
int xPos = x + BOARD_X_OFFSET - (fontWidth / 2);
int yPos = y + BOARD_Y_OFFSET + (fontHeight / 2);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawRect(x, y, CELL_WIDTH, CELL_HEIGHT);
g.drawString(label, xPos, yPos);
}
}
E,W,B,E,E,E,E,B,B,B,
E,E,E,E,K,K,E,B,B,B,
E,E,B,E,E,E,E,B,B,K,
E,W,B,E,E,E,E,B,B,B,
E,W,B,B,W,W,W,B,B,B,
E,B,B,E,E,E,W,B,B,E,
E,W,B,W,W,B,W,W,E,E,
E,W,B,B,B,B,E,E,E,E,
K,W,B,B,E,E,E,W,E,E,
E,W,B,B,E,E,E,W,E,F
答案 1 :(得分:0)
我这样做了,它现在运行了一次,但它不会绘制瓷砖或绘制字符串了。
git checkout -