Results 1 to 8 of 8
  1. #1
    Jayayoh is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Question ActionListener conflicting Keylistener

    Hello. I am creating a pong app. the game itself works great, so I decided to introduce a title page into the application. to create this title page, I created an if statement in the paint() method to only paint the title screen when an integer called lvl was equal to 0. The main class has a key listener while the JPanel has an actionlistener. when the title page is drawn, it has a button called start, which increases the lvl integer to 1, where the game will start. If I press this button, the scene will change, but the keystrokes will not respond. If I start the game on lvl 1 instead of the title page, the keystrokes work. If I start the game on the title page, I can get response from the keys UNTIL I have pressed the button to increase the integer. Does anyone have any solutions for me? Any and all comments are greatly appreciated.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    If I start the game on the title page, I can get response from the keys UNTIL I have pressed the button to increase the integer
    What does the button listener change that stops the response from the keys?

    Can you post code to demonstrate your problem.

  3. #3
    Jayayoh is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default the code

    Yes. Here is the code that directly relates to the problem. I have not shown the other classes that operate the game, for length's sake. I dont see any problem involving the button directly stoping the keys, but as I am still learning this language, a different perspective might shed some light on this.
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    class pong extends JFrame implements KeyListener
    {
    	static Toolkit tool = Toolkit.getDefaultToolkit();
    	final static int game_width = 500;
    	final static int game_height = 400;
    	static int lvl = 0;
    	static boolean up = false;
    	static boolean down = false;
    	
    	public static void main(String[] args)
    	{ 
    		pong window = new pong();
    	}
    	
    	public pong()
    	{
    		super("Classic Pong");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            setVisible(true);
    		setSize(game_width,game_height);
    		setResizable(false);
    		add(new mainpnl());
    		this.addKeyListener(this);
    	}
    	
    	public void keyPressed(KeyEvent e)
    	{
    		if(e.getKeyCode() == 40)
    		{
    			up = false;	
    			down = true;
    		}
    		if(e.getKeyCode() == 38)
    		{
    			down = false;
    			up = true;
    		}
    	}
    		
    	public void keyTyped(KeyEvent e)
    	{
    	}
    		
    	public void keyReleased(KeyEvent e)
    	{
    		if(e.getKeyCode() == 40)
    		{
    			up = false;	
    			down = false;
    		}
    		if(e.getKeyCode() == 38)
    		{
    			down = false;
    			up = false;
    		}
    	}
    		
    	static public void gameloop()
    	{
    		if(lvl == 2)
    		{
    			ball.move();
    			enemy.move();
    			player.move();
    		}
    		else if(lvl == 0)
    		{
    		}
    		else if(lvl == 1)
    		{
    			event.counting.start();
    		}
    		System.out.println("up = " + up + " down = " + down);
    	}
    	
    	
    	
    	class mainpnl extends JPanel implements ActionListener
    	{
    		public mainpnl()
    		{
    			setVisible(true);
    			setLayout(null);
    			setSize(game_width,game_height);
    			update.start();
    		}
    		
    		public void paintComponent(Graphics g)
    		{
    			super.paintComponent(g);
    			if(lvl == 2)
    			{
    				remove(title.Title);
    				remove(title.start);
    				remove(event.number);
    				g.setColor(Color.black);
    				g.fillRect(0,0,pong.game_width,pong.game_height);
    				g.setColor(Color.green);
    				g.fillOval(ball.x,ball.y,ball.width,ball.height);
    				g.setColor(Color.yellow);
    				g.fillRect(enemy.x,enemy.y,enemy.width,enemy.height);
    				g.setColor(Color.white);
    				g.fillRect(player.x,player.y,player.width,player.height);
    				g.setColor(Color.yellow);
    				g.drawString(score.playername + score.playerscore,10,25);
    				g.drawString(score.enemyname + score.enemyscore,400,25);
    				
    			}
    			else if(lvl == 1)
    			{
    				remove(title.Title);
    				remove(title.start);
    				add(event.number);
    				event.number.setBounds(150,100,200,200);
    				g.setColor(Color.black);
    				g.fillRect(0,0,pong.game_width,pong.game_height);
    				g.setColor(Color.green);
    				g.fillOval(ball.x,ball.y,ball.width,ball.height);
    				g.setColor(Color.yellow);
    				g.fillRect(enemy.x,enemy.y,enemy.width,enemy.height);
    				g.setColor(Color.white);
    				g.fillRect(player.x,player.y,player.width,player.height);
    				g.setColor(Color.yellow);
    				g.drawString(score.playername + score.playerscore,10,25);
    				g.drawString(score.enemyname + score.enemyscore,400,25);
    			}
    			else if(lvl == 0)
    			{
    				add(title.start);
    				title.start.setBounds(50,200,75,25);
    				title.start.addActionListener(this);
    				
    				add(title.Title);
    				title.Title.setBounds(-3,-8,title.width,title.height);
    				
    				g.setColor(Color.white);
    				g.fillRect(0,0,pong.game_width,pong.game_height);
    			}
    		}
    		
    		Timer update = new Timer(10,new ActionListener(){public void actionPerformed(ActionEvent event){
    			pong.gameloop();
    			repaint();
    		}});
    		
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource() == title.start)
    			{
    				lvl = 1;
    			}
    		}
    	}
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Will this code compile and execute and demonstrate the problem?
    An executing program is needed to see and solve the problem.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Don't ever ever change state in a painting method override. You have absolutely no control over how often and how many times the method will be called.

    Get those calls to remove(...) and add(...) out of there. The right place for mucking around with the displayed components is where you assign a new value to lvl.

    Also, have you heard of CardLayout?

    db

  6. #6
    Jayayoh is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    So your suggesting taking the add/remove out of the paint() method. I have tried that but in some cercumstances, this wont work. I will recieve errors such as "non-static method add cannot be referenced from a static variable" as one. I do not know how to dynamically add / subtract components from a GUI. furthermore, I do not know what card layout is, but I will look into it further.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Do yourself a favor and get a grip on basics before mucking around with GUIs.

    The Java™ Tutorials

    db

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I will recieve errors
    When you get error messages you don't understand, post them with your questions.

Similar Threads

  1. Replies: 7
    Last Post: 02-27-2011, 01:17 AM
  2. ActionListener+KeyListener
    By mandelbrot in forum AWT / Swing
    Replies: 5
    Last Post: 09-10-2010, 12:25 AM
  3. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  4. KeyListener Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  5. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •