Search code examples
java-medrawingmidplcdui

J2ME : How to save a line created by using drawline() and start again from the line's end point keeping the previous one?


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);
}
}

Solution

  • /*
    * 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);
    }
    }