Results 1 to 11 of 11
Thread: Press enter to continue (Jpanel)
- 05-21-2011, 08:52 AM #1
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
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.
- 05-21-2011, 09:05 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
You want to clear your panel when ENTER KEY is pressed but your code do different.
and your keyPressed method is waiting for 's'.Java Code:public void keyPressed(KeyEvent e) { if (e.getKeyChar() == 's') { showTitleScreen = false; } }
Also, Is this running? I do not see main?
- 05-21-2011, 09:11 AM #3
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
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.
- 05-21-2011, 09:30 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Your panel should be focusable and the focus should be on panel so you need to add
Also, you have to call the paintComponent() on keyPressed()Java Code:setFocusable(true); requestFocus();
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.
- 05-21-2011, 09:49 AM #5
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
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.
- 05-21-2011, 10:02 AM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
OK. Is your problem solved? Kindly mark this thread solve through thread tools located at top of this thread.
- 05-21-2011, 10:04 AM #7
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
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...
have got this now under the keypressed method but everyway I've tried to call the paintComponent method results in an error.Java Code:public void keyPressed(KeyEvent e) { if(e.getKeyChar() == e.VK_ENTER) { showTitleScreen = false; } }
- 05-21-2011, 10:22 AM #8
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
This does not end here. You have to call again the paintComponent() or update() method to clear JPanel under keyPressed method.Java Code:public A3JPanel() { setBackground(Color.white); showTitleScreen = true; addKeyListener(this); [b]setFocusable(true); requestFocus();[/b] }
Post your code on how you call paintComponent() under keyPressed().
- 05-21-2011, 11:27 AM #9
Don't even try to call paintComponent(...). The correct way to update a component is to call repaint().
Also, from the API for requestFocus():
dbBecause the focus behavior of this method is platform-dependent, developers are strongly encouraged to use requestFocusInWindow when possible.
- 05-22-2011, 10:08 AM #10
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
Cheers have solved :) Just added repaint(); Thanks guys!
- 05-22-2011, 03:40 PM #11
Newbies
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
[SOLVED] Press any key to continue??
By jon80 in forum New To JavaReplies: 4Last Post: 06-15-2009, 11:40 PM -
press any key to continue
By dotnet007 in forum New To JavaReplies: 3Last Post: 05-11-2008, 05:19 AM -
Reading Char without needing to press enter
By x0psci in forum New To JavaReplies: 0Last Post: 11-23-2007, 04:28 PM -
Press any key to continue/press enter
By JT4NK3D in forum New To JavaReplies: 1Last Post: 11-17-2007, 09:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks