• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anyone used Canvas + graphics ?

 
Naveen Sharma
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i m trying to draw a string at some random position on the screen
i have extended the Canvas class and overided the paint()
now say on some clicks i am generating a random x,y positions.. and drawing a string using drawString() of Graphics class
evrything works fine,
but i want the previously written string should be cleand up... and each time the text should is written at new x,y location, it should not messed up with previous written
i have called the repaint() of graphics class to repaint the text at new location... but as i donot find any update() in canvas class... how to achieve this
any ideas?
Thanks
Naveen
 
Naveen Sharma
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprised no one is having ANY idea about that
 
Xiao Qinglai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you should stroe the (x, y) of your latest panited String; Whenever you are painting the nextString, repaint both area of oldString and newString.
like this:
repaint(xOfOldString, yOfOldString, widthOfOldString, heightOfOldString);
repaint(xOfNewString, yOfNewString, widthOfNewString, heightOfNewString);
 
Naveen Sharma
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
as Xiao Qinglai said use repaint(int,int,int,int) instead of repaint()..
i have written the following code, my aim is to make a toggle,
when we press key "3" it should write the string..
and when we press key "9" it should delete the string from canvas...
please tell me where things are wrong
Thanks
Navi
//------ Following is the code


//--------Code ends here
 
Xiao Qinglai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
plz pay attention to this method:
/************paint() of CanvasArea************/
public void paint(Graphics g){g.setColor(255, 0, 0); if(toDraw)g.drawString(txt,50,100, Graphics.TOP | Graphics.HCENTER);}
/*****************end**************************/
if toDraw is true, paint txt to screen;
if toDraw is false, do nothing --- instead, you should do this:
/*************start********/
Font f = g.getFont();
g.setColor(0x00FFFFFF);
g.fillRect(50, 100, f.stringWidth(txt), f.getHeight());
/********end**********/
good luck
Regards,
XQL
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic