2013-05-09 3 views
0

Je suis nouveau à Java, étudiant actuellement l'interface graphique. Je crée un jeu de labyrinthe simple. Actuellement, j'ai ma disposition de labyrinthe en utilisant un tableau String converti en un tableau 2D et en plaçant une image pour les murs et l'espace en utilisant 'W' et 'S' dans le tableau String. Le problème que je suis confronté maintenant est mon image de caractère se déplaçant avec les clefs de tableau, j'ai créé un BufferedImage, l'a peint à la forme et ai mon auditeur principal et je ne peux juste pas trouver où je vais mal.Java déplaçant un BufferedImage sur JFrame

Voici mon code;

public class Maze extends JFrame implements KeyListener { 

    private Container contain; 
    private JPanel pEast, pNorth, pSouth, pCenter, pWest; 
    private JButton btnStart, btnReset, btnExit; 
    private JLabel lblTitle, lblmazewall; 
    private JTextArea txtArea; 
    //private String s; 
    //private FileInputStream in; 
    //private int no; 
    //private char ch; 
    private ImageIcon mazewall; 
    private BufferedImage player; 
    private int xpos = 100, ypos = 100; 
    private String [] mazeLayout; 
    private String [][] mazeLayout2d; 



    Maze(){ 

     loadImage(); 
     mazeLayout = new String[10]; 
     mazeLayout2d = new String[10][10]; 

     contain = getContentPane(); 
     pEast = new JPanel(); 
     pNorth = new JPanel(); 
     pSouth = new JPanel(); 
     pCenter = new JPanel(); 
     pWest = new JPanel(); 



     //pCenter.setBackground(Color.BLUE); 

     pCenter.setLayout(new FlowLayout (FlowLayout.CENTER,0,0)); 

     pWest.setLayout(new FlowLayout (FlowLayout.CENTER, 10, 10)); 

     btnStart = new JButton("Start"); 
     btnStart.setFont(new Font("Dialog", Font.BOLD, 18)); 
     btnReset = new JButton("Reset"); 
     btnReset.setFont(new Font("Dialog",Font.BOLD,18)); 
     pEast.setLayout(new FlowLayout (FlowLayout.CENTER,10,10)); 
     pEast.add(btnStart); 
     pEast.add(btnReset); 

     btnExit = new JButton("Exit"); 
     btnExit.setFont(new Font("Dialog",Font.BOLD,18)); 
     pSouth.setLayout(new FlowLayout (FlowLayout.CENTER, 20,10)); 
     pSouth.add(btnExit); 

     lblTitle = new JLabel("Maze Game"); 
     lblTitle.setFont(new Font("Dialog",Font.BOLD,24)); 
     lblTitle.setHorizontalAlignment(SwingConstants.CENTER); 
     pNorth.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); 
     pNorth.add(lblTitle); 

     txtArea = new JTextArea(20,70); 
     txtArea.setFont(new Font("dialog",Font.BOLD,16)); 

     mazewall = new ImageIcon("mazewall.png"); 

//  Read in from a text file, needed later   
//  try{ 
//   in = new FileInputStream("maze.txt"); 
//   while((no = in.read()) != -1){ 
//    ch = (char)no; 
//    s+=ch; 
//   } 
//   txtArea.setText(s); 
//  }catch(FileNotFoundException ef){ 
//   JOptionPane.showMessageDialog(null, "File not found - maze.txt"); 
//   }catch(IOException e){ 
//    JOptionPane.showMessageDialog(null, "Unable to read file maze.txt"); 
//   } 

     contain.add(pEast, BorderLayout.EAST); 
     contain.add(pNorth, BorderLayout.NORTH); 
     contain.add(pSouth, BorderLayout.SOUTH); 
     contain.add(pCenter, BorderLayout.CENTER); 
     contain.add(pWest, BorderLayout.WEST); 

