| | |
Canvas, pain() and playing sound
Thread Solved |
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
I tried to move playSelected() at the end of keyPressed(), however this made no change to processing. Any suggestions how to tackle this issue?
Java Syntax (Toggle Plain Text)
public class ContactMenu extends Canvas implements CommandListener { public ContactMenu() { backCommand = new Command("Back", Command.BACK, 0); addCommand(backCommand); selectCommand = new Command("Select", Command.SCREEN, 2); addCommand(selectCommand); setCommandListener(this); backImg = il.loadImage("se_logo2.jpg"); } public void paint(Graphics g) { //All drawing activities playSelected(); } public void commandAction(Command c, Displayable d) { if(lastCmd.equals(c.getLabel())) { if (c == backCommand) { } } else { lastCmd = c.getLabel(); tts.setSoundURL(lastCmd.toLowerCase()); tts.playSound("soundURL"); } } protected void keyPressed(int keyCode) { String str = getKeyName(keyCode); if(str.equals("Up") || str.equals("UP")) { cml.moveSelection(-1); repaint(); } if(str.equals("Down") || str.equals("DOWN")) { cml.moveSelection(1); repaint(); } } public void playSelected() { } public void playSelected(String str) { } }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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.
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
----------------------------------------------
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
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
Just press a key to trigger the repaint. It will process the doSomething() code after the paint has completed.
java Syntax (Toggle Plain Text)
import java.awt.Color; import java.awt.Graphics; import javax.swing.JOptionPane; public class PaintExample extends java.awt.Frame { public PaintExample() { initComponents(); } private void doSomething() { JOptionPane.showMessageDialog(this, "done"); } private void formKeyPressed(java.awt.event.KeyEvent evt) { repaint(); } @Override public void paint(Graphics g) { // arbitrary long paint process int w = getWidth(); int h = getHeight(); int reps = 500000; g.setColor(Color.BLACK); for (int i = 0; i < reps; i++) { int y = (int)(i / (float)reps * h); g.drawLine(0, y, w, y); } } @Override public void update(Graphics g) { super.update(g); doSomething(); } private void initComponents() { setTitle("PaintExample"); setMinimumSize(new java.awt.Dimension(400, 400)); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { formKeyPressed(evt); } }); pack(); } private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new PaintExample().setVisible(true); } }); } }
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.
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?
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Other Threads in the Java Forum
- Previous Thread: Need help doing sorts
- Next Thread: So very lost =( trying to get a total
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






