×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Regular Contributor
    Join Date
    Apr 2009
    Location
    Kuala Lumpur
    Posts
    98

    Question Paint, Draw Drag line in Canvas

    Hi guys,

    I've written this program and I think its correct but i don't know why after run there is no result.
    in this program i want, when i drag on touch screen, see the shape. but i couldn't see any thing.
    my phone is 5800 and I've written this program by Netbeans.

    My source code: http://uploading.com/files/add5241c/Doodle.zip/

    or look at this:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class Doodle extends MIDlet
    {
    private Display display;
    private DoodleCanvas canvas;

    public Doodle()
    {
    display = Display.getDisplay(this);
    canvas = new DoodleCanvas(this);
    }

    protected void startApp()
    {
    display.setCurrent(canvas);
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    }

    public void exitMIDlet()
    {
    destroyApp(true);
    notifyDestroyed();
    }
    }
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class DoodleCanvas extends Canvas implements CommandListener
    {
    private Command cmExit;
    private Command cmClear;
    private int startx = 0;
    private int starty = 0;
    private int currentx = 0;
    private int currenty = 0;
    private Doodle midlet;
    private boolean clearDisplay = false;
    private String keyText = null;

    public DoodleCanvas(Doodle midlet)
    {
    this.midlet = midlet;

    cmExit = new Command("Exit", Command.EXIT, 1);
    cmClear= new Command("Clear", Command.SCREEN, 1);
    addCommand(cmExit);
    addCommand(cmClear);
    setCommandListener(this);
    }

    protected void paint(Graphics g)
    {
    //Clear the bachground to white
    if(clearDisplay)
    {
    g.setColor(255, 255, 255);
    g.fillRect(0, 0, getWidth(), getHeight());

    clearDisplay = false;
    startx = currentx = starty = currenty = 0;

    return;
    }
    //Draw with black pen
    g.setColor(0, 0, 0);

    //Draw Line
    g.drawLine(startx, starty, currentx, currenty);

    if(keyText != null)
    g.drawString(keyText, 0, 0, Graphics.TOP | Graphics.HCENTER);

    //New starting point is current position
    startx = currentx;
    starty = currenty;
    }

    public void commandAction(Command c, Displayable d)
    {
    if(c == cmExit)
    midlet.exitMIDlet();
    else if (c == cmClear)
    {
    clearDisplay = true;
    repaint();
    }
    }

    protected void pointerPressed(int x, int y)
    {
    startx = x;
    starty = y;
    keyText ="Presed";
    }

    protected void pointerDeagged(int x, int y)
    {
    currentx = x;
    currenty = y;
    keyText ="Draging";
    repaint();
    }

    protected void pointerReleased(int x, int y)
    {
    keyText ="Released";
    }
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Re: Paint, Draw Drag line in Canvas

    I dont think there is any problem with the code. Point is that where r u trying to run this program on emulator or device. I have gone through ur code but everything is fine and good and it should work.

  3. #3
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: Paint, Draw Drag line in Canvas

    Are you getting blank screen ? No output ? Nothing is happening in paint() ?

    Quote Originally Posted by Hesam View Post
    Hi guys,

    I've written this program and I think its correct but i don't know why after run there is no result.
    in this program i want, when i drag on touch screen, see the shape. but i couldn't see any thing.
    my phone is 5800 and I've written this program by Netbeans.

    My source code: http://uploading.com/files/add5241c/Doodle.zip/

    or look at this:

  4. #4
    Regular Contributor
    Join Date
    Apr 2009
    Location
    Kuala Lumpur
    Posts
    98

    Re: Paint, Draw Drag line in Canvas

    Quote Originally Posted by honest_success View Post
    Are you getting blank screen ? No output ? Nothing is happening in paint() ?
    Thanks dear friends,

    Unfortunately, I don't have any result or output. Only blank page.

    I thought that, maybe its because of simulator (net beans touch), then I installed jar file on my mobile but result was the same. Blank and nothing on output or screen.

  5. #5
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: Paint, Draw Drag line in Canvas

    Quote Originally Posted by Hesam View Post
    Thanks dear friends,

    Unfortunately, I don't have any result or output. Only blank page.

    I thought that, maybe its because of simulator (net beans touch), then I installed jar file on my mobile but result was the same. Blank and nothing on output or screen.
    In canvas we have to draw rectangle before writing or drawing something on screen. In clearDisplay you are drawing rectangle using fillRect(). So check that it is at least executing once. and check that you have set the color well. if your background and text color is same then it will not display any thing.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    45

    Smile Re: Paint, Draw Drag line in Canvas

    In canvas we have to draw rectangle before writing or drawing something on screen. In clearDisplay you are drawing rectangle using fillRect(). So check that it is at least executing once. and check that you have set the color well. if your background and text color is same then it will not display any thing.
    Hello honest_success

    Actually here with the help of fillRect() he is trying to fill the rectangular portion of the emulator with the white color. And also it is not necessary to always draw rectangle we can fill the rectangle with white color and make the screen blank and this what he has done here in the code mentioned.

    Also hesam please go through various other sample code of pointer events and check whether they r running on the device or not. Actually same prog i tried to implemented but the result was as that of u. So pls check another simple sample codes and test it whether it is running or not.

  7. #7
    Regular Contributor
    Join Date
    Dec 2008
    Posts
    58

    Re: Paint, Draw Drag line in Canvas

    I just used that code in my Midlet and it worked well... im using the same 5800.



    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class Midlet extends MIDlet
    {
      private Display  display;       // The display
      private DoodleCanvas canvas;   // Canvas
    
      public Midlet()
      {
        display = Display.getDisplay(this);
        canvas  = new DoodleCanvas(this);
      }
    
      protected void startApp()
      {
        display.setCurrent( canvas );
      }
    
      protected void pauseApp()
      { }
    
      protected void destroyApp( boolean unconditional )
      { }
    
      public void exitMIDlet()
      {
        destroyApp(true);
        notifyDestroyed();
      }
    }
    
    /*--------------------------------------------------
    * Class DoodleCanvas
    *
    * Pointer event handling
    *-------------------------------------------------*/
    class DoodleCanvas extends Canvas implements CommandListener
    {
      private Command cmExit;          // Exit midlet
    
      private Command cmClear;         // Clear display
    
      private int startx = 0,   // Where pointer was clicked
    
                  starty = 0,
    
                  currentx = 0, // Current location
    
                  currenty = 0;
      private Midlet midlet;
      private boolean clearDisplay = false;
    
      /*--------------------------------------------------
      * Constructor
      *-------------------------------------------------*/
      public DoodleCanvas(Midlet midlet)
      {
        this.midlet = midlet;
    
        // Create exit command & listen for events
        cmExit = new Command("Exit", Command.EXIT, 1);
        cmClear = new Command("Clear", Command.SCREEN, 1);
        addCommand(cmExit);
        addCommand(cmClear);
        setCommandListener(this);
      }
    
      /*--------------------------------------------------
      * Paint the text representing the key code
      *-------------------------------------------------*/
      protected void paint(Graphics g)
      {
        // Clear the background (to white)
        if (clearDisplay)
        {
          g.setColor(255, 255, 255);
          g.fillRect(0, 0, getWidth(), getHeight());
    
          clearDisplay = false;
          startx = currentx = starty = currenty = 0;
    
          return;
        }
    
        // Draw with black pen
        g.setColor(0, 0, 0);
    
        // Draw line
        g.drawLine(startx, starty, currentx, currenty);
    
        // New starting point is the current position
        startx = currentx;
        starty = currenty;
      }
    
      /*--------------------------------------------------
      * Command event handling
      *-------------------------------------------------*/
      public void commandAction(Command c, Displayable d)
      {
        if (c == cmExit)
          midlet.exitMIDlet();
        else if (c == cmClear)
        {
          clearDisplay = true;
          repaint();
        }
      }
    
      /*--------------------------------------------------
      * Pointer pressed
      *-------------------------------------------------*/
      protected void pointerPressed(int x, int y)
      {
        startx = x;
        starty = y;
      }
    
      /*--------------------------------------------------
      * Pointer moved
      *-------------------------------------------------*/
      protected void pointerDragged(int x, int y)
      {
        currentx = x;
        currenty = y;
        repaint();
      }
    }

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Paint, Draw Drag line in Canvas

    You're only painting the last line in the paint method, and that's a bad plan. You can't make any assumptions about what's on the screen already when a paint happens. Remember that paint() is also called when you application has been in the background (say, there was a phone call), and has come back to the foreground. You must paint everything every time in the paint() method.

    The paint() method should never set any non-local variables. This will cause all kinds of problems if paint() happens at times when you don't expect it to.

    PHP Code:
    import java.util.Vector;
    import java.util.Enumeration;
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.midlet.MIDlet;

    public class 
    Doodle extends MIDlet implements CommandListener {
        private 
    Canvas canvas;
        
        public 
    void startApp() {
            if (
    canvas == null) {
                
    canvas = new DoodleCanvas();
                
    canvas.addCommand(new Command("Exit"Command.EXIT, 0));
                
    canvas.setCommandListener(this);
            }
            
    Display.getDisplay(this).setCurrent(canvas);
        }

        public 
    void pauseApp() {
        }

        public 
    void destroyApp(boolean always) {
        }

        public 
    void commandAction(Command cDisplayable d) {
            if (
    c.getCommandType() == Command.EXIT) {
                
    notifyDestroyed();
            }
        }
    }

    class 
    Point {
        private 
    int px;
        private 
    int py;

        public 
    Point(int xint y) {
            
    px x;
            
    py y;
        }

        public 
    int getX() {
            return 
    px;
        }

        public 
    int getY() {
            return 
    py;
        }
    }

    class 
    DoodleCanvas extends Canvas {
        private 
    Vector lines = new Vector();
        private 
    Vector currentLine;
        
        protected 
    void paint(Graphics g) {
            
    g.setColor(0xffffff);
            
    g.fillRect(00getWidth(), getHeight());
            
    g.setColor(0x000000);
            
    Enumeration en lines.elements();
            while (
    en.hasMoreElements()) {
                
    drawLine(g, (Vectoren.nextElement());
            }
            if (
    currentLine != null) {
                
    drawLine(gcurrentLine);
            }
        }

        private static 
    void drawLine(Graphics gVector v) {
            
    Point last null;
            
    Enumeration en v.elements();
            while (
    en.hasMoreElements()) {
                
    Point p = (Pointen.nextElement();
                if (
    last != null) {
                    
    g.drawLine(last.getX(), last.getY(), p.getX(), p.getY());
                }
                
    last p;
            }
        }

        protected 
    void pointerPressed(int xint y) {
            
    currentLine = new Vector();
            
    currentLine.addElement(new Point(xy));
            
    repaint();
        }

        protected 
    void pointerDragged(int xint y) {
            
    currentLine.addElement(new Point(xy));
            
    repaint();
        }

        protected 
    void pointerReleased(int xint y) {
            
    currentLine.addElement(new Point(xy));
            
    lines.addElement(currentLine);
            
    currentLine null;
            
    repaint();
        }

    If you want to add "clear" functionality, simply reset "lines" and "currentLine" back to their original values, so that paint has nothing to paint.

    Graham.

  9. #9
    Regular Contributor
    Join Date
    Apr 2009
    Location
    Kuala Lumpur
    Posts
    98

    Re: Paint, Draw Drag line in Canvas

    Quote Originally Posted by frcc24 View Post
    I just used that code in my Midlet and it worked well... im using the same 5800.

    Thanks dear frcc24,

    I tested your code and it works. I can't understand why yours work while mine not work.
    thank you.


    Quote Originally Posted by grahamhughes View Post
    You're only painting the last line in the paint method, and that's a bad plan. You can't make any assumptions about what's on the screen already when a paint happens. Remember that paint() is also called when you application has been in the background (say, there was a phone call), and has come back to the foreground. You must paint everything every time in the paint() method.

    The paint() method should never set any non-local variables. This will cause all kinds of problems if paint() happens at times when you don't expect it to.

    If you want to add "clear" functionality, simply reset "lines" and "currentLine" back to their original values, so that paint has nothing to paint.

    Graham.
    Dear Graham thanks so much, your program is a little difficult for me. I should analyze it! ;-)
    thanks a lot

  10. #10
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Paint, Draw Drag line in Canvas

    Quote Originally Posted by Hesam View Post
    your program is a little difficult for me. I should analyze it! ;-)
    The important point is: with the paint() method, you must not assume that there is anything already on the screen. You must make sure that you paint every single pixel on the screen.

    I think you are assuming that the line you drew last time will still be there next time. And this just isn't true.

    There are other ways to do this. I have solved it be recording the start and end of each line (in a Vector), and redrawing all the lines each time. Or, you could create a "buffer" image, like:

    Code:
    private Image buffer = Image.createImage(screenWidth, screenHeight);
    Draw to that:

    Code:
    protected void pointerDragged(int x, int y) {
        Graphics g = buffer.getGraphics();
        g.drawLine(lastX, lastY, x, y);
        lastX = x;
        lastY = y;
        repaint();
    }
    then in paint(), draw the buffer to the screen:

    Code:
    protected void paint(Graphics g) {
        g.drawImage(buffer, 0, 0, Graphics.TOP | Graphics.LEFT);
    }
    This technique may cost more memory, but it is simple to use.

    The one problem you may find is that, as the screen rotates between landscape and portrait, the resolution changes, and the size of the buffer needs to change. You would need to cope with this situation.

    Graham.

Similar Threads

  1. playing aduio file when call is rejected.
    By santosh_sangewar in forum Symbian
    Replies: 6
    Last Post: 2009-06-12, 11:35
  2. Help needed
    By kuhnikuehnast in forum Symbian Tools & SDKs
    Replies: 20
    Last Post: 2009-03-31, 22:55
  3. compilation error: identifier redeclared
    By netra in forum Symbian
    Replies: 2
    Last Post: 2007-06-22, 10:42
  4. problem with codewarrior 5.2
    By mayankkedia in forum Symbian Tools & SDKs
    Replies: 3
    Last Post: 2004-09-27, 16:08

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×