Note that there are some explanatory texts on larger screens.

plurals
  1. POJava moving a BufferedImage on JFrame
    primarykey
    data
    text
    <p>I'm new to Java, currently studying GUI. I'm creating a simple Maze game. Currently i have my maze 'layout' using a String array converted into a 2D array and placing a image for walls and space by using 'W' and 'S' in in String array.</p> <p>The problem i am facing now is my character image moving with the array keys, i have created a BufferedImage, painted it to the form and have my key listener and i just cant find where im going wrong.</p> <p>Here is my code;</p> <pre><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 &lt; 10; y++){ for(int x = 0; x &lt; 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){} } </code></pre> <p>Hopefully someone can tell me where my problem is, thankyou.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload