Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1308

Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help

From Michael Adedeji <yankosmgt@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help
Date 2011-11-09 03:48 -0800
Organization http://groups.google.com
Message-ID <154b884b-5a2e-4c59-a88a-751d1635b61f@cu3g2000vbb.googlegroups.com> (permalink)
References <bd906c49-fab3-47b0-ad80-82dbdcc3cf9f@g1g2000vbd.googlegroups.com> <nospam-B1E5E2.12014407112011@news.aioe.org> <5b8f8046-c136-45c5-bd2e-cb8aeb85e2d3@g1g2000vbd.googlegroups.com> <d0ec0e85-ad00-4fe5-9a86-5511fbae10a5@w7g2000yqc.googlegroups.com> <nospam-C77D02.10082808112011@news.aioe.org>

Show all headers | View raw


On Nov 8, 3:08 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <d0ec0e85-ad00-4fe5-9a86-5511fbae1...@w7g2000yqc.googlegroups.com>,
>  Michael Adedeji <yankos...@gmail.com> wrote:
>
> > What if I want the ball colours to change and also I want the size and
> > the speed to be like a menu?
>
> Just make your changes in the actionPerformed() method, before calling
> repaint(). For menus, it's never too soon to learn about Action:
>
> <http://download.oracle.com/javase/tutorial/uiswing/misc/action.html>
>
> See also this the Model View Controller example:
>
> <http://sites.google.com/site/drjohnbmatthews/kineticmodel>
>
> --
> [Please omit signatures when responding.]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package basics;

/**
 *
 * @author michaeladedeji
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Formatter;
import javax.swing.*;
import java.io.File;
import javax.sound.sampled.*;

import helpers.GlobalProperties;
import java.awt.Color;
import java.util.Random;
//private Color ballColor = null;


public class AnimationSound extends JPanel {
    private static final int BOX_WIDTH = 640;
    private static final int BOX_HEIGHT = 480;
    private static final int RATE = 30;
    private File soundFile = new File("resources/snd16.wav");
    private Clip clip;
    private float ballRadius = 50;
    private float ballX = 250 - ballRadius;
    private float ballY = 250 - ballRadius;
    private float ballSpeedX = 10;
    private boolean randomColorMode = false;
    private Color ballColor = null, numberColor = null;
    private Color backgroundColor;

    public AnimationSound() {
        this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
        // Prepare a Clip

        try {
            AudioInputStream audioInputStream =
                AudioSystem.getAudioInputStream(soundFile);
            AudioFormat audioFormat = audioInputStream.getFormat();
            DataLine.Info dataLineInfo =
                new DataLine.Info(Clip.class, audioFormat);
            clip = (Clip) AudioSystem.getLine(dataLineInfo);
            clip.open(audioInputStream);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        Timer timer = new Timer(1000 / RATE, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ballX += ballSpeedX;
                if (ballX - ballRadius < 0) {
                    ballSpeedX = -ballSpeedX;
                    ballX = ballRadius;
                    playSound();
                    //getParameters();
                } else if (ballX + ballRadius > BOX_WIDTH) {
                    ballSpeedX = -ballSpeedX;
                    ballX = BOX_WIDTH - ballRadius;
                    playSound();
                    //getParameters();
                }
                repaint();

            }
        });
        timer.start();
    }
    // Play the sound in a separate thread.
    private void playSound() {
        Runnable soundPlayer = new Runnable() {
            @Override
            public void run() {
                try {
                    clip.setMicrosecondPosition(0);
                    clip.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(soundPlayer).start();
    }
    private void getColors() {
		if(randomColorMode){
			Random ran = new Random();
			int ranInt = ran.nextInt(4);

			switch(ranInt){
			case 0: ballColor = Color.white;break;
			case 1: ballColor = Color.blue;break;
			case 2: ballColor = Color.yellow;break;
			default: ballColor = Color.red;break;
			}
		}


		if(ballColor.equals(Color.white)){
			numberColor = Color.red;
			backgroundColor = Color.black;
		}
		else if(ballColor.equals(Color.blue)){
			numberColor = Color.yellow;
			backgroundColor = Color.black;
		}
		else if(ballColor.equals(Color.yellow)){
			numberColor = Color.black;
			backgroundColor = Color.black;
		}
		else{
			numberColor = Color.white;
			backgroundColor = Color.black;
		}
	}
    private void getParameters() {

        if(ballColor.equals(Color.black))
			randomColorMode   = true;
		else
			randomColorMode = false;

		getColors();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // Paint background

        //getParameters();
        g.setColor(backgroundColor);
        g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
        g.setColor(ballColor);
        //this.getParameters();
        g.fillOval(
            (int) (ballX - ballRadius),
            (int) (ballY - ballRadius),
            (int) (2 * ballRadius), (int) (2 * ballRadius));
        g.setColor(ballColor);
        g.setFont(new Font("Dialog", Font.PLAIN, 12));
        StringBuilder sb = new StringBuilder();
        Formatter formatter = new Formatter(sb);
        formatter.format(
            "Ball @(%3.0f) Speed=(%2.0f)", ballX, ballSpeedX);
        g.drawString(sb.toString(), 20, 30);
    }
    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override

            public void run() {
                JFrame frame = new JFrame("A Moving Ball");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new AnimationSound());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

}

I don't know how to connect the color to make it change the ball...any
help???

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help Michael Adedeji <yankosmgt@gmail.com> - 2011-11-07 03:33 -0800
  Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help "John B. Matthews" <nospam@nospam.invalid> - 2011-11-07 12:01 -0500
    Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help Michael Adedeji <yankosmgt@gmail.com> - 2011-11-07 09:27 -0800
      Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help Michael Adedeji <yankosmgt@gmail.com> - 2011-11-08 05:17 -0800
        Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help "John B. Matthews" <nospam@nospam.invalid> - 2011-11-08 10:08 -0500
          Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help Michael Adedeji <yankosmgt@gmail.com> - 2011-11-09 03:48 -0800
  Re: Help : I want the sound to sound at the left and right of the screen..I also want it to continue forever....but the sound plays 4 times and stop...kindly help Roedy Green <see_website@mindprod.com.invalid> - 2011-11-08 06:49 -0800

csiph-web