Results 1 to 11 of 11
  1. #1
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default problem pressing buttons

    when i press button 2 it the program doesn't response please help

    thank you

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class AnAppletWithButtons extends Applet implements ActionListener
    {
    	Button button1, button2;
    	int r, g , b;
    	Color color;
    	boolean drawLine = false;
    	boolean fillOval = false;
    
    	public void init()
    	{
    		String parmStringRED =  getParameter("red");
    		 r = Integer.parseInt(parmStringRED);
    		String parmStringGREEN =  getParameter("green");
    		 g = Integer.parseInt(parmStringGREEN);
    		String parmStringBLUE =  getParameter("blue");
    		 b = Integer.parseInt(parmStringBLUE);
    	 	color = new Color (r,g,b);
    		button1 = new Button("Button 1");
    		add(button1);
    		button1.addActionListener(this);
    
    		button2 = new Button("Button 2");
    		add(button2);
    		button2.addActionListener(this);
    		button1.setForeground(color);
    		button2.setForeground(color);
    
    		button1.setBackground(Color.yellow);
    		button2.setBackground(Color.yellow);
    
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    
    		if (e.getSource() == button1){
    			System.out.println("Button 1 was pressed");
    			drawLine = true;
    
    			}
    		else if (e.getSource() == button2){
    			System.out.println("Button 2 was pressed");
    	        fillOval = true;
    
    		}
            repaint();
    	}
    	public void paint(Graphics g)
    	{
    		super.paint(g);   // for the background
    		if(drawLine)
    			g.drawLine(0, 0, 400, 400);
    		else if(fillOval)
    			g.fillOval(10, 10, 390, 390);
    	}
    }

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

    Default Re: problem pressing buttons

    Button 2 works fine. Your problem is your logic in your paint method. Walk through the program in your mind and think what will happen if button 1 is pressed and then button 2. Concentrate on the state of your two boolean variables to see what effect they should have, and you'll see the problem and its solution.

    Hint 2: Specifically, what will happen here if drawLine is true and repaint() is called, and then if fillOval is true and repaint() is called. Also, ask yourself, what happens to the drawLine boolean when fillOval is set to true?

    Java Code:
        public void paint(Graphics g)
        {
            super.paint(g);   // for the background
            if(drawLine)
                g.drawLine(0, 0, 400, 400);
            else if(fillOval)
                g.fillOval(10, 10, 390, 390);
        }
    }
    Last edited by Fubarable; 10-30-2011 at 12:56 AM.

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

    Default Re: problem pressing buttons

    As you can guess, I'm trying to get you to discover the problem and its solution. When you see it, you'll likely slap your head and go "D'oh!". If you're still stuck, just let me know, but I have confidence you yourself can solve this.

  4. #4
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: problem pressing buttons

    Sorry i am going crazy and i cant see my mistake

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: problem pressing buttons

    Looking at code won't always help.

    I always find a good way to solve problems is to add System.out.println(...) statements. In this case you would want to see what the values of the "drawLine" and "fillOval" values are. Then you can better understand the logic flow.

  6. #6
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: problem pressing buttons

    Camicker, i followed your advise

    Java Code:
    	public void paint(Graphics g)
    	{
    		super.paint(g);   // for the background
    		if(drawLine){
    		System.out.println(1);
    			g.drawLine(0, 0, 400, 400);}
    		else if(fillOval){
    			g.fillOval(10, 10, 390, 390);
    			System.out.println(2);}
    
    	}
    and it seems like fillOval is never called

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

    Default Re: problem pressing buttons

    If fillOval is true and drawLine is true (which can easily happen if you press draw line button first), will the paint method call g.fillOval(10, 10, 390, 390);? Look carefully at the if/else block, and you'll see.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: problem pressing buttons

    Camicker, i followed your advise
    I suggested you print out the value of the variables you are testing.

  9. #9
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: problem pressing buttons

    my value is always true

  10. #10
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: problem pressing buttons

    ok this way it works, but is therw a better way /

    thank you guys

    Java Code:
    	public void paint(Graphics g)
    	{
    		super.paint(g);   // for the background
    		if(drawLine){
    		drawLine = false;
    			g.drawLine(0, 0, 400, 400);}
    		else if(fillOval){
    			drawLine = false;
    			g.fillOval(10, 10, 390, 390);
    			}
    
    	}

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

    Default Re: problem pressing buttons

    Quote Originally Posted by mike28 View Post
    ok this way it works, but is therw a better way /

    thank you guys

    Java Code:
    	public void paint(Graphics g)
    	{
    		super.paint(g);   // for the background
    		if(drawLine){
    		drawLine = false;
    			g.drawLine(0, 0, 400, 400);}
    		else if(fillOval){
    			drawLine = false;
    			g.fillOval(10, 10, 390, 390);
    			}
    
    	}
    Yes, either:
    • If you want to show either a line or an oval but not both, then use only one boolean variable and get rid of the second if. In other words, change else if to just else.
    • Or you could set both booleans in the actionEvent blocks, setting one to true the other to false and visa versa, but that's too prone to error (as you're finding out)
    • Or if it's OK to show both or neither, get rid of the else and change else if to simply if, and toggle the booleans in the actionPerformed method.


    But the most important thing is to fully think through the logic of what you want to do before committing code to computer.

Similar Threads

  1. Buttons Problem
    By Coold0wn in forum New To Java
    Replies: 3
    Last Post: 08-25-2011, 07:02 PM
  2. Renaming Buttons Problem
    By Huskies in forum New To Java
    Replies: 11
    Last Post: 07-29-2011, 01:53 AM
  3. Draw shapes by pressing a button
    By SWEngineer in forum AWT / Swing
    Replies: 16
    Last Post: 05-14-2010, 06:26 AM
  4. Focusing the JTextField and so on while pressing Tab
    By britto_bicsjohn in forum AWT / Swing
    Replies: 1
    Last Post: 08-28-2009, 09:24 AM
  5. [B]Tab Sequence problem with radio buttons[/B]
    By shobha2k8 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-25-2008, 08:25 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
  •