2

I am trying to create a midlet with which I can draw anything using my cursor keys. I have used drawLine method. But I want to create a line then change direction keeping the previous line but the line keeps rotating.

Basically I should be able to draw anything in any direction.

This is my code:

public class Pacer extends MIDlet{
public void startApp() {
Displayable d = new PacerCanvas();

d.addCommand(new Command("Exit", Command.EXIT, 0));
d.setCommandListener(new CommandListener() {
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
} );

Display.getDisplay(this).setCurrent(d);
 }

public void pauseApp() { }

public void destroyApp(boolean unconditional) { }
}
/**
*
* @author Rumman
*/
import javax.microedition.lcdui.*;

public class PacerCanvas extends Canvas {

private String name;
private int w,h,x1,y1,x2,y2;

public PacerCanvas(){
w = getWidth();
h = getHeight();
x1 =  w/2 ;
y1 = h/2 ;
x2 = x1 ;
y2 = y1 ;
}

protected void keyPressed(int key){
    name = getKeyName(key);
    if(name.equals("Right") || name.equals("6")){
    x2++;
    }
    else if(name.equals("Left") || name.equals("4")){
    x2--;
    }
    else if(name.equals("Up") || name.equals("2")){
    y2--;
    }
    else if(name.equals("Down") || name.equals("8")){
    y2++;
    }
    else if(name.equals("1")){
        x2--;
        y2++;
    }
    else if(name.equals("3")){
        x2++;
        y2++;
    }
    else if(name.equals("7")){

    }
    else if(name.equals("9")){
    }
    repaint();  
}

public void paint(Graphics g) {

g.setColor(0xffffff);
g.fillRect(0, 0, w, h);

g.setColor(0x000000);
g.drawLine(x1, y1, x2, y2);
}
}
gnat
  • 6,213
  • 108
  • 53
  • 73
rumman0786
  • 1,150
  • 10
  • 20

2 Answers2

1

your code erases the screen then draws one line every time it repaints.

(Graphics.fillRect(0,0,width,length) in paint() erases the screen)

the line always starts in the center of the screen because x1 and y1 never change.

user input only changes the end point of the line.

this is not what you seem to want to do.

there are 2 ways of doing what you ask:
- remember every user input (in a vector), clear the screen and redraw all the lines every time you repaint. not exactly efficient from a memory or flickering perspective.
- stop erasing the screen and just draw a new line for each user input.

in both cases, you probably want to do x1=x2;y1=y2; after each call to drawLine(); in order to move the origin of the next line to the end of the previous line.

I also think you want to implement keyRepeated() in order to improve user experience.

You can also change the clip (using Graphics.setClip()) when you repaint so the phone only updates a small part of the screen in order to improve performance.

If you want to use double-buffering (keep one Image in memory, draw to it, then print it on the screen), you still need to stop erasing the Image (with fillRect()) every time you want to draw to it.

In order to use double buffering, you need Image.getGraphics() and Graphics.drawImage()

michael aubert
  • 6,836
  • 1
  • 16
  • 32
  • Thank you for your reply,I am having problem in implementing what you said. 1. you said -"stop erasing the screen and just draw a new line for each user input." Now how do I stop erasing the screen ? and how may i save it ? I am using keyPressed() and now as you suggested keyreapeated() and without repainting how can I draw a newLine keeping the previous one ? 2.someone suggested me to draw on an image and save it , and next overwrite on that image....I am not succeding in doing that. – rumman0786 Mar 24 '12 at 08:21
  • 3. x1=x2;y1=y2; I understand this and also did what u told me to do but the problem is as i cannot save my previous drawings in the screen it always gets deleted and turn into a pointer/mouse. Here is my modified code... can you please show me how i can implement what i am trying to do? – rumman0786 Mar 24 '12 at 08:35
  • answer updated for 1 and 2. for 3, you didn't add the new code in the correct place. read the answer again. – michael aubert Mar 26 '12 at 02:49
  • Thank you, @QuickRecipesOnSymbianOS for your help. I did what i wanted to do. My code is given below as an answer. – rumman0786 Mar 30 '12 at 10:05
1
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Rumman
*/
import javax.microedition.lcdui.*;

public class PacerCanvas extends Canvas {

private String name;
private int w,h,x1,y1,x2,y2;
private Image image;
private Graphics ig;
public PacerCanvas(){
w = getWidth();
h = getHeight();
x1 =  w/2 ;
y1 = h/2 ;
x2 = x1 ;
y2 = y1 ;
image = Image.createImage(w,h);

ig = image.getGraphics();

ig.setColor(255, 255, 255);
ig.fillRect(0, 0, w, h);

}
protected void keyRepeated(int key){
keyPressed(key);
keyPressed(key);// I used this method multiple times as it makes it faster 
keyPressed(key);
keyPressed(key);
keyPressed(key);
keyPressed(key);
keyPressed(key);
keyPressed(key);
}
protected void keyPressed(int key){
    name = getKeyName(key);
    if(name.equals("Right") || name.equals("6")){
    x2++;
    }
    else if(name.equals("Left") || name.equals("4")){
    x2--;
    }
    else if(name.equals("Up") || name.equals("2")){
    y2--;
    }
    else if(name.equals("Down") || name.equals("8")){
    y2++;
    }
    else if(name.equals("1")){
        x2--;
        y2--;
    }
    else if(name.equals("3")){
        x2++;
        y2--;
    }
    else if(name.equals("7")){
        x2--;
        y2++;
    }
    else if(name.equals("9")){
        x2++;
        y2++;
    }
    repaint();  
}

public void paint(Graphics g) {


ig.setColor(0x000000);
ig.drawLine(x1, y1, x2, y2);
g.drawImage(image, 0, 0, Graphics.TOP|Graphics.LEFT);
x1=x2;
y1=y2;


}

public void draw(Image i) {
Graphics ig = i.getGraphics();
Pacer p = new Pacer();
// p.Display.getDisplay(this).setCurrent(i);
}
}
rumman0786
  • 1,150
  • 10
  • 20