2015-05-04 60 views
0

我在獲取程序中創建的兩個正方形時遇到了一些麻煩,導入的ImageIcon顯示在我的JPanel上。我已經有一段時間了,並且仍然無法弄清楚我能做些什麼來讓它們顯示出來,並且允許玩家圖像移動。這是我的代碼的第一部分是迷宮類:圖像未顯示在JPanel上?

public class Maze extends JPanel{ 
int[][][] mazeArray; //will be defined in the loop depending on how many rectangles are spawned, also for making sure the player can't walk over the walls (first is what number wall it is, second is the x coordiante of the wall, and third is the y coordinate of the wall) 
//depending on whether or not the rctangle is vertical or horizontal it will use both values for its height and width (vertical would have sideX for the height, and sideY for the width; this would be the opposite for the horizontal rectangle) 
int sideX = 50; //x side length 
int sideY = 50; //y side length 
int x; 
int y; 
//setters and getters for use later (for changing the rctangles location and size) 
public void setSideX(int sideX){ 
    this.sideX = sideX; 
} 
public int getSideX(){ 
    return sideX; 
} 

public void setSideY(int sideY){ 
    this.sideY = sideY; 
} 
public int getSideY(){ 
    return sideY; 
} 

public void setCoordinates(int x, int y){ 
    this.x = x; 
    this.y = y; 
} 

public void setX(int x){ 
    this.x = x; 
} 
public int getX(){ 
    return x; 
} 

public void setY(int y){ 
    this.y = y; 
} 
public int getY(){ 
    return y; 
} 
//end setters and getters 
public void generateMaze(){ 
    //the left side of the maze 
    for(int i = 0; i < 10; i++){ //ten blocks on the left side 
     setX(0); //x is always 0 for the left side 
     setY(getY() + 50); //adds 50 to the previous Y coordinate amount, m making it go down the whole left side 
    } 
    setY(0); //set y back to zero to be able to start the right column of blocks 
    //the right side of the maze 
    for(int i = 0; i < 10; i++){ 
     setX(500); //x is always 500 for the right side 
     setY(getY() + 50); //does the same as it did on the left side, except on the right 
    } 
    setY(0); //set y to zero again 
    setX(50); //start x at 50 since there is no need to remake the corners 
    for(int i = 0; i < 8; i++){ //only goes up to 8 this this time because the corners of the maze can be ignored 
     setY(0); //just in case y changes back 
     setX(getX() + 50); //x increases by 50 each time 
    } 
    setY(500); 
    setX(50); 
    for(int i = 0; i < 8; i++){ //same as above except for the bottom 
     setY(500); 
     setX(getX() + 50); 
    } 
    //the maze walls are now generated 
} 
public void paintComponent(Graphics g){ //for painting the rectangles 
    super.paintComponent(g); 
    g.setColor(Color.BLACK); 
    g.fillRect(getX(), getY(), sideX, sideY); //uses x and y coordinates defined in the generateMaze loop, and the uses whatever current value for the two side, depending on what type of rectangle it is 
} 
} 

這是我用來創建迷宮牆。現在到了與選手圖像涉及Player類,玩家座標,玩家的移動速度:

public class Player { 
//Player starts in the top left corner 
int playerX = 50; 
int playerY = 50; 
int moveSpeed = 5; //I can edit move speed here 
Image character; 

//getters and setters to utilize the player's location and image 
public Player(){ //constructor for initial starting points 
    playerX = 50; 
    playerY = 50; 
    ImageIcon player = new ImageIcon("E://Workspace//Maze//images//Player.jpg"); 
    character = player.getImage(); 
} 

public void setPlayerX(int playerX){ 
    this.playerX = playerX; 
} 
public int getPlayerX(){ 
    return playerX; 
} 

public void setPlayerY(int playerY){ 
    this.playerY = playerY; 
} 
public int getPlayerY(){ 
    return playerY; 
} 

public void setMoveSpeed(int moveSpeed){ 
    this.moveSpeed = moveSpeed; 
} 
public int getMoveSpeed(){ 
    return moveSpeed; 
} 

public Image getPlayerImage(){ 
    return character; 
} 
} 

下一個就是我覺得這個問題是出現在玩家的圖像(迷宮,我認爲它是什麼在迷宮類本身,雖然也可以在佈局類的問題以及):

public class Layout extends JPanel implements ActionListener { //GUI with a non null FlowLayout 
Maze m = new Maze(); 
Player p = new Player(); 

//500 x 500 seemed like a good size for the maze game 
int x = 500; 
int y = 500; 
Image player; 
JPanel panel; 

public Layout() { 
    panel = new JPanel(); 
    panel.setLayout(new FlowLayout()); //same as the JFrame 
    panel.addKeyListener(new Move(p)); 
    panel.setFocusable(true); 
    panel.setBackground(Color.YELLOW); //background of the maze 
    m.generateMaze(); //create the maze 
} 

//for use in setting and getting the borders of the game 
public void setX(int x){ 
    this.x = x; 
} 
public int getX(){ 
    return x; 
} 

public void setY(int y){ 
    this.y = y; 
} 
public int getY(){ 
    return y; 
} 

public JPanel getPanel(){ 
    return panel; 
} 
@Override //so it can repaint as needed 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    g.drawImage(p.getPlayerImage(), p.getPlayerX(), p.getPlayerY(), this); 
} 

