Advertisement
Guest User

Guitar TUner

a guest
May 28th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.88 KB | None | 0 0
  1. import java.awt.Rectangle;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.MouseMotionListener;
  7. import javax.swing.JApplet;
  8. import java.awt.Color;
  9. import java.awt.Image;
  10. import java.awt.*;
  11. import javax.swing.*;
  12. import java.util.ArrayList;
  13. import java.applet.AudioClip;
  14. import java.io.*;
  15. import java.net.*;
  16. import java.applet.*;
  17.  
  18. //eventually allow the user to select specific notes and play them at once.
  19. //what if the notes had constructors that made it so they had predefined coordinates?
  20. //why is does the select chord feature act buggy at times?
  21.  
  22.  
  23. public class PianoPlayer extends JApplet implements MouseListener, MouseMotionListener
  24. {
  25. private int takeAway;
  26. private int inc;
  27. private String lastAction;
  28. private Color turnOn;
  29. private Color disclamer;
  30. private Graphics2D g2d;
  31. private Color colorS;
  32. private Color colorL;
  33. private int clicked;
  34. private int songType;
  35. private Color color;
  36. private Color colorSingle;
  37. private Color colorMaj;
  38. private Color colorMin;
  39.  
  40. private Image piano;
  41. private Image barNorm;
  42. private Image barNormal;
  43. private Image barFinal;
  44. private Image typeBox;
  45. private Image wordSound;
  46. private Image typeBox2;
  47. private Image right;
  48. private Image left;
  49. private Image turner;
  50. private Image currentTurner;
  51. private int x;
  52. private int y;
  53. private int xBar;
  54. private int yBar;
  55. private static final int onWHITE = 365;
  56. // private static final int onBLACK = 0; //hasn't been used, but I hope to use it someday
  57. private int playBool;
  58. private static final int level = 0;
  59. private boolean runner;
  60. private boolean single;
  61. private boolean maj;
  62. private boolean min;
  63. public int turn;
  64. private Song singleMode = new Song(358, 453, 21, 10);
  65. private Song majMode = new Song(357, 467, 35, 25);
  66. private Song minMode = new Song(357, 467, 50, 40);
  67. private Song onRight = new Song(32, 41, 380, 366);
  68. private Song onLeft = new Song(17, 26, 380, 366);
  69. private Song board = new Song(51,215,392,110);
  70.  
  71.  
  72. private String note;
  73. private String currentSong = "";
  74. private int currentSongLength;
  75.  
  76. private String type;
  77. private String typeOn;
  78.  
  79. private Image mediant;
  80. private Image dominant;
  81. private int xMed;
  82. private int yMedDom;
  83. private int xDom;
  84. private int xE1;
  85. private int xA1;
  86. private int xD2;
  87. private int xG2;
  88. private int xB2;
  89. private int xE3;
  90.  
  91. private ArrayList<Keys> hold = new ArrayList<Keys>();// use this and an array of pics to create a loop? How would I display them?
  92. private String showHold;
  93. private boolean holder;
  94. private Color colorHold;
  95. private Song holdMode = new Song(358, 444, 66, 55);
  96. private Song play = new Song (450, 509, 66, 55);
  97. private Color colorPlay;
  98. private Song pianoBoard = new Song(52, 758, 393, 108);
  99. //================================================================================
  100.  
  101.  
  102. Sound eSound1;
  103. Sound aSound1;
  104. Sound dSound2;
  105. Sound gSound2;
  106. Sound bSound2;
  107. Sound eSound3;
  108.  
  109. //================================================================================
  110. private Image loading;
  111.  
  112. private Image pE1;
  113. private Image pA1;
  114. private Image pD2;
  115. private Image pG2;
  116. private Image pB2;
  117. private Image pE3;
  118.  
  119. //private Image pD1PRESS;
  120. private Image pE1PRESS;
  121. private Image pA1PRESS;
  122. private Image pD2PRESS;
  123. private Image pG2PRESS;
  124. private Image pB2PRESS;
  125. private Image pE3PRESS;
  126.  
  127.  
  128. private Image ePRESS1Add;
  129. private Image aPRESS1Add;
  130. private Image dPRESS2Add;
  131. private Image gPRESS2Add;
  132. private Image bPRESS2Add;
  133. private Image ePRESS3Add;
  134. //================================================================================
  135.  
  136. private White e1 = new White(153, 202, 174, 202, ePRESS1Add);
  137. private White a1 = new White(304, 354, 320, 347, aPRESS1Add);
  138. private White d2 = new White(457, 507, 465, 497, dPRESS2Add);
  139. private White g2 = new White(607, 657, 618, 644, gPRESS2Add);
  140. private White b2 = new White(709, 759, 732, 754, bPRESS2Add);
  141. private White e3 = new White(406, 456, 406, 436, ePRESS3Add);
  142.  
  143. //================================================================================
  144.  
  145. public PianoPlayer()
  146. {
  147. takeAway = 0;
  148. inc = 0;
  149. lastAction = "none";
  150. addMouseListener(this);
  151. addMouseMotionListener(this);
  152. colorS = Color.white;
  153. colorL = Color.white;
  154. color = Color.white;
  155.  
  156. colorSingle = Color.white;
  157. //colorMaj = Color.gray;
  158. //colorMin = Color.gray;
  159. //colorPlay = Color.black;
  160. piano = Toolkit.getDefaultToolkit().getImage("Piano.jpg");
  161. barNorm = Toolkit.getDefaultToolkit().getImage("BarNorm.jpg");
  162. barNormal = Toolkit.getDefaultToolkit().getImage("BarNorm.jpg");
  163. barFinal = Toolkit.getDefaultToolkit().getImage("BarFinal.jpg");
  164. typeBox = Toolkit.getDefaultToolkit().getImage("TypeBox.jpg");
  165. wordSound = Toolkit.getDefaultToolkit().getImage("Sound.jpg");
  166. typeBox2 = Toolkit.getDefaultToolkit().getImage("TypeBox2.jpg");
  167. right = Toolkit.getDefaultToolkit().getImage("Right.jpg");
  168. left = Toolkit.getDefaultToolkit().getImage("Left.jpg");
  169. runner = false;
  170. turn = 0;
  171.  
  172. note = "";
  173. xBar = 670;
  174. yBar = 21;
  175. currentSong = "";
  176. currentSongLength = 0;
  177. single = true;
  178. //maj = false;
  179. //min = false;
  180. playBool = 0;
  181. //turnOn = Color.black;
  182. //disclamer = Color.black;
  183. clicked = 0;
  184. type = "Note: ";
  185. songType = 1;
  186. currentTurner = typeBox;
  187. typeOn = "Piano";
  188. yMedDom = level;
  189.  
  190. xE1 = 146;
  191. xA1 = 296;
  192. xD2 = 446;
  193. xG2 = 602;
  194. xB2 = 701;
  195. xE3 = 396;
  196.  
  197. holder= false;
  198. colorHold = Color.gray;
  199. ePRESS1Add = Toolkit.getDefaultToolkit().getImage("EPRESS1add.jpg");
  200. aPRESS1Add = Toolkit.getDefaultToolkit().getImage("APRESS1add.jpg");
  201. dPRESS2Add = Toolkit.getDefaultToolkit().getImage("DPRESS2add.jpg");
  202. gPRESS2Add = Toolkit.getDefaultToolkit().getImage("GPRESS2add.jpg");
  203. bPRESS2Add = Toolkit.getDefaultToolkit().getImage("BPRESS2add.jpg");
  204. ePRESS3Add = Toolkit.getDefaultToolkit().getImage("EPRESS3add.jpg");
  205.  
  206.  
  207.  
  208. pE1 = Toolkit.getDefaultToolkit().getImage("E1.jpg");
  209. pA1 = Toolkit.getDefaultToolkit().getImage("A1.jpg");
  210. pD2 = Toolkit.getDefaultToolkit().getImage("D2.jpg");
  211. pG2 = Toolkit.getDefaultToolkit().getImage("G2.jpg");
  212. pB2 = Toolkit.getDefaultToolkit().getImage("B2.jpg");
  213. pE3 = Toolkit.getDefaultToolkit().getImage("E3.jpg");
  214.  
  215. loading = Toolkit.getDefaultToolkit().getImage("loader.jpg");
  216. pE1PRESS = Toolkit.getDefaultToolkit().getImage("EPRESS1.jpg");
  217. pA1PRESS = Toolkit.getDefaultToolkit().getImage("APRESS1.jpg");
  218. pD2PRESS = Toolkit.getDefaultToolkit().getImage("DPRESS2.jpg");
  219. pG2PRESS = Toolkit.getDefaultToolkit().getImage("GPRESS2.jpg");
  220. pB2PRESS = Toolkit.getDefaultToolkit().getImage("BPRESS2.jpg");
  221. pE3PRESS = Toolkit.getDefaultToolkit().getImage("EPRESS3.jpg");
  222. }
  223.  
  224. // private Keys[] myNotes = { c1, cSharp1, d1, dSharp1, e1, f1, fSharp1, g1, gSharp1, a1, aSharp1, b1,
  225. // c2, cSharp2, d2, dSharp2, e2, f2, fSharp2, g2, gSharp2, a2, aSharp2, b2, e3 };
  226.  
  227. public void paint(Graphics g)
  228. {
  229.  
  230. g2d = (Graphics2D) getGraphics();
  231.  
  232. g2d.drawImage(piano, 0, 0, this);
  233.  
  234. g2d.drawImage(mediant, xMed, yMedDom, this);
  235.  
  236. g2d.drawImage(dominant, xDom, yMedDom, this);
  237.  
  238. g2d.drawImage(barNorm, xBar, yBar, this);
  239. g2d.drawImage(barNormal, 670, 21, this);
  240. g2d.drawImage(barFinal, 670, 41, this);
  241.  
  242. g2d.setColor(Color.white);
  243. g2d.drawString(typeOn, 14, 316);
  244. g2d.drawImage(currentTurner, 10, 316, this);
  245. g2d.drawImage(turner, 17, 363, this);
  246.  
  247.  
  248.  
  249. //if (maj || min)
  250. // takeAway = 10;
  251. //else
  252. if ( !holder )
  253. takeAway = 0;
  254. g2d.setColor(color);
  255. g2d.drawString(type + note, (369 - takeAway), 106);
  256.  
  257.  
  258. // g2d.setColor(Color.white);
  259. //g2d.drawString("" + x + "," + y, x, y);
  260.  
  261. g2d.setColor(Color.white);
  262. //g2d.drawString("= Normal guide note" , 689, 32);
  263. //g2d.drawString("= Final guide note" , 689, 52);
  264.  
  265. g2d.setColor(colorSingle);
  266. g2d.drawString("Single Note Mode" , 357, 21);
  267.  
  268. g2d.setColor(colorMaj);
  269. g2d.drawString("Major Chord Mode" , 356, 36);
  270.  
  271. g2d.setColor(colorMin);
  272. g2d.drawString("Minor Chord Mode" , 356, 51);
  273.  
  274. g2d.setColor(colorHold);
  275. g2d.drawString("Personal Chord", 358, 65);
  276. g2d.setColor(colorPlay);
  277. g2d.drawString("Play Chord", 450, 65);
  278.  
  279. g2d.setColor(turnOn);
  280. g2d.drawString("One mode must be selected to make sound", 285,80);
  281.  
  282.  
  283. }
  284.  
  285. public void restore()
  286. {
  287. colorS = Color.white;
  288. colorL = Color.white;
  289. xBar = 670;
  290. yBar = 21;
  291. turn = 0;
  292. runner = false;
  293. currentSong = "";
  294. currentSongLength = 0;
  295. barNorm = barNormal;
  296. takeAway = 0;
  297. }
  298.  
  299. public void restoreChords()
  300. {
  301. mediant = null;
  302. dominant = null;
  303. }
  304.  
  305. // TheSounds=====================================
  306.  
  307. public void e1()
  308. {
  309. if (songType == 1)
  310. {
  311. Sound eSound1 = new Sound(this, "sE1.wav");
  312. eSound1.playSoundOnce();
  313. }
  314.  
  315. }
  316.  
  317. public void a1()
  318. {
  319. if (songType == 1)
  320. {
  321. Sound aSound1 = new Sound(this, "sA1.wav");
  322. aSound1.playSoundOnce();
  323. }
  324.  
  325. }
  326.  
  327. public void d2()
  328. {
  329. if (songType == 1)
  330. {
  331. Sound dSound2 = new Sound(this, "sD2.wav");
  332. dSound2.playSoundOnce();
  333. }
  334.  
  335. }
  336.  
  337. public void g2()
  338. {
  339. if (songType == 1)
  340. {
  341. Sound gSound2 = new Sound(this, "sG2.wav");
  342. gSound2.playSoundOnce();
  343. }
  344.  
  345. }
  346.  
  347. public void b2()
  348. {
  349. if (songType == 1)
  350. {
  351. Sound bSound2 = new Sound(this, "sB2.wav");
  352. bSound2.playSoundOnce();
  353. }
  354.  
  355. }
  356.  
  357. public void e3()
  358. {
  359. if (songType == 1)
  360. {
  361. Sound eSound3 = new Sound(this, "sE3.wav");
  362. bSound2.playSoundOnce();
  363. }
  364.  
  365. }
  366.  
  367. public void mouseClicked(MouseEvent e)
  368. {
  369. x = e.getX();
  370. y = e.getY();
  371.  
  372.  
  373. if ((!single) && (!min) && (!maj) && (!holder))
  374. turnOn = Color.red;
  375. else
  376. turnOn = Color.black;
  377.  
  378. // if ( (maj || min) && clicked < 3)
  379. // disclamer = Color.red;
  380. // else
  381. // {
  382. // disclamer = Color.black;
  383. // }
  384.  
  385. if ( single )
  386. {
  387. //del49
  388. //del55
  389. if( e1.onKey(x, y) )
  390. {
  391. e1();
  392. }
  393.  
  394. else if( a1.onKey(x, y) )
  395. {
  396. a1();
  397. }
  398.  
  399. else if( d2.onKey(x, y) )
  400. {
  401. d2();
  402. }
  403.  
  404. else if( g2.onKey(x, y) )
  405. {
  406. g2();
  407. }
  408.  
  409. else if( b2.onKey(x, y) )
  410. {
  411. b2();
  412. }
  413.  
  414. else if( e3.onKey(x, y) )
  415. {
  416. e3();
  417. }
  418. }
  419.  
  420. }
  421.  
  422. public void mouseEntered(MouseEvent e)
  423. {
  424. }
  425.  
  426. public void mouseExited(MouseEvent e)
  427. { }
  428.  
  429. public void mousePressed(MouseEvent e)
  430. {
  431. x = e.getX();
  432. y = e.getY();
  433. if ( holder && pianoBoard.isIn(x,y) )
  434. takeAway+=9;
  435.  
  436. //Del1
  437.  
  438. else if (singleMode.isIn(x,y) )
  439. {
  440. colorSingle = Color.darkGray;
  441. //colorMaj = Color.gray;
  442. //colorMin = Color.gray;
  443. colorHold = Color.gray;
  444. repaint();//why do I need this here for this to work?
  445. }
  446. else if (holdMode.isIn(x,y) )
  447. {
  448. colorHold = Color.darkGray;
  449. colorSingle = Color.gray;
  450. //colorMaj = Color.gray;
  451. //colorMin = Color.gray;
  452. repaint();//why do I need this here for this to work?
  453. }
  454. else if (play.isIn(x,y))
  455. colorPlay = Color.gray;
  456. if (onRight.isIn(x,y) )
  457. turner = right;
  458. else if (onLeft.isIn(x,y) )
  459. turner = left;
  460.  
  461. else if( e1.onKey(x, y) )
  462. {
  463. piano = pE1PRESS;
  464. //del6
  465. if (holder)
  466. {
  467. hold.add(e1);
  468. note += " E, ";
  469. }
  470. }
  471.  
  472. else if( a1.onKey(x, y) )
  473. {
  474. piano = pA1PRESS;
  475. //del11
  476. if (holder)
  477. {
  478. hold.add(a1);
  479. note += " A, ";
  480. }
  481. }
  482.  
  483. else if( d2.onKey(x, y) )
  484. {
  485. piano = pD2PRESS;
  486. //del16
  487. if (holder)
  488. {
  489. hold.add(d2);
  490. note += " D, ";
  491. }
  492. }
  493.  
  494. else if( g2.onKey(x, y) )
  495. {
  496. piano = pG2PRESS;
  497. if (holder)
  498. {
  499. hold.add(g2);
  500. note += " G, ";
  501. }
  502. }
  503.  
  504. else if( b2.onKey(x, y) )
  505. {
  506. piano = pB2PRESS;
  507. if (holder)
  508. {
  509. hold.add(b2);
  510. note += " B, ";
  511. }
  512. }
  513.  
  514. else if( e3.onKey(x, y) )
  515. {
  516. piano = pE3PRESS;
  517. if (holder)
  518. {
  519. hold.add(e3);
  520. note += " E, ";
  521. }
  522. }
  523. else
  524. {
  525. piano = Toolkit.getDefaultToolkit().getImage( "Piano.jpg" );
  526. }
  527. repaint();
  528. }
  529.  
  530. public void mouseReleased(MouseEvent e)
  531. {
  532. x = e.getX();
  533. y = e.getY();
  534. if (singleMode.isIn(x,y) )
  535. {
  536. if ( single )
  537. {
  538. colorSingle = Color.gray;
  539. single = false;
  540. }
  541. else
  542. {
  543. colorSingle = Color.white;
  544. single = true;
  545. note = "";
  546. type = "Note: ";
  547. //maj = false;
  548. //colorMaj = Color.gray;
  549. //min = false;
  550. //colorMin = Color.gray;
  551. clicked = 0;
  552. holder = false;
  553. colorHold = Color.gray;
  554. hold.clear();
  555. }
  556. }
  557.  
  558. else if ( holdMode.isIn(x,y) )
  559. {
  560. if ( holder )
  561. {
  562. colorHold = Color.gray;
  563. holder = false;
  564. }
  565. else
  566. {
  567. holder = true;
  568. note = "";
  569. colorHold = Color.white;
  570. type = "Your Chord: ";
  571. //maj = false;
  572. //colorMaj = Color.gray;
  573. single = false;
  574. colorSingle = Color.gray;
  575. //min = false;
  576. //colorMin = Color.gray;
  577. hold.clear();
  578. }
  579. }
  580. if ( !holder )
  581. colorPlay = Color.black;
  582. if ( holder )
  583. {
  584. if (hold.size() < 2)
  585. colorPlay = Color.gray;
  586. else
  587. {
  588. colorPlay = Color.red;//why does it not realize hold has been cleared? Why do i need to use inc?
  589. }
  590. if ( colorPlay == Color.red && play.isIn(x,y) )
  591. {
  592. colorPlay = Color.gray;
  593. }
  594. }
  595.  
  596. if (onRight.isIn(x,y) )
  597. {
  598. turner = null;
  599. if (songType == 1)
  600. {
  601. currentTurner = typeBox2;
  602. songType = 2;
  603. typeOn = "Guitar";
  604. }
  605. else
  606. {
  607. currentTurner = typeBox;
  608. songType = 1;
  609. typeOn = "Piano";
  610. }
  611. }
  612. else if (onLeft.isIn(x,y) )
  613. {
  614. turner = null;
  615. if (songType == 1)
  616. {
  617. currentTurner = typeBox2;
  618. songType = 2;
  619. typeOn = "Guitar";
  620. }
  621. else
  622. {
  623. currentTurner = typeBox;
  624. songType = 1;
  625. typeOn = "Piano";
  626. }
  627. }
  628.  
  629. if( e1.onKey(x, y) )
  630. {
  631. piano = pE1;
  632. }
  633.  
  634. else if( a1.onKey(x, y) )
  635. {
  636. piano = pA1;
  637. }
  638.  
  639. else if( d2.onKey(x, y) )
  640. {
  641. piano = pD2;
  642. }
  643.  
  644. else if( g2.onKey(x, y) )
  645. {
  646. piano = pG2;
  647. }
  648.  
  649. else if( b2.onKey(x, y) )
  650. {
  651. piano = pB2;
  652. }
  653.  
  654. else if( e3.onKey(x, y) )
  655. {
  656. piano = pE3;
  657. }
  658. else
  659. {
  660. piano = Toolkit.getDefaultToolkit().getImage( "Piano.jpg" );
  661. }
  662.  
  663.  
  664. if ( holder )
  665. {
  666. if (play.isIn(x,y) )
  667. {
  668. for (int rep = 0; rep < hold.size(); rep++)
  669. {
  670. //del51
  671. //del45
  672. //del58
  673.  
  674. if( hold.get(rep) == e1 )
  675. {
  676. e1();
  677. }
  678.  
  679. if( hold.get(rep) == a1 )
  680. {
  681. a1();
  682. }
  683.  
  684. if( hold.get(rep) == d2 )
  685. {
  686. d2();
  687. }
  688.  
  689. if( hold.get(rep) == g2 )
  690. {
  691. g2();
  692. }
  693.  
  694. if( hold.get(rep) == b2 )
  695. {
  696. b2();
  697. }
  698.  
  699. if( hold.get(rep) == e3 )
  700. {
  701. e3();
  702. }
  703. hold.remove(rep--); //why does this work?????????????????????????????? why should there be a --?
  704. }
  705. note = "";
  706. takeAway = 0;
  707. }
  708. }
  709. repaint();
  710. restoreChords();
  711. }
  712.  
  713. // =======================================
  714.  
  715. public void mouseMoved(MouseEvent e)
  716. {
  717. x = e.getX();
  718. y = e.getY();
  719. //del52
  720. //del59
  721. if( e1.onKey(x, y) )
  722. {
  723. piano = pE1;
  724. if (single)
  725. note ="E";
  726. }
  727.  
  728. else if( a1.onKey(x, y) )
  729. {
  730. piano = pA1;
  731. if (single)
  732. note ="A";
  733. }
  734.  
  735. else if( d2.onKey(x, y) )
  736. {
  737. piano = pD2;
  738. if (single)
  739. note ="D";
  740. }
  741.  
  742. else if( g2.onKey(x, y) )
  743. {
  744. piano = pG2;
  745. if (single)
  746. note ="G";
  747. else if (!holder && !single )
  748. note = "";
  749. }
  750.  
  751. else if( b2.onKey(x, y) )
  752. {
  753. piano = pB2;
  754. if (single)
  755. note ="B";
  756. else if (!holder && !single )
  757. note = "";
  758. }
  759.  
  760. else if( e3.onKey(x, y) )
  761. {
  762. piano = pE3;
  763. if (single)
  764. note ="E";
  765. //else if (!holder && !single )
  766. //note = "";
  767. }
  768. else if ( !holder )
  769. {
  770. note = "";
  771. }
  772.  
  773. repaint();
  774. }
  775.  
  776. public void mouseDragged(MouseEvent e)
  777. {
  778. x = e.getX();
  779. y = e.getY();
  780. if ( !holder )
  781. {
  782. //del53
  783. //del60
  784.  
  785. if( e1.onKey(x, y) )
  786. {
  787. e1();
  788. }
  789.  
  790. else if( a1.onKey(x, y) )
  791. {
  792. a1();
  793. }
  794.  
  795. else if( d2.onKey(x, y) )
  796. {
  797. d2();
  798. }
  799.  
  800. else if( g2.onKey(x, y) )
  801. {
  802. g2();
  803. }
  804.  
  805. else if( b2.onKey(x, y) )
  806. {
  807. b2();
  808. }
  809.  
  810. else if( e3.onKey(x, y) )
  811. {
  812. e3();
  813. }
  814. else
  815. {
  816.  
  817. }
  818. }
  819. }
  820. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement