Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie, struggling with update method

Status
Not open for further replies.

NiceButDim

Programmer
Feb 12, 2002
154
GB
I am having a problem related to the update method. In my applet users can select a box from a grid I have drawn. When they select a box I draw the box in red, that part is okay. When the user selects a second box, I want to clear the first and ‘select’ the second. This is where my problem lies.

My code basically say;

If(boxAlreadyDrawn == true) {
drawingMode = CLEAR_BOX;
repaint();
}

boxAlreadyDrawn = true;
drawingMode = DRAW_BOX;
repaint();


Within my Update method I check the value of the ‘drawingMode’ variable and clear or draw a box accordingly. What I am encountering is that within the Update method the variable drawingMode is never set to ‘CLEAR_BOX’. When I expect it to be ‘CLEAR_BOX’ it is instead set to ‘DRAW_BOX’.

If I comment out the last 3 lines of the above code then I do see ‘CLEAR_BOX’ coming through. I assume from this that the program isn’t waiting for the update method to complete before moving on with the next line of code in the calling method. Is this correct? and if so, is there anything I can do about it?

Thanks.
 
try this :
If(boxAlreadyDrawn == true) {
drawingMode = CLEAR_BOX;
repaint();
return;
}

boxAlreadyDrawn = true;
drawingMode = DRAW_BOX;
repaint();
Water is not bad as long as it stays out human body ;-)
 
Thanks for your reply.
Unfortunately what you're suggesting won't solve my problem.
 
I don't understand the code so. The code after the if bloc will be run everytime replacing the values set in the if by true and DRAW_BOX ???
Am I missing something ? Water is not bad as long as it stays out human body ;-)
 
The problem is simply this; following the call to repaint (line 2 of the above code) the update method is called. Within update(Graphics g) the value of drawingMode is 'DRAW_BOX' even though immediately prior to calling repaint I am setting it to 'CLEAR_BOX'.
 
The problem is simply this; following the call to repaint (line 3 of the above code) the update method is called. Within update(Graphics g) the value of drawingMode is 'DRAW_BOX' even though immediately prior to calling repaint I am setting it to 'CLEAR_BOX'.
 
So the code you gave is not full code, It misses some lines or the "update" method is call within "repaint" method? Water is not bad as long as it stays out human body ;-)
 
Sorry, but having the full code is irrelevant here. If I set the first line of code in update(Graphics g) to be;

System.out.println("Value of drawingMode = " + drawingMode);

I can see the variable is set to 'DRAW_BOX' despite the fact that I've just set it to 'CLEAR_BOX' !
 
So your problem may be on the scope of your "drawingMode" variable. I mean : Are you sure it's the same one that you use or did you redefine it ? Water is not bad as long as it stays out human body ;-)
 
Sorry, but you're not understanding me at all. Here is an example program that shows my 'problem'.

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

public class ani5 extends Applet implements Runnable, MouseListener {

static final int NEITHER = 0;
static final int DRAW_BOX1 = 1;
static final int DRAW_BOX2 = 2;

int drawingMode = NEITHER;

public void init() {
addMouseListener(this);
}

public void start() {
}

public void stop() {
}

public void run() {
}

public void paint(Graphics g) {
update(g);
}

public void update(Graphics g) {
g.setColor(Color.red);

if(drawingMode==DRAW_BOX1)
g.fillRect(10,10,50,50);
if(drawingMode==DRAW_BOX2)
g.fillRect(80,10,50,50);
}

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

public void mouseClicked (MouseEvent me) {
drawingMode = DRAW_BOX1;
repaint();
drawingMode = DRAW_BOX2;
repaint();
}
}



When the user clicks the mouse button it should (by my logic) draw 2 boxes, but it doesn't. It only draws the second, try it for yourself.

My question IS, how do I get Java to wait for the repaint to complete (I.E. the paint & update methods) before moving on to set drawingMode to DRAW_BOX2 ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top