How to fix this bug?
Assalamu alikom,
i'm doing a sample canvas to display 9 screen buttons - like 1..9 standard phone buttons - with pointer event; when moving the mouse pointer within any button area, the button color should be high-lighted (activated).
The problem is: when pointing to a button at the right or the bottom screen boundries, i got them not high lighted. (i.e. buttons: 1,2,4,5 highlighted well, the other buttons are acting in a strange way.)
here's my steps:
- define an array [9*4]: -> {x1,y1,x2,y2} for each button coordinates. (Note: space bet buttons =3 pixels... top, left & right screen margin = 5 pixels)
- in pointerPressed, pointerDragged & pointerReleased events i detect the button number i'm pointing to; key_num detects the button that should be high-lighted, or is equal to -1 if no key is focused, i call repaint after that
- in paint() method: i draw the highlighted button, then looping to draw other buttons - except the high-lighted one. i'm also attaching snapshots of the output.
& here's my code:
public class AnimationCanvas extends Canvas {
/** Creates a new instance of AnimationCanvas */
static int key_num= -1;
int total_width=this.getWidth();
int total_height=this.getHeight();
final int BT_W=(int)((total_width-6-10)/3); //button width
final int BT_H=(int)((total_height-20-6)/3); //button height
// buttons arr = {x1,y1,x2,y2) -> for each button (9 * 4 array)
int buttons_arr[][]={{5,5,5+BT_W,5+BT_H},{8+BT_W,5,8+(2*BT_W),5+BT_H},{11+(2*BT_W),5,(total_width-5),5+BT_H},
{5,8+BT_H,5+BT_W,8+(2*BT_H)},{8+BT_W,8+BT_H,8+(2*BT_W),8+(2*BT_H)},{11+(2*BT_W),8+BT_H,(total_width-5),8+(2*BT_H)},
{5,11+(2*BT_H),5+BT_W,(total_height-15)},{8+BT_W,11+(2*BT_H),8+(2*BT_W),(total_height-15)},{11+(2*BT_W),11+(2*BT_H),(total_width-5),(total_height-15)}};
//Font font;
// int banner_height, txt_width, x_txt;
//String banner_txt;
boolean right_to_left=true; // txt direction
boolean stopped=false;
boolean pressed=false;
//Command exit_command,options;
//Thread thr;
HelloMidlet midlet;
public AnimationCanvas(HelloMidlet mid) {
super();
midlet=mid;
}
private int getKeyNum(int x,int y)
{
for(int i=0;i<9;i++)
{
if((x>buttons_arr[i][0])&&(x<buttons_arr[i][2])&&(y>buttons_arr[i][1])&&(y<buttons_arr[i][3]))
return i;
}
return -1; //x,y doesn't belong to any button
}
private void handlePointer(int x,int y)
{
key_num=getKeyNum(x,y);
repaint();
}
protected void pointerPressed(int x, int y)
{
pressed=true;
handlePointer(x,y);
}
protected void pointerDragged(int x, int y)
{
pressed=false;
handlePointer(x,y);
}
protected void pointerReleased(int x, int y)
{
pressed=false;
handlePointer(x,y);
}
private void draw(Graphics g)
{
g.setColor(0,0,139);
g.fillRect(0,0,getWidth(),getHeight());
//////////// Draw the active button ///////////////////
if(key_num != -1)
{
g.setColor(255,255,255);
g.fillRect(buttons_arr[key_num][0],buttons_arr[key_num][1],buttons_arr[key_num][2],buttons_arr[key_num][3]);
}
///////////// Draw Buttons (1-9) ///////////////////////
for(int i=0;i<9;i++)
{
// draw Button[i]
if(i!=key_num){
g.setColor(255,255,255);
g.drawRect(buttons_arr[i][0],buttons_arr[i][1],buttons_arr[i][2],buttons_arr[i][3]);
g.setColor(67,110,238);
g.fillRect(buttons_arr[i][0]+1,buttons_arr[i][1]+1,buttons_arr[i][2]-1,buttons_arr[i][3]-1);
}
}
}
public void paint(Graphics g)
{
draw(g);
}
}
Hope someone'll help.
Thanks.
| Attachment | Size |
|---|---|
| output.zip | 112.03 KB |





