...

View Full Version : Button stops after 1 click



MrMurda
03-19-2012, 05:14 PM
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.

ckeyrouz
03-19-2012, 05:31 PM
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:


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.

Fou-Lu
03-19-2012, 05:51 PM
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.

MrMurda
03-19-2012, 07:49 PM
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
**/

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.
**/

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
**/


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,

ckeyrouz
03-19-2012, 08:50 PM
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

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.

MrMurda
03-19-2012, 09:01 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum