×

Discussion Board

Results 1 to 3 of 3

Hybrid View

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    37

    make an Image move over the display

    hi
    I want to display a 300x300 px image into the canvas and then move it. I used this code, it works well, but I have a problem about the canvas size limit:

    import javax.microedition.lcdui.*;
    public class BigImage extends Canvas implements CommandListener {
    private Image image1;
    private int canvasY = getHeight();
    private int canvasX = getWidth();
    private int locateY = canvasY / 2;
    private int locateX = canvasX / 2;
    private int x = 0;
    private int y = 0;
    public BigImage() {
    try {
    image1 = Image.createImage("/BigImage.png");
    }
    catch (Exception e) {}
    try {
    jbInit();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    }
    private void jbInit() throws Exception {
    setCommandListener(this);
    addCommand(new Command("Exit", Command.EXIT, 1));
    }
    public void commandAction(Command command, Displayable displayable) {
    }
    protected void paint(Graphics g) {
    g.drawImage(image1,locateX + x,locateY + y,Graphics.HCENTER|Graphics.VCENTER);
    }
    protected void keyPressed (int keyCode) {
    int action = getGameAction(keyCode);
    switch (action) {
    case Canvas.RIGHT:
    x = x + 10;
    repaint();
    break;
    case Canvas.LEFT:
    x = x - 10;
    repaint();
    break;
    case Canvas.UP:
    y = y - 10;
    repaint();
    break;
    case Canvas.DOWN:
    y = y + 10;
    repaint();
    break;
    }
    }
    }

    I used the image center as the anchor point. variable x , y are used as image current position.
    I don't know how to set image's corners as displayble position limit.
    I mean the image must always cover all the canvas!
    can you help me? code samples are welcome.
    thanks

    eml

  2. #2
    Registered User
    Join Date
    Apr 2003
    Location
    Russia
    Posts
    34
    hi, try this code

    import javax.microedition.lcdui.*;

    public class Screen
    extends Canvas
    implements CommandListener {

    private final int height = getHeight();
    private final int width = getWidth();
    private final int xOffset = 0;
    private final int yOffset = 0;
    private Image image;
    private int maxX = 0;
    private int maxY = 0;
    private int x = 0;
    private int y = 0;
    private int speed = 5;
    Command exit;


    public Screen() {
    try {
    image = Image.createImage("/BigImage.png");
    }
    catch (Exception e) {}
    maxX = width - image.getWidth();
    maxY = height - image.getHeight();
    exit = new Command("Exit", Command.EXIT, 1);
    addCommand(exit);
    setCommandListener(this);
    }

    protected void paint(Graphics g) {
    int anchor = Graphics.LEFT | Graphics.TOP;
    g.drawImage(image, x, y, anchor);

    }

    private void moveImage(int direction) {
    switch (direction) {
    case 1:

    if (x + speed <= xOffset) {
    x+=speed;
    }
    break;
    case 2:
    if (y + speed <= yOffset) {
    y+=speed;
    }
    break;
    case 3:
    if (x - speed >= maxX) {
    x-=speed;
    }
    break;
    case 4:
    if (y - speed >= maxY) {
    y-=speed;
    }
    break;
    }
    repaint();
    }

    public void commandAction(Command command, Displayable displayable) {
    }

    protected void keyPressed(int keyCode) {
    int action = getGameAction(keyCode);
    switch (action) {
    case Canvas.RIGHT:
    moveImage(1);
    break;
    case Canvas.LEFT:
    moveImage(3);
    break;
    case Canvas.UP:
    moveImage(4);
    break;
    case Canvas.DOWN:
    moveImage(2);
    break;
    }

    }
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    37
    thank you very much lizergil

    your code is strong and works well

    regards

    eml

Posting Permissions

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