Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Button stops after 1 click

    Hi i have an upcomming JAva exame and im strugling with a problem,

    i have 4 classes : BarApp (Starting class), Glas(Glas on the bar), BarPanel(paints the bar and the glass on it),ButtonPanel(puts buttons on panel)

    Now my problem is i have a code in my ButtonPanel where i redirect an action to the liquid inside the glass this liquid drains -10 pixels evry click, now the problem is it only works once and then stops.

    Here is the code for that part:

    if(e.getSource() == drinkButton)
    {
    glas.setLiquid(10);
    }

    Now when i click it it drains 10 pixels of liquid but then it doenst drain anymore.
    Does any1 know where i should add something else or work with an integer or something ?
    Probably an easy fix but i cant find it:P

    PS:exuse my bad grammar.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I cannot guess from what you have posted above the real reason of the problem, however there is something not accurate in what you have posted. You are comparing objects with the == signs. the == compares values not instances of objects.

    You should use the equals method to compare objects.

    Try this instead:
    Code:
      if(e.getSource().equals(drinkButton))
    {
    glas.setLiquid(10);
    }
    Let me know what happened.
    If it does not work, it is better if you posted your code so we can help you correct the problem.
    Software and cathedrals are much the same - first we build them, then we pray.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No, that's not correct. == compares object instances for identical, not for values. Values you would use .equals, == compares the same object pointers. Using e.getSource() pulls the component triggering the event e, which in this case is the drinkButton (or so it appears to be). So the use of e.getSource() == drinkButton is correct.

    Without seeing glas, its likely that the .setLiquid method simply sets the value associated, not subtracting it. If that's the case, then you want to simply use glas.setLiquid(glas.getLiquid() - 10); to decrement its size by 10. Assuming you have a getLiquid method as well of course. You can always write a new method called reduceLiquid in glas that does this for you.

  • Users who have thanked Fou-Lu for this post:

    MrMurda (03-19-2012)

  • #4
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Still not working

    Thanks for your post. The code didnt work so il post my code now its in dutch so il JavaDoc some things.
    /** this is the BarPanel class
    @int inhoud This is the inside of the glass
    @int drinken This is the sort of drink thats inside the glass cola,orange etc
    **/
    Code:
    public class BarPaneel extends JPanel
    {
    		private int drinken;
    	private int inhoud;
    	
    	public int getInhoud() {
    		return inhoud;
    	}
    
    	public void setInhoud(int inhoud) {
    		this.inhoud = inhoud;
    	}
    
    
    
    	public void paintComponent(Graphics g)
    	{
    	
    super.paintComponent(g);
    		
    		glas.setInhoud(inhoud);
    		glas.teken(g);
    	}
    	
    	
    }
    /** this is the ButtonPanel
    inside here i put in the 'drink' button this gifes the int of the .fillRect() for the y-possition -10, so its looks like the glass is emptying.
    **/
    Code:
    public void actionPerformed(ActionEvent e) 
    	{
    		
    		  if(e.getSource().equals(drinkKnop))
    		  {
    		  glas.setInhoud(10);
    		  
    		  }
    		glas.repaint();
    	}
    	
    	
    }
    /**
    This is the Glass Class where i draw the glas and inside liquid and gife them variables
    **/
    Code:
    public class Glas
    {
    	private int GlasyPos;
    	private Color kleur;
    	private int inhoud;
    	
    	private int drinkenStatus;
    
    	public int getInhoud() {
    		return inhoud;
    	}
    
    	public void setInhoud(int inhoud) {
    		this.inhoud = inhoud;
    	}
    
    
    
    	public int getGlasyPos() {
    		return GlasyPos;
    	}
    
    
    
    	public void setGlasyPos(int glasyPos) {
    		GlasyPos = glasyPos;
    	}
    
    
    	public void teken (Graphics g)
    	{
    		
    		
    		//glas
    		g.setColor(Color.WHITE);
    		g.fillRect(GlasxPos,GlasyPos,GlasBreedte,GlasHoogte);
    		//glashandvat
    		g.fillRect(GlasxPos-40, GlasyPos+20, 40, 20);
    		g.fillRect(GlasxPos-40, GlasyPos+20, 20,70);
    		
    		//inhoud
    		if(drinkenStatus ==0)
    		{
    			kleur = Color.BLACK;
    		}
    		if(drinkenStatus ==1)
    		{
    			kleur = Color.YELLOW;
    		}
    		if(drinkenStatus ==2)
    		{
    			kleur = Color.pink;
    		}
    		g.setColor(kleur);
    		g.fillRect(GlasxPos +5, GlasyPos +30 +inhoud, GlasBreedte-10, GlasHoogte -40- inhoud);
    		
    	}
    
    }
    Hope i gave enough info for you to look at.
    Thanks,

  • #5
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Fou Lu is absolutely right. I apologize for this dummy mistake.

    In all cases I want to MrMurda if this code is compiling and if it is him who wrote it.

    Example of errors:

    this line
    Code:
    g.fillRect(GlasxPos,GlasyPos,GlasBreedte,GlasHoogte);
    points to non existing variables GlasxPos, GlasBreedte and GlasHoogte.

    So three errors in one line.

    Kindly attach your files. I am starting to doubt that it ran with you.
    Software and cathedrals are much the same - first we build them, then we pray.

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ckeyrouz i dont understand what you mean?
    Do you mean this code is not mine ? i did not put all the codes in their cause i have allot of script for the bar and collors and all.
    I apriciate your help verry much though.

    FOU LU THANK YOU VM the code worked^^
    i want to tell myself duuhhh but you did it for me the code works great!
    now i can move on with my assignment.


  •  

    Posting Permissions

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