0

JFrame here

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
public class PikaPlatformer
{

   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)throws Exception
   {
      URL url = new URL(
      "http://tindeck.com/download/f8d086eb0dadd3745cb7c6dfa027fd89/ihmz/[tindeck.com]%20-%20minime010%20-%20Pokemon_Red_Blue_Yellow_Battle_music.wav");
      Clip clip = AudioSystem.getClip();
      AudioInputStream ais = AudioSystem.
      getAudioInputStream( url );
      clip.open(ais);
      clip.loop(5);
      JFrame frame = new JFrame ("Go Pikachu, go!!!");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add (new PikaPlatformerPanel());
      frame.pack();
      frame.setVisible(true);
   }

}

Program here

//********************************************************************
//  Pikachu Platformer Game
//  Jonathan Connelly
//********************************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;

public class PikaPlatformerPanel extends JPanel
{
   private final int WIDTH = 1433, HEIGHT = 100;
   private int JUMP = 5;  // increment for image movement
   private Clip clip;
   private final int IMAGE_SIZE = 40;

   private ImageIcon up, down, right, left, back, currentImage, attack, atkmoving;
   private int x, y;

   public static void main(String args[]){
     new PikaPlatformerPanel();}
   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel and loads the images.
   //-----------------------------------------------------------------
   public PikaPlatformerPanel()
   {
      addKeyListener (new DirectionListener());

      x = 1;
      y = 50;
      attack = new ImageIcon ("Thundershock.png");
      up = new ImageIcon ("New position.png");
      down = new ImageIcon ("Pikachu1.png");
      left = new ImageIcon ("PikachuLeft.png");
      right = new ImageIcon ("PikaRunning.png");
      back = new ImageIcon ("PikaPlatBackground.PNG");
      currentImage = right;
      setBackground (Color.black);
      setPreferredSize (new Dimension(WIDTH, HEIGHT));
      setFocusable(true);
      }

   //-----------------------------------------------------------------
   //  Draws the image in the current location.
   //-----------------------------------------------------------------

   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);
      back.paintIcon (this, page, 1, 1);
      currentImage.paintIcon (this, page, x, y);
   }
   private class DirectionListener implements KeyListener
   {
          //--------------------------------------------------------------
          //  Responds to ...
0

whether or not you want it to run after clicking on a button or at startup does not change the code of playing the music file at all. you want it to play in the background the whole time? run it first from early within the main. start an additional thread that monitors whether or not your sound finished playing (or any other way) and if done, re-start it.

Could you give me instructions? I'm not super experienced.

0

New code:

import javax.swing.*;

import sun.audio.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class PikaPlatformer
{
   public static void main (String[] args)
   {
       AePlayWave aw = new AePlayWave( "C:\\Documents and Settings\\JonathanConnelly\\Desktop\\Pokemon_Red_Blue_Yellow_Battle_music.wav" );
       aw.start();     

      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new PikaPlatformerPanel());

      frame.pack();
      frame.setVisible(true);
   }            
}

Shouldn't the AePlayWave work? Is my .wav file too big?

0

I have a problem with this...

import javax.swing.*;
import sun.audio.*;
import java.awt.event.*;
import java.io.*;

public class PikaPlatformer
{

   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new PikaPlatformerPanel());

      frame.pack();
      frame.setVisible(true);
   }

}

How would I insert sound into this I just want music to play in my game. Any suggestions?( this is just the jFrame)

-1

I have a problem with this...

import javax.swing.*;
import sun.audio.*;
import java.awt.event.*;
import java.io.*;

public class PikaPlatformer
{

   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new PikaPlatformerPanel());

      frame.pack();
      frame.setVisible(true);
   }
}

How would I insert sound into this I just want music to play in my game. Any suggestions?( this is just the jFrame)