Don And Nicks Violin Hero

<< Russell's Final Project | OldProjectsTrailIndex | Michael's Checkers Game >>

Fr Chris' notes on progress


Don will be working on a Note class that will know how to draw itself, based on the path chosen at its instantiation.

Nick needs to work on the keyboard input.. (paddle that is moved into one of four locations, and knows where it is. The applet then can see if a note is at the bottom, and ask the paddle is at the correct string location or not.

The song class may take a while to write, so maybe Suren & I can work on a class to make notes play a simple song like "Twinkle, Twinkle, Little Star"

Twinkle Twinkle


This song is familiar, only uses 6 (C,D,E,F,G,A) notes, and the rhythm is very uniform, so if we keep a frame counter, we can add a new note when ever we have a frame%tempo==0. We can either change so that there is six strings or assign more than one note to the same string.

C,C,G,G,A, A, G, (rest),

F,F,E, E, D, D, C, (rest),

G, G, F, F, E, E, D (rest),

G, G, F, F, E, E, D (rest),

C,C,G,G,A, A, G, (rest)

F,F,E, E, D, D, C, (rest),

Note sound files

c.wav

d.wav

e.wav

f.wav

g.wav

a.wav

vC.wav

vD.wav

vE.wav

vF.wav

vG.wav

vA.wav

honk1.wav


Starter Code

Song.java

import java.util.ArrayList;
import java.awt.event.*; 
import java.applet.*;
import java.awt.*;
import javax.swing.*;

/**
 * Song class .
 * 
 * @author Fr CHris
 * @version 28 May 2009
 */
public class Song
{
	// instance variables - replace the example below with your own
	private ArrayList<String> theSong;
    private int next;
    private AudioClip c,d,e,f,g,a;
	/**
	 * Constructor for objects of class Song
	 */
	public Song(Applet ap)
	{
		// initialize instance variables
		theSong=new ArrayList<String>();
		next=0;
		c = ap.getAudioClip(ap.getDocumentBase(), "c.wav");
        d = ap.getAudioClip(ap.getDocumentBase(), "d.wav");
        e = ap.getAudioClip(ap.getDocumentBase(), "e.wav");
        f = ap.getAudioClip(ap.getDocumentBase(), "f.wav");
        g = ap.getAudioClip(ap.getDocumentBase(), "g.wav");
        a = ap.getAudioClip(ap.getDocumentBase(), "a.wav");
		//a.play();
	}

	/**
	 * An example of a method - replace this comment with your own
	 * 
	 * @param  y   a sample parameter for a method
	 * @return     the sum of x and y 
	 */
	public void add(String n)
	{
		theSong.add(n);
	}
	public int getNext(){

	    return next++;
	   }

	public String playNextTone()
	{

	    String currentNote=theSong.get(next);
	    int theToneNumber = "cdefga".indexOf(currentNote);    

	    switch (theToneNumber){
	        case 0: c.play(); break;
	        case 1: d.play(); break;
	        case 2: e.play(); break;
	        case 3: f.play(); break;
	        case 4: g.play(); break;
	        case 5: a.play(); break;
	       }
	    next++;
	    if (next >= theSong.size() ) next=0;  
	    return currentNote;
	   }

}

SongPlayer.java

import java.awt.event.*; 
import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class SongPlayer extends Applet 
  implements  ActionListener
{

    Timer timer;
    Image virtualMem;
    Graphics gBuffer;

    int appletWidth;        
    int appletHeight; 
    int frame;
    int lastFrame;
    Button myButton;
    Song s;

    String tone="";
   /**
     * ActionListener is an interface and
     * requires the actionPerformed() method
     * to be defined..in this case we
     * look for a restart button being pressed
     */
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == timer) 
        {
            if (frame <= 110) 
            {

               frame++;


            }
            else 
            {
                frame=0;

            }
            if (frame==110) tone=s.playNextTone();
        }
        else {
            //restart button pressed
              frame=0;
              if (timer.isRunning()) 
              {
                  timer.stop();
                  myButton.setLabel("Continue");

               }
               else
               {
                   timer.start();
                   myButton.setLabel("Pause");

                }

            }
        repaint();
    }
    // OUR CLASS IS A SUBCLASS OF APPLET
    /**
     * this init() method will be run at the beginning
     * and where we create the Listeners 
     */

    public void init()
    {


        appletWidth = getWidth();
        appletHeight = getHeight(); 
        frame=0;
        timer=new Timer(10, this);
        lastFrame=appletHeight;
        virtualMem = createImage(appletWidth,appletHeight);
        gBuffer = virtualMem.getGraphics();
        gBuffer.setColor(Color.white);
        gBuffer.fillRect(0,0,appletWidth,appletHeight);


        myButton= new Button("Begin Song");

        myButton.addActionListener (this);
        add(myButton);   
        s = new Song(this);
        s.add("c"); s.add("c"); s.add("g"); s.add("g");
        s.add("a"); s.add("a"); s.add("g"); s.add("rest");

        s.add("f"); s.add("f"); s.add("e"); s.add("e");
        s.add("d"); s.add("d"); s.add("c"); s.add("rest");

        s.add("g"); s.add("g"); s.add("f"); s.add("f");
        s.add("e"); s.add("e"); s.add("d"); s.add("rest");

        s.add("g"); s.add("g"); s.add("f"); s.add("f");
        s.add("e"); s.add("e"); s.add("d"); s.add("rest");

        s.add("c"); s.add("c"); s.add("g"); s.add("g");
        s.add("a"); s.add("a"); s.add("g"); s.add("rest");

        s.add("f"); s.add("f"); s.add("e"); s.add("e");
        s.add("d"); s.add("d"); s.add("c"); s.add("rest");


    }

     public void paint(Graphics g)
    {
        //We draw using the object methods of
        // the graphics buffer, not what is
        // currently on the screen
        gBuffer.setColor(Color.white);
        gBuffer.fillRect(0,0,appletWidth,appletHeight);gBuffer.setColor(Color.black);

        gBuffer.setColor(Color.black);
        gBuffer.drawString("Frame "+frame, 180,50+frame);
        gBuffer.drawLine(0,50, 200,50);
         gBuffer.drawLine(0,50+110+10, 200,50+110+10);
        gBuffer.drawString("tone "+tone, 20,120);
        gBuffer.setColor(Color.blue);
        if (!tone.equals("rest") ) gBuffer.fillOval(150, 50+frame,10,10);

        //Now we send the result to the screen
        g.drawImage(virtualMem,0,0,this);
    }
    public void update(Graphics g)
    {
        paint(g); //get rid of flicker with this method
    }
}

starting base code Two Player Starter Code

Mouse Listener http://www.stfrank.com/apcs/Final_Project_Starters.html

KeyMovement

been working on the key movement and added a paddle class, having trouble getting it to appear on the Applet

Fr Chris' comments: needs some import statements, there is a bracketing issue in the actionPerformed(ActionEvent e) method, and p1 is never defined... I presume you want to do somthing in the init() method like " Paddle p1= new Paddle(); "

import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JApplet;
import javax.swing.Timer;

public class KeyMovement implements KeyListener,ActionListener
{

	private int keyCode;
    char c;
    int maxX,maxY,xLoc,yLoc;
    Image virtualMem;
    Graphics gBuffer;
    Paddle p1;

    private char  s1, s2, s3, s4, hit;
    String action="none";
    Timer timer;
    Song ths;

	public void init()
	{
		JApplet i=new JApplet();
		s1='a';
		s2='s';
		s3='d';
		s4='f';
		hit=Character.toChars(32)[0];
		maxX=i.getSize().width;
		maxY=i.getSize().height;
		int x = 0;
		xLoc=x;
		int y = 0;
		yLoc=x;
		timer = new Timer(10, this);
		timer.stop();
		Paddle p1=new Paddle(Color.orange, xLoc, yLoc, maxX, maxY);
	}

