943,857 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1282
  • Java RSS
Oct 2nd, 2008
0

Canvas, pain() and playing sound

Expand Post »
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

Java Syntax (Toggle Plain Text)
  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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Oct 2nd, 2008
0

Re: Canvas, pain() and playing sound

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 2nd, 2008
0

Re: Canvas, pain() and playing sound

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
java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 3rd, 2008
-1

Re: Canvas, pain() and playing sound

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?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Oct 3rd, 2008
-1

Re: Canvas, pain() and playing sound

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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Oct 3rd, 2008
0

Re: Canvas, pain() and playing sound

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 3rd, 2008
-1

Re: Canvas, pain() and playing sound

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:	5
Size:	15.5 KB
ID:	7605
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help doing sorts
Next Thread in Java Forum Timeline: So very lost =( trying to get a total





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC