glassPane update problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Neverhood
    New Member
    • Nov 2007
    • 3

    glassPane update problem

    Hi folks,
    I'm trying to make a small game about being a drug dealer, with a 10x8 grid JPanel. On top of that i have another 10x8 grid JPanel as glassPane for the player icon.

    I have all the properties of the player in a player object class with getter and setter methods for fx his X and Y position in the grid.
    When i set up his X and Y position myself everything works fine and he is painted the right place.
    My problem is when my keyPressed method updates his X,Y position and repaint the glass pane everything goes wrong. At first key press (no matter which) he disappers, and i have no ideer why.

    Something about the repainting of the glassPane goes wrong, but what?

    Any help or hint would be greatly appreciated.

    Here is my display code:
    Code:
    package org.DealOrDie;
    
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class GUI extends Main implements ActionListener, KeyListener {
    	
    	//import Player object
    	Player player = new Player();
    	
    	//description of start city
    	private final String[][] startCity = { 
    			{"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
    			{"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
    			{"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
    			{"grs", "vej", "hus", "grs", "hus", "grs", "grs", "grs", "grs", "grs"},
    			{"grs", "vej", "vej", "vej", "vej", "vej", "hus", "hus", "grs", "grs"},
    			{"grs", "grs", "vej", "grs", "grs", "vej", "vej", "hus", "grs", "grs"},
    			{"grs", "grs", "vej", "grs", "grs", "vej", "hus", "hus", "grs", "grs"},
    			{"grs", "grs", "hus", "grs", "grs", "vej", "grs", "grs", "grs", "grs"} };
    	
    	//city now
    	private final String[][] city = { 
    			{"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
    			{"grs", "vej", "grs", "grs", "grs", "grs", "hus", "hus", "hus", "grs"},
    			{"grs", "vej", "grs", "grs", "grs", "vej", "vej", "vej", "vej", "grs"},
    			{"grs", "vej", "hus", "grs", "hus", "vej", "grs", "grs", "grs", "grs"},
    			{"grs", "vej", "vej", "vej", "vej", "vej", "hus", "hus", "grs", "grs"},
    			{"grs", "grs", "vej", "grs", "grs", "vej", "vej", "hus", "grs", "grs"},
    			{"grs", "grs", "vej", "grs", "grs", "vej", "hus", "hus", "grs", "grs"},
    			{"grs", "grs", "hus", "grs", "grs", "vej", "grs", "grs", "grs", "grs"} };
    	
    	private JFrame frame;
    	private JPanel cityPanel;
    	private JPanel playerPane;
    	private JLabel icon;
    	
    	public GUI() {
    		player.reset();
    		
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("Deal Or Die");
    		frame.setLocation(200, 50);
    		frame.setSize(640, 512);
    		frame.setResizable(false);
    		frame.addKeyListener(this);
    		
    		setupMenuBar();
    		paintCity();
    		paintPlayerPane();
    		//paintStatusBar();
    		
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	private void setupMenuBar() {
    		JMenuBar menuBar = new JMenuBar();
    		JMenu menu = new JMenu("File");
    		JMenuItem menuItem = new JMenuItem("New");
    		menuItem.setActionCommand("New");
    		menuItem.addActionListener(this);
    		menu.add(menuItem);
    		menuItem = new JMenuItem("Load");
    		menuItem.setActionCommand("Load");
    		menuItem.addActionListener(this);
    		menu.add(menuItem);
    		menuItem = new JMenuItem("Save");
    		menuItem.setActionCommand("Save");
    		menuItem.addActionListener(this);
    		menu.add(menuItem);
    		menuItem = new JMenuItem("Exit");
    		menuItem.setActionCommand("Exit");
    		menuItem.addActionListener(this);
    		menu.add(menuItem);
    		menuBar.add(menu);
    		menu = new JMenu("Help");
    		menuItem = new JMenuItem("Help");
    		menuItem.setActionCommand("Help");
    		menuItem.addActionListener(this);
    		menu.add(menuItem);
    		menuItem = new JMenuItem("About");
    		menuItem.setActionCommand("About");
    		menuItem.addActionListener(this);
    		menu.add(menuItem);
    		menuBar.add(menu);
    		frame.setJMenuBar(menuBar);
    		
    	}
    
    	private void paintCity() {
    		//sets up a city map of 8,10 squares. Painted according to the city object
    		cityPanel = new JPanel();
    		cityPanel.setLayout(new GridLayout(8,10));
    		frame.add(cityPanel, BorderLayout.CENTER);
    		String citymap[][] = getCity();
    		for (int x = 0; x < 8; x++) {
    			for (int y = 0; y < 10; y++) {
    				ImageIcon iconX = null;
    				if (citymap[x][y].equals("grs")) { iconX = createImageIcon("images/grs.JPG"); } 
    				if (citymap[x][y].equals("vej")) { iconX = createImageIcon("images/vej.JPG"); }
    				if (citymap[x][y].equals("hus")) { iconX = createImageIcon("images/hus.JPG"); }
    				icon = new JLabel();
    				icon.setIcon(iconX);
    				cityPanel.add(icon);
    			}
    		}		
    	}
    	
    	private void paintPlayerPane() {
    		//setup a JPanel and use it as glasspane on top of city map
    		
    		playerPane = new JPanel();
    		playerPane.setLayout(new GridLayout(8,10));
    		ImageIcon icon_player = createImageIcon("images/player.gif");
    		
    		for (int y = 0; y <= 7; y++) { //paint grid with empty labels except at player position 
    			for (int x = 0; x <= 9; x++) {
    			if ((player.getPositionX() == x) && (player.getPositionY() == y)) {
    					JLabel label_player = new JLabel();
    					label_player.setSize(64, 64);
    					label_player.setIcon(icon_player);
    					playerPane.add(label_player);
    				}else {
    					JLabel label_player = new JLabel();
    					label_player.setSize(64, 64);
    					label_player.setIcon(null);
    					playerPane.add(label_player);
    				}
    			}
    		
    		}
    		frame.setGlassPane(playerPane);
    		playerPane.setOpaque(false);
    		playerPane.setVisible(true);
    		
    	}
    	
    	private String[][] getCity() {
    		return city;
    	}
    
    	private void paintStatusBar() {
    		// TODO Auto-generated method stub
    		
    	}
    		
    	protected ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = getClass().getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    	
    	public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("Save")) {
    			
    		}
    		else if (e.getActionCommand().equals("Load")) {
    			
    		}
    		else if (e.getActionCommand().equals("Exit")) {
    			System.exit(0);
    		}
    		else if (e.getActionCommand().equals("Help")) {
    			JOptionPane.showMessageDialog(frame, "");
    		}
    		else if (e.getActionCommand().equals("About")) {
    			JOptionPane.showMessageDialog(frame, "Deal or Die beta 0.1 ©2007\n\nMade by Svante Joergensen");
    		}
    		else if (e.getActionCommand().equals("New")) {
    			
    		}
    	}
    	
    	public void keyPressed(KeyEvent e) {
    		if (e.getKeyCode() == 37) { //if left arrow is being pressed
    			if ((player.getPositionX() - 1) >= 0) { //if left of player is within map limits
    				player.setPositionX((player.getPositionX() - 1)); //move player 1 to the left
    			}
    		}
    		if (e.getKeyCode() == 38) { //if up arrow is being pressed
    			if ((player.getPositionY() - 1) >= 0) { //if above player is within map limits
    				player.setPositionY((player.getPositionY() - 1)); //move player 1 up
    			}
    		}
    		if (e.getKeyCode() == 39) { //if right arrow is being pressed
    			if ((player.getPositionX() + 1) <= 9) { //if right of player is within map limits
    				player.setPositionX((player.getPositionX() + 1)); //move player 1 to the right
    				System.out.println("right!!!");
    			}
    		}
    		if (e.getKeyCode() == 40) { //if down arrow is being pressed
    			if ((player.getPositionY() + 1) <= 7) { //if under player is within map limits
    				player.setPositionY((player.getPositionY() + 1)); //move player 1 down
    			}
    		}
    		JPanel empty = new JPanel();
    		frame.setGlassPane(empty);
    		paintPlayerPane();
    		frame.repaint();
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    And my player object class:
    Code:
    package org.DealOrDie;
    
    public class Player {
    	
    	private String name;
        private int positionX;
        private int positionY;
    	private float cash;
    	private int heroin;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public int getPositionX() {
    		return positionX;
    	}
    	public void setPositionX(int positionX) {
    		this.positionX = positionX;
    		System.out.println("player position is now: (" + this.positionX + "," + this.positionY + ")");
    	}
    	public int getPositionY() {
    		return positionY;
    	}
    	public void setPositionY(int positionY) {
    		this.positionY = positionY;
    		System.out.println("player position is now: (" + this.positionX + "," + this.positionY + ")");
    	}
    	public float getCash() {
    		return cash;
    	}
    	public void setCash(float cash) {
    		this.cash = cash;
    	}
    	public int getHeroin() {
    		return heroin;
    	}
    	public void setHeroin(int heroin) {
    		this.heroin = heroin;
    	}
    	public void reset() {
    		name = "player";
    		positionX = 5;
    		positionY = 5;
    		cash = 250;
    		heroin = 0;
    	}
    	
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Am I missing the method slapThatHo()?

    Comment

    Working...