0
我正在做一個遊戲,我想有一個彈出窗口的選項,一旦採取了某些行動。我不知道在哪裏把彈出式jpanel的實例放在我的代碼中。我不希望它在主遊戲板的主JPanel的構造函數中,我試圖在主方法中添加它,但似乎沒有工作。建議嗎?JPanel和彈出窗口的,在哪裏插入彈出的實例
public class GameBoard extends JPanel {
public final int EARTH_ORBIT_RADIUS = 200;
public final int BOARD_WIDTH = 800;
public final int BOARD_HEIGHT = 600;
public final int INITIAL_EARTH_X = 700;
public final int INITIAL_EARTH_Y = 300;
public final int INITIAL_DELAY = 100;
public final int PERIOD_DELAY = 30;
public final static int SUN_COORDINATE_X = 500;
public final static int SUN_COORDINATE_Y = 300;
public final int INITIAL_ASTEROID_X = 500;
public final int INITIAL_ASTEROID_Y = 100;
public final int INITIAL_SHIP_X = 500;
public final int INITIAL_SHIP_Y = 500;
public static int earth_x, earth_y;
public static int asteroid_x, asteroid_y;
public static int ship_x, ship_y;
public int t = 0;
public int ship_t = 0;
public int keyheard = 0;
public int[] earthcoordinates;
public int[] asteroidcoordinates;
public int[] shipcoordinates;
public static boolean isCollision = false;
Image earth;
Image sun;
Image asteroid;
Image background;
Image ship, shipL, shipR, shipU, shipD;
Timer timer;
GameBoard() {
initGameBoard();
loadImage();
earth_x = INITIAL_EARTH_X;
earth_y = INITIAL_EARTH_Y;
asteroid_x = INITIAL_ASTEROID_X;
asteroid_y = INITIAL_ASTEROID_Y;
ship_x = INITIAL_SHIP_X;
ship_y = INITIAL_SHIP_Y;
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduledTask(), INITIAL_DELAY,
PERIOD_DELAY);
}
void initGameBoard(){
setBackground(Color.BLACK);
setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
setDoubleBuffered(true);
addKeyListener(new KeyListen());
setFocusable(true);
}
void loadImage() {
ImageIcon earth_image_icon = new ImageIcon("earth2.png");
earth = earth_image_icon.getImage();
ImageIcon sun_image_icon = new ImageIcon("sun.png");
sun = sun_image_icon.getImage();
ImageIcon asteroid_image_icon = new ImageIcon("asteroid.png");
asteroid = asteroid_image_icon.getImage();
ImageIcon bg_image_icon = new ImageIcon("bg_pr.png");
background = bg_image_icon.getImage();
ImageIcon ship_image_icon = new ImageIcon("ship.png");
ship = ship_image_icon.getImage();
ImageIcon shipL_image_icon = new ImageIcon("shipL.png");
shipL = shipL_image_icon.getImage();
ImageIcon shipR_image_icon = new ImageIcon("shipR.png");
shipR = shipR_image_icon.getImage();
ImageIcon shipU_image_icon = new ImageIcon("shipU.png");
shipU = shipU_image_icon.getImage();
ImageIcon shipD_image_icon = new ImageIcon("shipD.png");
shipD = shipD_image_icon.getImage();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawBackground(g);
drawEarth(g);
drawSun(g);
drawAsteroid(g);
drawShip(g);
}
void drawEarth(Graphics g) {
g.drawImage(earth, earth_x, earth_y, this);
Toolkit.getDefaultToolkit().sync();
}
void drawSun(Graphics g) {
g.drawImage(sun, SUN_COORDINATE_X, SUN_COORDINATE_Y, this);
Toolkit.getDefaultToolkit().sync();
}
void drawAsteroid(Graphics g) {
g.drawImage(asteroid, asteroid_x, asteroid_y, this);
Toolkit.getDefaultToolkit().sync();
}
void drawBackground(Graphics g) {
g.drawImage(background, 0, 0, null);
}
void drawShip(Graphics g){
if(keyheard == 0 || isCollision == true){g.drawImage(ship,ship_x, ship_y, this);}
else if(keyheard == 1){g.drawImage(shipL,ship_x, ship_y, this);}
else if(keyheard == 2){g.drawImage(shipR,ship_x, ship_y, this);}
else if(keyheard == 3){g.drawImage(shipU,ship_x, ship_y, this);}
else if(keyheard == 4){g.drawImage(shipD,ship_x, ship_y, this);}
}
public class ScheduledTask extends TimerTask {
@Override
public void run() {
OrbitCalculation oe = new OrbitCalculation();
earthcoordinates = oe.earthOrbitCalculation(t);
earth_x = earthcoordinates[0];
earth_y = earthcoordinates[1];
OrbitCalculation oa = new OrbitCalculation();
asteroidcoordinates = oa.asteroidOrbitCalculation(t);
asteroid_x = asteroidcoordinates[0];
asteroid_y = asteroidcoordinates[1];
OrbitCalculation os = new OrbitCalculation();
shipcoordinates = os.shipOrbitCalculation(ship_t,keyheard, ship_x, ship_y);
ship_x = shipcoordinates[0];
ship_y = shipcoordinates[1];
CollisionDetector c = new CollisionDetector();
if(c.whoCollision() == "asteroid"){
ship_x = asteroid_x;
ship_y = asteroid_y;
isCollision = true;
}
else if(c.whoCollision() == "earth"){
ship_x = earth_x;
ship_y = earth_y;
isCollision = true;
}
else{ isCollision = false;}
//if(GameBo
t = t + 1;
ship_t = ship_t + 1;
repaint();
}
}
public class KeyListen extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
keyheard = 1;
}
if(key == KeyEvent.VK_RIGHT){
keyheard = 2;
}
if(key == KeyEvent.VK_UP){
keyheard = 3;
}
if(key == KeyEvent.VK_DOWN){
keyheard = 4;
}
}
/**public int[] lastTwoKeys(KeyEvent e){
last_two_keys[1] = last_two_keys[0];
last_two_keys[0] = keyheard;
return last_two_keys;
}*/
}
}
什麼動作會觸發彈出? – MadProgrammer 2015-02-24 04:22:27
碰撞會觸發彈出窗口。所以布爾isCollision = true;應該觸發彈出窗口 – abaxtastic 2015-02-24 17:09:08
好的,這是什麼「彈出」?它只是一個覆蓋主面板的面板嗎?對話框?還有其他的東西? – MadProgrammer 2015-02-24 20:17:16