Advertisement
Guest User

tuner

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