Canvas, pain() and playing sound

Thread Solved

Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Canvas, pain() and playing sound

 
-1
  #1
Oct 2nd, 2008
I created menu for mobile phone, where after you make move screen should be repainted and after that a sound should be played. However at present stage sound is played first and just after that Canvas view is repainted. Bellow is skeleton of the class

  1. public class ContactMenu extends Canvas implements CommandListener
  2. {
  3.  
  4. public ContactMenu()
  5. {
  6. backCommand = new Command("Back", Command.BACK, 0);
  7. addCommand(backCommand);
  8. selectCommand = new Command("Select", Command.SCREEN, 2);
  9. addCommand(selectCommand);
  10. setCommandListener(this);
  11.  
  12. backImg = il.loadImage("se_logo2.jpg");
  13. }
  14.  
  15. public void paint(Graphics g)
  16. {
  17. //All drawing activities
  18. playSelected();
  19. }
  20.  
  21. public void commandAction(Command c, Displayable d)
  22. {
  23. if(lastCmd.equals(c.getLabel()))
  24. {
  25. if (c == backCommand)
  26. {
  27. }
  28. }
  29. else
  30. {
  31. lastCmd = c.getLabel();
  32. tts.setSoundURL(lastCmd.toLowerCase());
  33. tts.playSound("soundURL");
  34. }
  35. }
  36.  
  37. protected void keyPressed(int keyCode)
  38. {
  39. String str = getKeyName(keyCode);
  40. if(str.equals("Up") || str.equals("UP"))
  41. {
  42. cml.moveSelection(-1);
  43. repaint();
  44. }
  45. if(str.equals("Down") || str.equals("DOWN"))
  46. {
  47. cml.moveSelection(1);
  48. repaint();
  49. }
  50. }
  51.  
  52. public void playSelected()
  53. {
  54. }
  55.  
  56. public void playSelected(String str)
  57. {
  58. }
  59. }
I tried to move playSelected() at the end of keyPressed(), however this made no change to processing. Any suggestions how to tackle this issue?
Last edited by peter_budo; Oct 2nd, 2008 at 9:11 am. Reason: typo
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Canvas, pain() and playing sound

 
0
  #2
Oct 2nd, 2008
I believe the result of the paint method (i.e. the actual changing of the picture seen on the screen) doesn't occur until the paint method returns. Which means, playing the sound inside the paint method will ensure that the sound is played first.

I could be completely off-base, but I believe that is the way it is.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Canvas, pain() and playing sound

 
0
  #3
Oct 2nd, 2008
If you only want the sound to be played on app-triggered repaints and not system-triggered (like repainting on resizes or other windows being dragged over it), you can override the update() method
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import javax.swing.JOptionPane;
  4.  
  5. public class PaintExample extends java.awt.Frame {
  6.  
  7. public PaintExample() {
  8. initComponents();
  9. }
  10.  
  11. private void doSomething() {
  12. JOptionPane.showMessageDialog(this, "done");
  13. }
  14.  
  15. private void formKeyPressed(java.awt.event.KeyEvent evt) {
  16. repaint();
  17. }
  18.  
  19. @Override
  20. public void paint(Graphics g) {
  21. // arbitrary long paint process
  22. int w = getWidth();
  23. int h = getHeight();
  24. int reps = 500000;
  25. g.setColor(Color.BLACK);
  26. for (int i = 0; i < reps; i++) {
  27. int y = (int)(i / (float)reps * h);
  28. g.drawLine(0, y, w, y);
  29. }
  30. }
  31.  
  32. @Override
  33. public void update(Graphics g) {
  34. super.update(g);
  35. doSomething();
  36. }
  37.  
  38. private void initComponents() {
  39. setTitle("PaintExample");
  40. setMinimumSize(new java.awt.Dimension(400, 400));
  41. addWindowListener(new java.awt.event.WindowAdapter() {
  42. public void windowClosing(java.awt.event.WindowEvent evt) {
  43. exitForm(evt);
  44. }
  45. });
  46. addKeyListener(new java.awt.event.KeyAdapter() {
  47. public void keyPressed(java.awt.event.KeyEvent evt) {
  48. formKeyPressed(evt);
  49. }
  50. });
  51.  
  52. pack();
  53. }
  54.  
  55. private void exitForm(java.awt.event.WindowEvent evt) {
  56. System.exit(0);
  57. }
  58.  
  59. public static void main(String args[]) {
  60. java.awt.EventQueue.invokeLater(new Runnable() {
  61. public void run() {
  62. new PaintExample().setVisible(true);
  63. }
  64. });
  65. }
  66.  
  67. }

Just press a key to trigger the repaint. It will process the doSomething() code after the paint has completed.
Last edited by Ezzaral; Oct 2nd, 2008 at 7:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Canvas, pain() and playing sound

 
-1
  #4
Oct 3rd, 2008
I have no idea how to change supported source version from 1.3 to 1.5 in NetBeans. It was simple to do with IntelliJ IDEA but this IDE has currently certain issues with Sony Ericsson WTK which I'm using.
Does anyone know where to change this setting?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Canvas, pain() and playing sound

 
-1
  #5
Oct 3rd, 2008
According to reply on NetBeans forum Java ME runs only on 1.4 Language Level. I can only hope there will be some changes with the new release next year
Last edited by peter_budo; Oct 3rd, 2008 at 11:04 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Canvas, pain() and playing sound

 
0
  #6
Oct 3rd, 2008
Originally Posted by peter_budo View Post
I have no idea how to change supported source version from 1.3 to 1.5 in NetBeans. It was simple to do with IntelliJ IDEA but this IDE has currently certain issues with Sony Ericsson WTK which I'm using.
Does anyone know where to change this setting?
Since you mention ME only runs against 1.4, the option may not be available, but just for the sake of info you would normally set that in the Project Properties dialog Sources node, Source/Binary Format setting.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Canvas, pain() and playing sound

 
-1
  #7
Oct 3rd, 2008
No such option under mobility project properties
We cannot have everything in our life, this is only petty university project and I already showed off too much
Click image for larger version

Name:	netbeans.png
Views:	1
Size:	15.5 KB
ID:	7605
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC