DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Canvas, pain() and playing sound (http://www.daniweb.com/forums/thread148691.html)

peter_budo Oct 2nd, 2008 9:10 am
Canvas, pain() and playing sound
 
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

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)
    {
    }
}
I tried to move playSelected() at the end of keyPressed(), however this made no change to processing. Any suggestions how to tackle this issue?

masijade Oct 2nd, 2008 9:27 am
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.

Ezzaral Oct 2nd, 2008 7:07 pm
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
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.

peter_budo Oct 3rd, 2008 7:31 am
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?

peter_budo Oct 3rd, 2008 11:03 am
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:-/

Ezzaral Oct 3rd, 2008 12:10 pm
Re: Canvas, pain() and playing sound
 
Quote:

Originally Posted by peter_budo (Post 704093)
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.

peter_budo Oct 3rd, 2008 1:57 pm
Re: Canvas, pain() and playing sound
 
1 Attachment(s)
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 ;)
Attachment 7605


All times are GMT -4. The time now is 6:56 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC