You Are Here:

Community: Developer Discussion Boards

#1 Old how to put pointer on canvas - 2006-07-27, 10:12

Join Date: Jul 2006
Posts: 33
mshouab
Offline
Registered User
i want to put a pointer on the canvas which is used for getting the x,y coordinates so that i can paste the image on the required coordinates this pointer should be handled through arrow keys how is it possible in canvas.the pinter can be of any shape.

Regards,
Muhammad Shouab.
Reply With Quote

#2 Old Re: how to put pointer on canvas - 2006-07-28, 00:50

Join Date: Dec 2005
Posts: 1,696
Location: Europe/Poland/Warsaw
peterblazejewicz
Offline
Super Contributor
hi Muhammad,

pointer can be drawn or can be custom png-based image, so it really could be anything,

here is basic, rudimentary, not optimized sample:

Code:
package tests;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GameKeysTest extends MIDlet implements CommandListener {
    public void startApp() {
        Displayable current = Display.getDisplay(this).getCurrent();
        if(current == null){
            current = new MyCanvas();
            current.addCommand(new Command("Quit", Command.EXIT, 1));
            current.setCommandListener(this);
        }
        Display.getDisplay(this).setCurrent(current);
    }
    
    public void pauseApp() { 
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command command, Displayable displayable) {
        if(command.getCommandType() == Command.EXIT){
            Display.getDisplay(this).setCurrent(null);
            notifyDestroyed();
        }
    }

    class MyCanvas extends Canvas {
        int w;
        int h;
        Point pointer;
        public MyCanvas(){
            setTitle("EXAMPLE");
            setFullScreenMode(true);
            w = getWidth();
            h = getHeight();
            pointer = new Point(w/2, h/2);
        }
        protected  void keyPressed(int keyCode) {
            int gameKey = getGameAction(keyCode);
            int x = pointer.getX();
            int y = pointer.getY();
            switch(gameKey){
                case UP:
                    if(y>0){
                        pointer.setY(--y);
                        repaint();
                    }
                    break;
                case DOWN:
                    if(y<w){
                        pointer.setY(++y);
                        repaint();
                    }
                    break;
                case LEFT:
                    if(x>0){
                        pointer.setX(--x);
                        repaint();
                    }
                    break;
                case RIGHT:
                    if(x<w){
                        pointer.setX(++x);
                        repaint();
                    }
                    break;
                default:
                    // break through //
            }
        }
        protected  void keyRepeated(int keyCode) {
            keyReleased(keyCode);
        }
        protected void sizeChanged(int aWidth, int aHeight) {
            w = aWidth;
            h = aHeight;
            repaint();
        }
        protected void paint(Graphics g) {
            int x = pointer.getX();
            int y = pointer.getY();
            g.setColor(0xFFFFFFFF);
            g.fillRect(0, 0, w, h);
            g.setColor(0x00000000);
            g.drawLine(x-3, y, x-1, y);
            g.drawLine(x+1, y, x+3, y);
            g.drawLine(x, y-3, x, y-1);
            g.drawLine(x, y+1, x, y+3);           
        }
        
    }
    
    class Point{
        int x;
        int y;
        public Point(){
            x = 0;
            y = 0;
        }
        public Point(int x, int y){
            this.x = x;
            this.y = y;
        }
        public int getX(){
            return x;
        }
        public void setX(int value){
            x = value;
        }
        public int getY(){
            return y;
        }
        public void setY(int value){
            y = value;
        }
    }
}
hth,
regards,
Peter
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a partly transparent Canvas to lay on top of another Canvas? mkleijer Mobile Java Media (Graphics & Sounds) 4 2007-05-22 08:00
Canvas doesn't show when getWidth/getHeight is used claudio4j Mobile Java Media (Graphics & Sounds) 2 2003-11-05 09:41
Linux driver in RedHat 7.3 nsolhjoo Multimodecards 0 2003-10-24 18:04
Major problem with delegation to Canvas bartekn Mobile Java Media (Graphics & Sounds) 8 2003-10-08 18:46
7650 and 3650 Canvas and FullCanvas memory marcilgen Mobile Java General 5 2003-07-08 13:24

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d134434X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