• Post Reply Bookmark Topic Watch Topic
  • New Topic

moving a component with keys in UI

 
guru Khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ...
I want to have a component (or image) in my mobile screen that should be moved when i press left-arrow / right arrow keys. It should be something like a small bar having the capability to move left n right. Can anybody tell me what should I use for it, i mean a button or bar or something else?? Any expert idea, then plz help!!!
Raheel.
 
Paul Kelcey
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Conceptually, there's 2 APIs.
One API is for high-level widgets like Forms, Screens, Lists, Commands etc. The idea behind this API is that you DON'T have control over the look-and-feel. The implementation will control what it looks like so that it sits well with the existing user-interface of the device.
The other API is the low-level API. Basically, the low-level API is the canvas and graphics classes. You can also use mutable images for double buffering.
If you want to move something around on the screen then you need to write code using the low-level API. As an example, you might have the following classes...
- MyMIDlet (extends MIDlet)
- MyCanvas (extends Canvas)
In MyMIDlet in startApp you could implement the startApp method as...
<code>
public void startApp()
{
Display.getDisplay(this).setCurrent(new MyCanvas());
}
</code>
In MyCanvas you could have some members representing your object...
<code>
private int ballX = 20;
private int ballY = 20;
private int ballRadius = 10;
public void paint(Graphics g)
{
// clear screen (better to double buffer here but hey, this is a start)
g.setColor(0xFFFFFF);
g.fillRect(0, 0, getWidth(), getHeight());

// draw ball
g.fillArc(ballX, ballY, ballRadius, ballRadius, 0, 360);
}
public void keyPressed(int keyCode)
{
int gameKey = getGameKey(keyCode);
switch (gameKey)
{
case UP:
--ballY;
break;
case DOWN:
etc.
etc.
}
repaint();
}
I've not touched MIDP in about 6 months so you'll need to sanity check this.
PK
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!