	public void paint(Graphics g)
	{
		// simple text displayed on applet
		g.setColor(Color.white);
		g.fillRect(0, 0, 500, 500);
		g.setColor(Color.black);
		g.drawString("Hokey Hocky - KeyListener / ActionListener Demo - Press a Key", 20, 20);
		g.setColor(Color.blue);


		g.drawString("action is "+action, 20, 60);
		g.setFont(new Font("Arial", Font.PLAIN, 32));
		g.setColor(Color.red);
		g.drawString("\""+c+"\"", 50, 100);
		p1.draw(g);
	}


/**
* KeyListener Interface's Implementation
*/
	public void keyTyped(KeyEvent e) {}
	public void keyPressed(KeyEvent e) 
	{ JApplet i=new JApplet();
		keyCode=e.getKeyCode();
		c=e.getKeyChar();
		action="none";
		if (c==hit) {
			action= "BACH!";
			timer.start();
			p1.draw(gBuffer);
    		}
		if (c==s1){
			p1.setX(230);
			p1.draw(gBuffer);
		}
		if (c==s2){
			p1.setX(325);
			p1.draw(gBuffer);
		}
		if (c==s3){
			p1.setX(425);
			p1.draw(gBuffer);
		}
		if (c==s4){
			p1.setX(525);
			p1.draw(gBuffer);
		}

		i.repaint();
		}
	public void keyReleased(KeyEvent e) {}
	/**
 	* ActionListener is an interface and
	* requires the actionPerformed() method
	* to be defined..in this case we
	* look for a timer event
	*/
		public void actionPerformed(ActionEvent e)
		{   JApplet i=new JApplet();
			Object source = e.getSource();
			if (source == timer) 
    	// the passage of time, so we move our puck
    	// if it was hit
			{
				if (timer.isRunning()){

            	if (p1.getX()+30>maxX)
            	{
            		timer.stop();

                	}else{
                    	action="goal!";
                	}
            	}
        	}i.repaint();
    	}



	public void update(Graphics g){
    paint(g);
	}
}

Paddle

import java.awt.*;


public class Paddle {

	/**
	 * @param args
	 */
	protected int xLoc;
	protected int yLoc;
	protected int maxX;
    protected int maxY;
    protected Color c;

	 public Paddle (Color color, int x, int y, int maximumX, int maximumY)
	    {
		 	c= Color.RED;
	        xLoc=x;
	        yLoc=y;
	        maxX=maximumX;
	        maxY=maximumY;

	    }


	    public int getX()
	    {
	        return xLoc;
	    }
	    public int getY()
	    {
	        return yLoc;
	    }
	    public void setX(int x)
	    {
	        if (x>=0 && x<=maxX) xLoc=x;
	    }
	    public void setY(int y)
	    {
	        if (y>=0 && y<=maxY) yLoc=y;
	    }
	    public void draw(Graphics g)
	    {
	        g.setColor(c);
	        g.fillRect(xLoc, yLoc, 10,40);
	    }
	}


Fr Chris' Notes to Nick

The plan is to have Don work on the dots coming down the string, and Nick to work on the "paddle" end to catch the notes.

Looks like great progress Nick!

To Do:

  1. Find some nice sounds for a hit or a miss.
  2. keep score

Fr Chris' Notes to Don

Note.java


import java.awt.*;
/**
 *  class Player 
 * 
 * @author Don Palmigiano
 * @version 11 May 2009
 */
public class Note
{

    protected int xLoc;
    protected int yLoc;
    protected int deltaX;
    protected int deltaY;
    protected Color color;

    /**
     * Constructor for objects of class Player
     */
 public Note (Color c, int startX, int startY, int endX, int endY)
    {

        xLoc=startX;
        yLoc=startY;
        deltaX= (endX-startX)/(1000/40);
        deltaY= (endY-startY)/(1000/40);
        color=c;
    }
 public void move()
    {
       xLoc+=deltaX;
       yLoc+=deltaY;


    }
    public void draw(Graphics g)
    {
        g.setColor(color);
        g.fillOval(xLoc-15, yLoc+15, 30,30);


    }
    public int getX()
    {
        return xLoc;
    }
    public int getY()
    {
        return yLoc;
    }
    //public void setX(int x)
    //{
     //   if (x>=0 && x<=maxX) xLoc=x;
    //}
   // public void setY(int y)
    //{
      //  if (y>=0 && y<=maxY) yLoc=y;
    //}

}

FretNot.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.applet.*;
/**
 * Write a description of class FretNot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FretNot extends Applet implements ActionListener
{
     Song twinkle;
     Button myButton;
     String tone="";
     Note green;
     Note blue;
     Note red;
     Note yellow;
     Timer t;
     Image virtualMem;
     Graphics gBuffer;
     int appletWidth;        
     int appletHeight;
     int frame=110;
     int lastFrame;
     public ArrayList<Note> n;
     public ArrayList<Note> s;


 public void actionPerformed(ActionEvent e)
    {
        Graphics g;
        Object source = e.getSource();
       if (source == t) 
        {
            if (frame <= 40) 
            {

               frame++;


            }
            else 
            {
                frame=0;
                s.add(n.get((int)(Math.random()*4)));  

            }

        }
        else {
            //restart button pressed
              frame=0;
              if (t.isRunning()) 
              {
                  t.stop();
                  myButton.setLabel("Continue");

               }
               else
               {
                   t.start();
                   myButton.setLabel("Pause");

                }

            }
            repaint();
         }

     public void init()
        {

        green= new Note(Color.GREEN,230,10,50,500);
        yellow= new Note(Color.YELLOW,325,10,250,500);
        blue= new Note(Color.BLUE,425,10,450,500);
        red= new Note(Color.RED,525,10,650,500);
        appletWidth = getWidth();
        appletHeight = getHeight(); 
        frame=0;
        t=new Timer(40, this);
        t.start();
        n= new ArrayList<Note>();
        s= new ArrayList<Note>();
        n.add(blue);
        n.add(yellow);
        n.add(green);
        n.add(red);
        lastFrame=appletWidth;
        virtualMem = createImage(appletWidth,appletHeight);
        gBuffer = virtualMem.getGraphics();
        gBuffer.setColor(Color.white);
        gBuffer.fillRect(0,0,appletWidth,appletHeight);



        myButton= new Button("Begin Song");

        myButton.addActionListener (this);
        add(myButton);   
        twinkle = new Song(this);
        twinkle.add("c"); twinkle.add("c"); twinkle.add("g"); twinkle.add("g");
        twinkle.add("a"); twinkle.add("a"); twinkle.add("g"); twinkle.add("rest");

        twinkle.add("f"); twinkle.add("f"); twinkle.add("e"); twinkle.add("e");
        twinkle.add("d"); twinkle.add("d"); twinkle.add("c"); twinkle.add("rest");

        twinkle.add("g"); twinkle.add("g"); twinkle.add("f"); twinkle.add("f");
        twinkle.add("e"); twinkle.add("e"); twinkle.add("d"); twinkle.add("rest");

        twinkle.add("g"); twinkle.add("g"); twinkle.add("f"); twinkle.add("f");
        twinkle.add("e"); twinkle.add("e"); twinkle.add("d"); twinkle.add("rest");

        twinkle.add("c"); twinkle.add("c"); twinkle.add("g"); twinkle.add("g");
        twinkle.add("a"); twinkle.add("a"); twinkle.add("g"); twinkle.add("rest");

        twinkle.add("f"); twinkle.add("f"); twinkle.add("e"); twinkle.add("e");
        twinkle.add("d"); twinkle.add("d"); twinkle.add("c"); twinkle.add("rest");


    } 



   public void paint(Graphics g){
        // simple text displayed on applet

        gBuffer.setColor(Color.BLACK);
        gBuffer.fillRect(0, 0, appletWidth, appletHeight);
        gBuffer.setColor(Color.blue);
        gBuffer.drawLine(230,10,50,500);
        gBuffer.drawLine(325,10,250,500);
        gBuffer.drawLine(425,10,450,500);
        gBuffer.drawLine(525,10,650,500);

          for (Note theNote:s)
           {
               theNote.draw(gBuffer);
               theNote.move();
           }
           for (int i=0;i<s.size();i++)
           {
               if(s.get(i).getY()>=490 && s.size()!=0)
                {
                   s.remove(i);
                   tone=twinkle.playNextTone();
                }
           }


        g.drawImage(virtualMem,0,0,this);





       // g.drawString("\"""\"", 400, 100);
       // g.drawString("", 375, 90);

       // g.drawString("\""+p1score+"\"", 50, 100);
       // g.drawString("", 25, 90);


    }
         public void update(Graphics g)
    {
        paint(g); //get rid of flicker with this method
    }
}