MenuButton.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package model.objects;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics2D;
  6. import java.awt.LinearGradientPaint;
  7. import java.awt.Paint;
  8. import java.awt.geom.GeneralPath;
  9. import java.awt.geom.Point2D;
  10. import java.util.ArrayList;
  11. import audio.Song;
  12. public class MenuButton {
  13. private ArrayList<GeneralPath> buttonparts;
  14. private GeneralPath border, line;
  15. private ArrayList<Color> colors;
  16. private Paint gradient;
  17. private int x, y, rounding;
  18. private double scalefactor;
  19. private boolean selected;
  20. private int fadecounter;
  21. private Song song;
  22. public MenuButton(int x, int y, double scale, int rounding, Color c0, Song song){
  23. this.x = x;
  24. this.y = y;
  25. this.scalefactor = scale;
  26. this.rounding = rounding;
  27. calculateButton();
  28. setSong(song);
  29. colors = new ArrayList<Color>();
  30. Color c1 = c0.darker();
  31. Color c2 = c0.darker().darker();
  32. Color c3 = c0.darker().darker().darker();
  33. c1 = new Color((int)(c1.getRed()*0.8), (int)(c1.getGreen()*0.8),(int)(c1.getBlue()*0.8));
  34. c2 = new Color((int)(c2.getRed()*0.8), (int)(c2.getGreen()*0.8),(int)(c2.getBlue()*0.8));
  35. c3 = new Color((int)(c3.getRed()*0.8), (int)(c3.getGreen()*0.8),(int)(c3.getBlue()*0.8));
  36. colors.add(c2);
  37. colors.add(c3);
  38. colors.add(c2);
  39. colors.add(c0);
  40. colors.add(c1);
  41. colors.add(c0);
  42. gradient = new LinearGradientPaint(
  43. new Point2D.Double((-100)*scalefactor,(512)*scalefactor),
  44. new Point2D.Double((600)*scalefactor,(512)*scalefactor),
  45. new float[]{0.0f, 1.0f},
  46. new Color[]{new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), 10), c0});
  47. }
  48. public void calculateButton(){
  49. buttonparts = new ArrayList<GeneralPath>();
  50. buttonparts.add(arrayToGeneralpath(new int[][]{{449, 1}, {449, 68},{113, 103}, {106, 35}})); //background right
  51. buttonparts.add(arrayToGeneralpath(new int[][]{{113, 103}, {106, 35},{70, 12+rounding}, {77, 88+rounding}})); //background middle
  52. buttonparts.add(arrayToGeneralpath(new int[][]{{70, 12+rounding}, {77, 88+rounding},{4, 104+(int)(rounding*0.5)}, {-4, 32+(int)(rounding*0.5)}})); //background left
  53. buttonparts.add(arrayToGeneralpath(new int[][]{{449, 15}, {449, 54},{118, 88}, {114, 48}})); //foreground right
  54. buttonparts.add(arrayToGeneralpath(new int[][]{{118, 88}, {114, 48},{78, 25+rounding}, {82, 73+rounding}})); //foreground middle
  55. buttonparts.add(arrayToGeneralpath(new int[][]{{78, 25+rounding}, {82, 73+rounding},{15, 88+(int)(rounding*0.5)}, {10, 43+(int)(rounding*0.5)}})); //foreground left
  56. border = arrayToGeneralpath(new int[][]{{449,15}, {114,48},{78,25+rounding},{10,43+(int)(rounding*0.5)},{15,88+(int)(rounding*0.5)},{82,73+rounding},{118,88},{449,54}});
  57. line = arrayToGeneralpath(new int[][]{{-11,55},{-40,60},{-38,78},{-358,170-rounding*3},{-358,174-rounding*3},{-37,87},{-35,102},{-5,95}});
  58. }
  59. public GeneralPath arrayToGeneralpath(int block[][]){
  60. GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, block.length);
  61. polyline.moveTo ((block[0][0]+x)*scalefactor, (block[0][1]+y)*scalefactor);
  62. for (int index = 1; index < block.length; index++) {
  63. polyline.lineTo((block[index][0]+x)*scalefactor, (block[index][1]+y)*scalefactor);
  64. };
  65. polyline.closePath();
  66. return polyline;
  67. }
  68. public void draw(Graphics2D g2d){
  69. for(int i = 0; i < buttonparts.size(); i++){ //fill every part of the button with a specific color
  70. g2d.setColor(colors.get(i));
  71. g2d.fill((buttonparts.get(i)));
  72. }
  73. if(selected){
  74. g2d.setStroke(new BasicStroke(4));
  75. g2d.setColor(Color.BLACK);
  76. g2d.draw(border);
  77. }
  78. g2d.setPaint(gradient);
  79. g2d.fill(line);
  80. //draw text
  81. g2d.setColor(Color.BLACK);
  82. Font textFont = new Font("OCR A Extended", Font.BOLD, (int)(30*scalefactor));
  83. g2d.setFont(textFont);
  84. g2d.translate((x+160)*scalefactor, (y+74)*scalefactor);
  85. g2d.rotate(-0.1);
  86. g2d.drawString(song.getTitle(), 0, 0);
  87. g2d.rotate(0.1);
  88. g2d.translate(-(x+160)*scalefactor, -(y+74)*scalefactor);
  89. }
  90. public void update(){
  91. if(selected && fadecounter < 5){
  92. x += 8;
  93. y -= 8;
  94. scalefactor += 0.04;
  95. fadecounter++;
  96. calculateButton();
  97. }else if(!selected && fadecounter >0){
  98. x += 8;
  99. y += 8;
  100. fadecounter--;
  101. scalefactor -= 0.04;
  102. calculateButton();
  103. }
  104. }
  105. public void setSelected(boolean selected) {
  106. this.selected = selected;
  107. }
  108. public boolean isSelected(){
  109. return selected;
  110. }
  111. public void setX(int x){
  112. this.x = x;
  113. calculateButton();
  114. }
  115. public int getX() {
  116. return x;
  117. }
  118. public Song getSong() {
  119. return song;
  120. }
  121. public void setSong(Song song) {
  122. this.song = song;
  123. }
  124. }