[英]Tilemap drawing not working Java
I am making a small program that just draws a tilemap on my JPanel, but when i run the program, it draws the map, but its draws it twice, and when i maximize the window everything disappears. 我正在做一个小程序,只在我的JPanel上绘制一个tilemap,但是当我运行该程序时,它绘制了地图,但是它绘制了两次,而当我最大化窗口时,一切都消失了。
here is my code: 这是我的代码:
package main;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class Board extends JPanel{
int width = 10;
int height = 10;
int size = 30;
int x;
int y;
int[][] map = map1.map;
ArrayList<Tile> tiles = new ArrayList<Tile>();
public Board(){
loadMap();
Thread t = new Thread(){
public void run(){
while(1 != 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
};
t.start();
}
public void loadMap(){
int x = 0;
int y = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int tex = map[i][j];
Tile t = new Tile(x,y,tex);
tiles.add(t);
x+= size;
}
y += size;
}
}
public void drawMap(Graphics g){
int index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Tile t = tiles.get(index);
if(t.tex == 0)
g.setColor(Color.gray);
else if(t.tex == 1)
g.setColor(Color.magenta);
g.fillRect(x,y,size,size);
index++;
x += size;
}
x = 0;
y += size;
}
}
public void paint(Graphics g){
drawMap(g);
}
public static void main(String[] args){
JFrame j = new JFrame();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
j.add(new Board());
j.setSize(600,600);
j.setLocationRelativeTo(null);
}
}
My tile class and map class are self explanatory, the Tile class just holds a couple variables x,y, and the map just has an array[][] in it, and those don't seem to be the problem. 我的tile类和map类是不言自明的,Tile类仅包含几个变量x,y,而map中仅包含array [] [],而这些似乎不是问题。
It prints the map out the way i wanted it to but then it has a clone below it and when i resize the window it everything just disappears. 它按照我想要的方式打印地图,但是下面有一个副本,当我调整窗口大小时,一切都消失了。 I don't know why it is doing this, and any help would be appreciated.
我不知道为什么要这么做,我们将不胜感激。
Thanks 谢谢
Without having run the code: 无需运行代码:
You are breaking the paint chain. 您正在打破油漆链。 But not calling
super.paint
you are not allowing the component to prepare itself properly for painting. 但是,如果不调用
super.paint
,则不允许组件为绘画进行适当的准备。 Painting is a complex series of chained method calls which work together to produce the desired output, unless you're prepared to take over this work, you must always call super.xxx
first. 绘画是一系列复杂的链式方法调用,它们一起工作以产生所需的输出,除非您准备好接手这项工作,否则必须始终先调用
super.xxx
。
It is typically recommended that you override paintComponent
instead, for a vertiy of reasons, one of which you've just discovered... 通常出于种种原因,建议您重写
paintComponent
,而您刚刚发现了其中一个...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawMap(g);
}
See Painting in AWT and Swing and Performing Custom Painting for more details about how painting in Swing works... 有关Swing中的 绘画工作原理的更多详细信息,请参阅AWT中的绘画和Swing和执行自定义绘画 。
Your drawMap
method is relying on instance fields to draw the grid, this means that each time the method is called, the values of y
(in particular) are the same as they were when the method last exited... 您的
drawMap
方法依赖于实例字段来绘制网格,这意味着每次调用该方法时, y
的值(尤其是)都与该方法最后一次退出时的值相同...
No there's no real reason that they should be instance fields, they only have context within the drawMap
method... 不,没有真正的理由,它们应该是实例字段,它们仅在
drawMap
方法中具有上下文...
public void drawMap(Graphics g) {
int x = 0;
int y = 0;
int index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Tile t = tiles.get(index);
if (t.tex == 0) {
g.setColor(Color.gray);
} else if (t.tex == 1) {
g.setColor(Color.magenta);
}
g.fillRect(x, y, size, size);
index++;
x += size;
}
x = 0;
y += size;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.