Results 1 to 11 of 11
  1. #1
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default deselecting a button after the click.

    ok. i have 3 buttons on my screen and my applet works great until i click the button. the function of the button works great but after the button is click it leaves a small ring int he button and makes it where when i press keys the applet dosent respawn.

    anyideas?

    please and thanks ; ]

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You may have tied up the edt, hard to say. why not post a small compilable program that reproduces your problem. Then we'll all be able check and perhaps fix the code.

    Best of luck

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It sounds like a deadlock condition in your event handler.

  4. #4
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    alright heres my code... now when i run the applet and you choose a difficulty it stops listening for the buttons.. but when you just press S to start the game it works fine...






    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import javax.swing.*;

    public class GUITAR_HERO extends Applet implements ActionListener, MouseListener, KeyListener
    {
    // instance variables:
    Timer timer;
    private int difficulty;
    private int money;

    private boolean leftbool=true;
    private boolean upbool=true;
    private boolean downbool=true;
    private boolean rightbool=true;

    private int key;

    private Button easybutton;
    private Button mediumbutton;
    private Button hardbutton;

    private int lefty=75;
    private int upy=75;
    private int downy=75;
    private int righty=75;

    private int leftspeed=1;
    private int upspeed=1;
    private int downspeed=1;
    private int rightspeed=1;

    private int scorex=50;
    private int scorey=400;

    private boolean start=true;

    public void init()
    {
    easybutton = new Button("Easy");
    add(easybutton);
    easybutton.addActionListener(this);

    mediumbutton = new Button("Medium");
    add(mediumbutton);
    mediumbutton.addActionListener(this);

    hardbutton = new Button("Hard");
    add(hardbutton);
    hardbutton.addActionListener(this);


    timer = new Timer(15, this);
    //timer.start(); //timers

    addMouseListener(this);
    addKeyListener(this);
    setFocusable(true);





    }

    public void mouseEntered(MouseEvent me){}
    public void mouseExited(MouseEvent me){}
    public void mousePressed(MouseEvent me){}
    public void mouseReleased(MouseEvent me){}
    public void mouseClicked(MouseEvent me)
    {

    }

    public void keyPressed(KeyEvent ke)
    {
    key = ke.getKeyCode();//returns the interger coe of the key taht was pressed

    if (key==KeyEvent.VK_LEFT)//left key listener
    {
    if ( ((500-lefty) <= 110) && ((500-lefty) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-lefty) <= 130) && ((500-lefty) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_UP)//left key listener
    {
    if ( ((500-upy) <= 110) && ((500-upy) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-upy) <= 130) && ((500-upy) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_DOWN)//left key listener
    {
    if ( ((500-downy) <= 110) && ((500-downy) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-downy) <= 130) && ((500-downy) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_RIGHT)//left key listener
    {
    if ( ((500-righty) <= 110) && ((500-righty) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-righty) <= 130) && ((500-righty) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_P)//left key listener
    {
    timer.stop();

    }

    if (key==KeyEvent.VK_S)//left key listener
    {
    timer.start();

    }



    }
    public void keyReleased(KeyEvent ke){}
    public void keyTyped(KeyEvent ke){}



    public void actionPerformed(ActionEvent ae)
    {
    Object source = ae.getSource(); //allows buttons

    if (source.equals(easybutton))
    {
    timer.stop();
    difficulty=1000;
    start=true;
    timer= new Timer(50, this);
    timer.start();


    }

    if (source.equals(mediumbutton))
    {
    timer.stop();
    difficulty=50;
    start=true;
    timer= new Timer(25, this);
    timer.start();

    }

    if (source.equals(hardbutton))
    {
    timer.stop();
    difficulty=10;
    start=true;
    timer=new Timer(12, this);
    timer.start();
    }
    setFocusable(true);
    repaint();

    }

    //public void update(Graphics g)
    //{
    //super.update(g);

    // paint(g);
    //money(g);

    //}

    public void paint(Graphics g)
    {


    if (start==true)
    {
    left(g);
    up(g);
    down(g);
    right(g);
    }
    money(g);
    target(g);
    running(g);

    }

    public void left(Graphics g)
    {
    g.fillRect(50, lefty, 50, 50);
    if (lefty==75)
    {
    leftspeed = (int) (Math.random()*4);
    }

    lefty+=leftspeed;

    if (lefty>=450)
    {
    lefty=75;
    leftbool=true;
    }


    }

    public void up(Graphics g)
    {
    g.fillRect(150, upy, 50, 50);
    if (upy==75)
    {
    upspeed = (int) (Math.random()*4);
    }
    upy+=upspeed;

    if (upy>=450)
    {
    upy=75;
    upbool=true;
    }

    }

    public void down(Graphics g)
    {
    g.fillRect(250, downy, 50, 50);
    if (downy==75)
    {
    downspeed = (int) (Math.random()*4);
    }
    downy+=downspeed;

    if (downy>=450)
    {
    downy=75;
    downbool=true;
    }
    }

    public void right(Graphics g)
    {
    g.fillRect(350, righty, 50, 50);
    if (righty==75)
    {
    rightspeed = (int) (Math.random()*4);
    }
    righty+=rightspeed;

    if (righty>=450)
    {
    righty=75;
    rightbool=false;
    }

    }

    public void target(Graphics g)
    {
    scorex=50;
    for (int target=1; target<=4; target++)
    {
    g.setColor(Color.GREEN);
    g.drawRect(scorex, scorey, 50, 50);
    scorex+=100;
    }


    g.setColor(Color.CYAN);
    g.drawString("left", 70, 425);

    g.setColor(Color.CYAN);
    g.drawString("up", 170, 425);

    g.setColor(Color.CYAN);
    g.drawString("down", 261, 425);

    g.setColor(Color.CYAN);
    g.drawString("right", 365, 425);
    }

    public void money(Graphics g)
    {
    g.setColor(Color.BLACK);
    String pmoney = Integer.toString(money);

    g.drawString(pmoney, 405, 50);

    }

    public void running(Graphics g)
    {
    boolean pause;

    pause = timer.isRunning();

    if (pause==false)
    {
    g.drawString("Applet paused press S to start", 5, 490);
    }
    else
    {
    g.drawString("Press P to pause", 5, 490);
    }

    }




    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    to solve your problem just call setFocasable(false) on all the buttons. i.e.,
    Java Code:
    easybutton.setFocusable(false);
    This prevents the buttons from retaining focus and allows your pane to keep focus allowing the keylisteners to work.

    BUT having said that, there are other issues that need to be addressed. First and foremost: don't put program logic inside of your paint method or methods called from within paint. For instance your paint method calls left(g) which increments lefty and does some boolean checks. You must realize that you have only partial control of when the paint method is called. You can suggest that the JVM call paint via a call to repaint, but in many situations, the JVM can choose to ignore you. Likewise, the JVM may be required to call paint due to forces outside of your program, in particular due to the OS telling the JVM that it's window is not clean. So better is to you have your game loop outside of paint, and call repaint as needed after each loop of your gui. Then just have paint draw, that's it. Don't have it increment a variable or set a boolean; just have it draw.

    The paint method should be used for painting and not much else. Second, I strongly suggest you move away from AWT and learn to code in the more advanced and robust Swing idiom. Third, if you do code in Swing, you'll want to draw in JPanels via paintComponent method and use key binding rather than key listeners.

    Finally (for now) please use code tags when posting code in this forum. Not doing so makes your code quite hard to read.

    To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]
    Last edited by Fubarable; 02-15-2009 at 06:30 AM.

  6. #6
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    sorry about that (im new here)

    this is my 6 week project for school and i did it in like 2 days ; ]

    ive only been programming java since december so im still quite new, and this is just stuff my teacher has taught us. i really enjoy programming we started with c++ in the first semester and moved in to java.

    Most of what you said i didnt really understand. what exactly AWT? and swing idiom, Jpanels, and whats the difference between listeners and bindings?

    im sorry about all the questions. im really interested in learning more about java.

  7. #7
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    ohh and by the way. code worked perfectly! i tried to reset focus back to the applet but i neer thought of unfocusing the button. thanks a bunch.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    AWT is a graphical user interface (GUI) library that's been with Java from near its inception. It uses the operating system to create its widgets, its buttons and windows and what not, and these widgets are thus called heavy weight components. Swing is another GUI library that is newer, was built on top of AWT, creates 90% of its widgets via Java (or light weight components), is much more flexible, much more powerful, and just a better overall library to work with.

    AWT uses Frames, Applets, Panels, Canvases, Buttons, TextFields, and the like. Swing uses JFrames, JApplets, JPanels, JComponents, JButtons, JTextFields and the like. You don't want to ever mix one type of component (AWT) with the other (Swing) in the same app unless you have a real good reason and really know what you are doing.

    keylisteners are what you are currently using to trap keystrokes (arrow keystrokes), and work ok as long as the component that is being listened to has the focus. You've learned this as when your buttons stole the focus from the panel, the keylisteners stopped working. Key Binding is a higher level of listener type idea that won't work with your program as it only works with Swing, not AWT. One advantage of key bindings is that the component that uses them doesn't have to have the direct focus.

  9. #9
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    ahhhh i got it...

    we may be learning AWT because our teacher is in his early 30s so he probably learned it in school when it was semi new.

    im in highschool so what i learn in class is about all i get. not much of a choice on my part. but if i ever decide to read up on java ill read alot more in to swing.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm 50, am a progamming hobbiest, and although I learned AWT in the past, I have moved on when it was time to move on. I suggest that your teacher do the same. ;)

  11. #11
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    im 15 and i wana learn it all...

    i love computers. always have. its my 2nd love right after skating.

    after i get more practice in on java and hopefully more C++ i want to learn more about the language that iphones/itouches use.

    also.. i need to learn more about DB creation and mysql and all that stuff.

Similar Threads

  1. Double click radio button
    By mandragora in forum AWT / Swing
    Replies: 11
    Last Post: 11-10-2008, 11:06 AM
  2. Warning: Page has Expired on click of back button
    By bhupendra in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-27-2008, 07:42 PM
  3. Swing - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:03 PM
  4. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:02 PM
  5. How to perform some event to button click
    By eva in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 12:27 AM

Posting Permissions

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