     //Array for maze 
     mazeLayout[0] = "WWWWWWWWWW"; 
     mazeLayout[1] = "WSSSWWSWWW"; 
     mazeLayout[2] = "WSWSWWSSSW"; 
     mazeLayout[3] = "WSWSWWWWSW"; 
     mazeLayout[4] = "WSWSWWWWSW"; 
     mazeLayout[5] = "WSWSWSSSSW"; 
     mazeLayout[6] = "WSWSWSWWWW"; 
     mazeLayout[7] = "WSWSWSWWWW"; 
     mazeLayout[8] = "WSWSSSWWWW"; 
     mazeLayout[9] = "WWWWWWWWWW"; 


     //Converting array into 2d array 
     for(int y = 0; y < 10; y++){ 
      for(int x = 0; x < 10; x++){ 
       mazeLayout2d[y][x] = mazeLayout[y].substring(x, x+1); 
       if (mazeLayout2d[y][x].equals("W")){ 
        lblmazewall = new JLabel(); 
        mazewall = new ImageIcon("mazewall.png"); 
        lblmazewall.setIcon(mazewall); 
        pCenter.add(lblmazewall); 
       } 
       if (mazeLayout2d[y][x].equals("S")){ 
        lblmazewall = new JLabel(); 
        mazewall = new ImageIcon("mazefloor.png"); 
        lblmazewall.setIcon(mazewall); 
        pCenter.add(lblmazewall); 
       } 
      } 
     }  
    } 

    public void loadImage(){ 
     try{ 
      String playerPath = "player.png"; 
      player = ImageIO.read(new File(playerPath)); 
     }catch(IOException ex){ 
      ex.printStackTrace(); 
     } 
     addKeyListener(this); 
    } 

    @Override 
    public void paint(Graphics g){ 
     super.paint(g); 
     g.drawImage(player, xpos, ypos,50,80, this);  
    } 


    public void keyPressed(KeyEvent ke){ 
     switch(ke.getKeyCode()){ 
     case KeyEvent.VK_RIGHT:{ 
      xpos+=3; 
     } 
     break; 
     case KeyEvent.VK_LEFT:{ 
      xpos-=3; 
     } 
     break; 
     case KeyEvent.VK_DOWN:{ 
      ypos+=3; 
     } 
     break; 
     case KeyEvent.VK_UP:{ 
      ypos-=3; 
     } 
     break; 
    } 
    repaint(); 
} 
    public void keyTyped(KeyEvent ke){} 
    public void keyReleased(KeyEvent ke){} 


} 

J'espère que quelqu'un peut me dire où est mon problème, merci.

+0

Que fait-il? Voyez-vous le joueur? Est-ce que ça bouge du tout? Si vous ajoutez un println au keyhandler, les frappes sont-elles traitées? –

+0

Je vois l'image du joueur à la position xpos et ypos. J'ai ajouté dans System.out.println ("RIGHT"); à gauche, de haut en bas dans les touches et rien ne se présente – Sqoh

Répondre

1

Ne remplacez pas la méthode paint() d'un conteneur de niveau supérieur (par exemple, JFrame).

La peinture personnalisée est effectuée en remplaçant la méthode paintComponent() d'un JPanel (ou JComponent). Donc, dans votre cas, vous devez remplacer le panneau central puisque c'est là que vous créez le labyrinthe. KeyEvent sont uniquement générés pour le composant qui a le focus.

Vous devrez donc faire en sorte que le panneau central puisse être mis au point.

De même, il n'est pas recommandé d'utiliser un KeyListener. Vous venez de trouver l'une des limites de cette ancienne approche qui était utilisée pour les applications AWT. Pour les applications Swing, vous devez utiliser les liaisons de touches. Rechercher sur le forum, ce conseil est donné quotidiennement.

De même, ne continuez pas à créer de nouveaux ImageIcons lorsque vous créez le labyrinthe. Vous n'avez besoin que d'une seule icône pour chaque image, vous pouvez réutiliser l'icône.