【发布时间】:2015-06-12 12:12:51
【问题描述】:
我想为我的游戏设置背景。
场景:
首先,我必须从文本文件中读取,然后根据该文本在其上绘制我的瓦片地图和图像。其次,我的地图是 3600*2400 像素,比我的屏幕大,所以我必须滚动它。第三,我的屏幕一角必须有一张迷你地图,显示我在哪里。 (我想我应该使用面板和awt.container。)
这是我的代码:
public class bkg extends Panel implements ImageObserver {
//i Initialize my variables here
// then read my images with image buffer in constructor
public static void main(String[] args) {
//my three panels and frame settings
Frame frame = new Frame();
frame.setLayout(null);
frame.setSize(1350, 700);
frame.setTitle("age of empires");
frame.setVisible(true);
frame.setLayout(null);
Panel p1 = new Panel();
Panel p2 = new Panel();
p2.setLayout(null);
JPanel p3 = new JPanel();
p3.setLayout(null);
p1.setSize(1350, 700); // i divide my screen in to 3 parts , one biggest panel, and two small. the biggest one is for main map
p1.setLayout(null);
p2.setBackground(Color.cyan);//just wanna test showing my panel.
p2.setSize(675, 350);
p3.setSize(675, 350);
p1.setLocation(0, 0);
p2.setLocation(0, 350);// this small panel must be under the main pannel.
p3.setLocation(675, 350);// this is with p2.
frame.add(p1);
p1.add(p2);
p2.add(p3);
tilemap = new int[60][75];// it creat my tilemap in console.
filereader();// it reads the text file.
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int mod_i = 100 * i;// based on images sizes
int mod_j = 50 * j;// based on images sizes
//now i start to draw images base on the text file numbers
switch (tilemap[i][j]) {
case 1:
g.drawImage(tree, mod_i, mod_j, null);
break;
.........
}
}
}
}
问题:好像我的代码连paint方法都看不到。它不会在我的面板上绘制任何图像。有什么问题?
【问题讨论】:
-
如果paint负责绘制,那为什么不在main方法中调用呢?
-
我做到了,但它要我争论,然后要我把油漆变成静态的!然后给我关于静态油漆的错误:|在应用中不需要调用paint方法,执行时会自动搜索paint方法