Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

i got into a unexplainable situtation pleas help  RSS feed

 
ks goh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void UpdateObstacle(int ReceiveX, int ReceiveY){
obstacleX = ReceiveX;
obstacleY = ReceiveY;
UpdateObstacle = true;
repaint();
}

hi the above function is run abt 4 times, and same images will be paint at 4 different location. but the overall graghic has only update my last location. the rest wasn't painted
i use debug option, step by step execute, it did execute repaint().

please help.... thank....
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you could show us the relevant "paint()" or "paintComponent()" method.
 
ks goh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there my paint method...
thanks

public void paint(Graphics g){
if(initial){
initial = false;
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
for(int x=0; x<XWidth; x++){
for(int y=0; y<YHeight; y++){
g.drawImage(Junctionimage,baseX+(x*50),baseY+(y*50),50,50,this);
g.drawImage(CarImage,baseX+(99*50),baseY+(((YHeight-1)*50) -(99*50)),50,50,this);
g.drawImage(ObstacleImage,15+(99*50),15+(((YHeight-1)*50) -(99*50)),20,20,this);
}
}
g.drawImage(CarImage,baseX+(99*50),baseY+(((YHeight-1)*50) -(99*50)),50,50,this);
g.drawImage(ObstacleImage,15+(99*50),15+(((YHeight-1)*50) -(99*50)),20,20,this);
}
if(UpdateCar){
g.drawImage(Junctionimage,baseX+(previousX*50),baseY+(((YHeight-1)*50) -(previousY*50)),50,50,this);
g.drawImage(CarImage,baseX+(currentX*50),baseY+(((YHeight-1)*50) -(currentY*50)),50,50,this);
UpdateCar = false;
}
if(UpdateObstacle){
g.drawImage(ObstacleImage,15+(obstacleX*50),15+(((YHeight-1)*50) -(obstacleY*50)),20,20,this);
UpdateObstacle = false;
}
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't a call to repaint the same thing like calling gc(), in that it is a "suggestion" for the "paint" to run, and that you have no guarantee that it will run immediately when you call repaint(), there for it is possible all four times the method is called, it is after all four have been called that paint() finally runs its four times with only the last values.

Mark
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, absolutely, Mark. I'm afraid I misunderstood the question.

So, ks goh, now the question is what you want to do, exactly. Do you want to paint all four images at the same time, or are you trying to do an animation?

I'm going to move this to our Swing/AWT forum for further discussion.
 
ks goh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i require is to paint the picture 4 time.
in the end i need to see 4 ObstacleImage at diff location....

but what i got was only 1.....
 
ks goh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got a idea....

so if i send it in a list of data and paint it,
that should work right... because i only call the repaint once....
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are making an animation. The way we usually do this in java is to use a javax.swing.Timer or a separate thread to cycle through repetitions of
  • sleep
  • alter variables such as currentX used in the paint code
  • call repaint to update the graphics

  •  
    Brian Cole
    Author
    Ranch Hand
    Posts: 935
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by ks goh:
    what i require is to paint the picture 4 time.
    in the end i need to see 4 ObstacleImage at diff location....

    but what i got was only 1.....


    If you want to see four of them, shouldn't
    you have four thingies and call repaint()
    once on each one, as opposed to having one
    thingie and calling repaint() four times?

    I would think so, but it's hard to tell since
    you don't provide us with many details.

    It's possible to use one object to paint to
    many different locations on the screen (in fact
    this is in essence what a ListCellRenderer does)
    but it's kind of a weird thing to do.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!