You Are Here:

Community: Developer Discussion Boards

#1 Old make an Image move over the display - 2003-04-19, 02:45

Join Date: Mar 2003
Posts: 37
nodevelop
Offline
Registered User
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
Reply With Quote

#2 Old 2003-04-21, 09:07

Join Date: Apr 2003
Posts: 34
Location: Russia
lizergil
Offline
Registered User
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;
}

}
}
Reply With Quote

#3 Old 2003-04-21, 14:54

Join Date: Mar 2003
Posts: 37
nodevelop
Offline
Registered User
thank you very much lizergil

your code is strong and works well

regards

eml
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

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