Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

How do i move a shape from one position to a next

I would like to move a shape on a button click.

 

Say I pressed s on my blackberry the shape moves left as i keep my hand on S it moves even further left

as I move my hand off S it stops as I press D it moves right in the same manor.

 

I am using the paint compnent and have drawn a rectangle. I'm guessing I will need an invlaidate to refresh the paint drawing but how do i continuly move the shapes coordinates?

Developer
lakshman9687
Posts: 243
Registered: ‎07-22-2011
My Device: BlackBerry Z10

Re: How do i move a shape from one position to a next

Hi,

 

Use this line in drawfocus or paint

 g.drawShadedFilledPath();

 

Regards,

Lakshman K

----------------------------------------------------------
Feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

Re: How do i move a shape from one position to a next

that is for shading, I would like to move an object from left to right

 

say this video here

http://www.youtube.com/watch?v=65XmnE4QnJo

 

the shooterspacecraft at the bottom moves left to right. I would like my rectangle to move left to right in the same manor on a button click and hold down?

Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

Re: How do i move a shape from one position to a next

I believe its along the lines

of navigationMovement method ?

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: How do i move a shape from one position to a next

Not navigationMovement.  To pick up keys, what you want to override is keyRepeat.  have a look at it. 

 

That said, why not use the trackpad?

 

That is how you pick up the direction the user wants to move.  The quietist then is how do you reflect this on your screen.  The easiest option is to set a new position and call invalidate.  However this is an expensive operation, so you might want to detect the areas that need to repainted and just repaint those (i.e. where the ship currently is to clear it and where it will be to move it. 

 

Hope this helps. 

Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

Re: How do i move a shape from one position to a next

found a similar example online

 

  1. final public class DBall extends JPanel implements ActionListener,  KeyListener {  
  2.   
  3.   
  4.     static JFrame frame = new JFrame("DBall");  
  5.     static Container contentPane = frame.getContentPane();  
  6.     double a = 250;  
  7.     double b = 175;  
  8.     double c = 50;  
  9.     double d = 50;  
  10.     double aa = 0;  
  11.     double bb = 0;  
  12.     double cc = 600;  
  13.     double dd = 500;  
  14.   
  15.     public DBall() {  
  16.     this.setLayout(null);  
  17.     this.setFocusable(true);  
  18.     this.setBackground(Color.black);  
  19.     this.addKeyListener(this);  
  20.     }  
  21.     public void keyPressed(KeyEvent e) {  
  22.         switch(e.getKeyCode()) {  
  23.         case KeyEvent.VK_UP :  bb -= 5; repaint(); break;  
  24.   
  25.         case KeyEvent.VK_DOWN : bb += 5; repaint(); break;  
  26.   
  27.         case KeyEvent.VK_LEFT : aa += 5; repaint(); break;  
  28.   
  29.         case KeyEvent.VK_RIGHT : aa -= 5; repaint(); break;  
  30.         }  
  31.     }  
  32.     public void keyTyped(KeyEvent e) {  
  33.     }  
  34.     public void keyReleased(KeyEvent e) {  
  35.     }  
  36.     protected void paintComponent (Graphics g) {  
  37.         super.paintComponent(g);//removes all previously drawn content and draw from scratch  
  38.         Graphics2D g2 = (Graphics2D) g;  
  39.     g.setColor(Color.black);  
  40.     Rectangle2D rec1 = new Rectangle2D.Double(aa, bb, cc, dd);  
  41.     g2.draw(rec1);  
  42.     g.setColor(Color.blue);  
  43.     rec1 rec2 = new Rectangle2D.Double(a, b, c, d); //rec1 instead of Rectangle2d  
  44.      g2.fill(rec2);  
  45.     g.setColor(Color.red);  
  46.     Ellipse2D person = new Ellipse2D.Double(2754505050);  
  47.      g2.fill(person);  
  48.     }  
  49.   
  50.     public void actionPerformed(ActionEvent ex) {  
  51.     }  
  52.    final public static void main(String[] args) {  
  53.         frame.setSize(600500);  
  54.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  55.     contentPane.add(new DBall());  
  56.     frame.setUndecorated(true);  
  57.     frame.setResizable(false);  
  58.         frame.setVisible(true);  
  59.     }  
  60. }  

??