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?

Recommended Answers

All 6 Replies

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.

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.

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?

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:-/

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.

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 ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.