Results 1 to 11 of 11
  1. #1
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Press enter to continue (Jpanel)

    Have googled this extensively. I first discovered that "press any key to continue" wasn't possible but now I am confused as to what i've found on google. Can't seem to find any threads which refer to this on a gui interface...

    What I need is for the title screen to clear when enter is pressed. Any help appreciated!
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class A3JPanel extends JPanel implements KeyListener {
    	private boolean showTitleScreen;
    
     	public A3JPanel() {
    		setBackground(Color.white);
    		showTitleScreen = true;
    		addKeyListener(this);
    	}
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyChar() == e.VK_ENTER)  {
    			showTitleScreen = false;
    		}	
    	}
    	public void actionPerformed(ActionEvent e) {
    	}
       	public void paintComponent(Graphics g){
     	    super.paintComponent(g);
    		if (showTitleScreen == true) {
    			g.setFont(new Font ("sansserif", Font.BOLD, 25));
    			g.drawString("Welcome to Crashing Balls", 100, 50);
    			g.setFont(new Font ("serif", Font.PLAIN, 15));
    			g.drawString("The objective of this game is to control your own ball while avoiding the ", 50, 150);
    			g.drawString("bouncing balls for as long as you can. After you collide with a ball twice,", 50, 165);
    			g.setColor(Color.RED);
    			g.setFont(new Font ("serif", Font.BOLD, 15));
    			g.drawString("it's game over.", 200, 180);
    			g.fillOval(185, 220, 35, 35);
    			g.fillOval(280, 220, 35, 35);
    			g.fillOval(185, 340, 35, 35);
    			g.fillOval(280, 340, 35, 35);
    			g.setColor(Color.BLUE);
    			g.fillOval(230, 280, 35, 35);
    			g.setColor(Color.BLACK);
    			g.drawString("Press any key to begin",50, 410);
    			
    		}
    		else {
    			g.fillOval(230, 280, 35, 35);
    		}	
    			
    		
    
    	}
    	public void keyReleased(KeyEvent e) {}
    	public void keyTyped(KeyEvent e) {}
    
    }
    Last edited by david522; 05-21-2011 at 10:02 AM.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    You want to clear your panel when ENTER KEY is pressed but your code do different.
    Java Code:
    	public void keyPressed(KeyEvent e) {
    		if (e.getKeyChar() == 's') {
    			showTitleScreen = false;
    		}
    	}
    and your keyPressed method is waiting for 's'.

    Also, Is this running? I do not see main?

  3. #3
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Yeh sorry I was just testing to try and get something to work.. This is just the JPanel component. Have a JFrame and app as well.

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Your panel should be focusable and the focus should be on panel so you need to add
    Java Code:
     setFocusable(true);
     requestFocus();
    Also, you have to call the paintComponent() on keyPressed()

    Is this a homework? Are you required to use KeyListener? It is much easier and correct to use KeyBinding.
    Last edited by mine0926; 05-21-2011 at 09:42 AM.

  5. #5
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Nope not homework, basically a project where I have to make a game. We haven't actually learnt how to use KeyBindings yet though so would be a bit lost if i used them.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    OK. Is your problem solved? Kindly mark this thread solve through thread tools located at top of this thread.

  7. #7
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Nope, I don't really understand how to implement the methods you've described... Having trouble just calling the method.. Sorry I'm very new to this...
    Java Code:
    public void keyPressed(KeyEvent e) {
    		if(e.getKeyChar() == e.VK_ENTER)  {
    			showTitleScreen = false;
    		}	
    	}
    have got this now under the keypressed method but everyway I've tried to call the paintComponent method results in an error.

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    The component that will receive the keyPressed() is JPanel. By default (and you have not set it), JPanels are NOT focusable,
    and that is the problem because KeyListeners run ONLY when the component is IN FOCUS. To solve this issue you have to set JPanels focusable to true
    and then set window's focus to the JPanel.
    Java Code:
     	public A3JPanel()
            {
                setBackground(Color.white);
                showTitleScreen = true;
                addKeyListener(this);
                [b]setFocusable(true);
                requestFocus();[/b]
    	}
    This does not end here. You have to call again the paintComponent() or update() method to clear JPanel under keyPressed method.

    Post your code on how you call paintComponent() under keyPressed().

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Don't even try to call paintComponent(...). The correct way to update a component is to call repaint().

    Also, from the API for requestFocus():
    Because the focus behavior of this method is platform-dependent, developers are strongly encouraged to use requestFocusInWindow when possible.
    db

  10. #10
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Cheers have solved :) Just added repaint(); Thanks guys!

  11. #11
    shashank.ganta is offline Newbies
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default

    public void keyPressed(KeyEvent keyEvent){
    if(keyEvent.getKeyCode() == KeyEvent.VK_ENTER){
    repaint();
    }
    }


    this also works check it once...

Similar Threads

  1. [SOLVED] Press any key to continue??
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 06-15-2009, 11:40 PM
  2. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  3. Reading Char without needing to press enter
    By x0psci in forum New To Java
    Replies: 0
    Last Post: 11-23-2007, 04:28 PM
  4. Press any key to continue/press enter
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 09:27 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
  •