public void actionPerformed(ActionEvent ae){ 
    repaint(); 
} 
} 

class Move implements KeyListener { //for player movement 
final Player p; 
Move(Player p){ 
    this.p = p; 
} 

public void keyPressed(KeyEvent press) { //for the movement in the game 
    //I used both keys so that if the player woukld like to use WASD or the arrow keys either will work 
    if(press.getKeyCode() == KeyEvent.VK_W || press.getKeyCode() == KeyEvent.VK_UP){ 
     //move up 
     p.setPlayerY(p.getPlayerY() - p.getMoveSpeed()); 
    } 
    else if(press.getKeyCode() == KeyEvent.VK_S || press.getKeyCode() == KeyEvent.VK_DOWN){ 
     //move down 
     p.setPlayerY(p.getPlayerY() + p.getMoveSpeed()); 
    } 
    else if(press.getKeyCode() == KeyEvent.VK_A || press.getKeyCode() == KeyEvent.VK_LEFT){ 
     //move left 
     p.setPlayerX(p.getPlayerX() - p.getMoveSpeed()); 
    } 
    else if(press.getKeyCode() == KeyEvent.VK_D || press.getKeyCode() == KeyEvent.VK_RIGHT){ 
     //move right 
     p.setPlayerX(p.getPlayerX() + p.getMoveSpeed()); 
    } 
} 


public void keyReleased(KeyEvent release) { 
    //nothing is needed here 
} 

public void keyTyped(KeyEvent e) { 
    //does nothing if a key is type (no need for it) 
} 
} 

最後是運行它的類,但我不認爲有任何的問題在這裏,但只是在不管怎樣我都會把它扔到這裏:

public class Play extends JPanel { 
public static void main(String[]args) { 
    play(); 
} 

public static void play() { 
    JFrame f = new JFrame(); 
    Layout l = new Layout(); 
    JPanel j = l.getPanel(); 

    f.setTitle("Maze Game for final project"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setSize(l.getX(), l.getY()); //size can be changed in layout 
    f.setVisible(true); 
    f.add(j); //adds the panel 
} 
} 

我試圖讓玩家的圖像在50,50的時候產生,然後啓動時也會產生牆壁。但是,目前唯一顯示的JPanel是黃色背景。幫助將不勝感激!

新更新的代碼在這裏:

public static void play() { 
    JFrame f = new JFrame(); 
    Layout l = new Layout(); 

    f.setTitle("Maze Game for final project"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setSize(l.getX(), l.getY()); //size can be changed in layout 
    f.add(new Layout()); //adds the panel 
    f.setVisible(true); 
} 

,也爲佈局構造

public Layout() { 
    setLayout(new FlowLayout()); 
    addKeyListener(new Move(p)); 
    setFocusable(true); 
    setBackground(Color.YELLOW); 
    m.generateMaze(); 
} 

回答

1

我不能說我已經通過所有的代碼了,但有一點沒有罷工我:

您的佈局類擴展JPanel並重寫paintComponet,但您從不使用此類作爲JPanel。相反,您可以在其中使用其他一些名爲panel的JPanel變量。擺脫那個變量,而是使用佈局類本身的JPanel,並且至少一些您的問題可能會被修復。

例如,

public class Layout extends JPanel implements ActionListener { 
    Maze m = new Maze(); 
    Player p = new Player(); 
    int x = 500; 
    int y = 500; 
    Image player; 

    // JPanel panel; 

    public Layout() { 
     // panel = new JPanel(); 
     // panel.setLayout(new FlowLayout()); //same as the JFrame 
     // panel.addKeyListener(new Move(p)); 
     // panel.setFocusable(true); 
     // panel.setBackground(Color.YELLOW); //background of the maze 

     setLayout(new FlowLayout()); 
     addKeyListener(new Move(p)); 
     setFocusable(true); 
     setBackground(Color.YELLOW); 
     m.generateMaze(); 
    } 

    public void setX(int x) { 
     this.x = x; 
    } 

    public int getX() { 
     return x; 
    } 

    public void setY(int y) { 
     this.y = y; 
    } 

    public int getY() { 
     return y; 
    } 

    // public JPanel getPanel() { 
    // return panel; 
    // } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawImage(p.getPlayerImage(), p.getPlayerX(), p.getPlayerY(), this); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     repaint(); 
    } 
} 

的另一個問題是你KeyListeners的使用,因爲按鍵綁定將是非常理想在這裏,但我會離開,你的下一個問題

此外,您將JPanel添加到JFrame 之後在JPanel上調用setVisible(true) - 不要這樣做。只有在添加完所有組件後才能撥打setVisible(true)

+0

我甚至不再有任何東西出現,你能告訴我如何正確使用我在Play中佈局中創建的JPanel(我真的有這個問題)? – user1234

+0

@ user1234:您是否將Layout JPanel添加到您的GUI? –

+0

@ user1234:ouch。請勿在註釋中張貼代碼,因爲它會丟失格式,導致無法閱讀。相反,通過[編輯您的問題](http://stackoverflow.com/posts/30034015/edit)將任何新代碼發佈到原始問題的底部。 –