2
我在使用ArrayList在JPanel/Jframe內繪製多個圖像時遇到了一些麻煩。我的項目是創建一個Yar的復仇類遊戲。到目前爲止,沒有在附近這一點,但這裏是我有:使用ArrayList繪製圖像(JPanel,JFrame)
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements ActionListener, KeyListener, ImageObserver {
public Image ship;
public Image enemy;
public Image shot;
private int xLoc;
private int yLoc = 180;
private int xLoc2 = 700;
private int yLoc2 = 180;
private int xVel;
private int yVel;
private int xVel_en;
private int xVel_sh;
private ImageObserver observer;
private ArrayList<Base> shield = new ArrayList<Base>();
private Image shieldPiece;
public GamePanel() {
ship = Toolkit.getDefaultToolkit().createImage("ship3.png");
enemy = Toolkit.getDefaultToolkit().createImage("alien-ship-1.png");
shot = Toolkit.getDefaultToolkit().createImage("shot4.png");
shieldPiece = Toolkit.getDefaultToolkit().createImage("block1.png");
for(int x = 300; x < 420; x = x + 30)
for(int y = 100; y < 220; y = y + 30) {
Base temp = new Base(x,y);
shield.add(temp);
}
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(ship, xLoc, yLoc, null);
//g.drawImage(enemy, xLoc2, yLoc2, null);
for(Base b: shield)
g.drawImage(shieldPiece, b.getShieldX(), b.getShieldY(), null);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
move();
setBackground(Color.BLACK);
repaint();
}
private void move() {
int endOfWindow = getWidth();
int endOfWindow1 = getHeight();
if(xLoc>=(endOfWindow - ship.getWidth(null))|| xLoc<0)
xVel = -xVel;
xLoc +=xVel;
if(xLoc>=(endOfWindow - ship.getWidth(null)))
xVel = -xVel;
xLoc +=xVel;
if(yLoc>=(endOfWindow1 - ship.getHeight(null)) || yLoc<0)
yVel = -yVel;
yLoc +=yVel;
if(yLoc>=(endOfWindow1 - ship.getHeight(null)))
yVel = -yVel;
yLoc +=yVel;
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode() == KeyEvent.VK_D)
xVel = 3;
if(arg0.getKeyCode() == KeyEvent.VK_A)
xVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_W)
yVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_S)
yVel = 3;
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
xVel = 3;
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
xVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_UP)
yVel = -3;
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
yVel = 3;
//if(arg0.getKeyCode() == KeyEvent.VK_SPACE)
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_A)
xVel = 0;
if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_S)
yVel = 0;
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT || arg0.getKeyCode() == KeyEvent.VK_LEFT)
xVel = 0;
if(arg0.getKeyCode() == KeyEvent.VK_UP || arg0.getKeyCode() == KeyEvent.VK_DOWN)
yVel = 0;
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
,這裏是我的基類:
import java.awt.Color;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class GameWindow extends JFrame {
public GameWindow() {
setSize(800, 500);
GamePanel pan = new GamePanel();
Timer t = new Timer(40, pan);
add(pan);
this.addKeyListener(pan);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.start();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
annnnd是這樣的:參考
import java.awt.Image;
import java.awt.Toolkit;
public class Base {
private Image shieldPiece;
private int x;
private int y;
public Base(int xCoord, int yCoord) {
xCoord = x;
yCoord = y;
}
public int getShieldX() {
return x;
}
public int getShieldY() {
return y;
}
}
GameWindow類什麼彈出:
http://i.imgur.com/4bmf3Ab.png
它應該在船的右側(從x = 300,y = 100開始)繪製多個屏蔽件(紅色矩形)。窗口是800x500。我不確定爲什麼它不通過數組列表循環繪製每一部分。任何人都可以幫我嗎?
糟糕,錯過了。工作!謝謝,只需要另一雙眼睛! – Stax 2013-05-04 20:12:19