MenuButton.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.RenderingHints;
  9. import java.awt.geom.GeneralPath;
  10. import java.awt.geom.Point2D;
  11. import java.awt.image.BufferedImage;
  12. import java.util.ArrayList;
  13. import audio.Song;
  14. public class MenuButton {
  15. private int x, y, rounding;
  16. private double scalefactor;
  17. private Color buttoncolor;
  18. private boolean selected;
  19. private BufferedImage background;
  20. private Song song;
  21. public MenuButton(int x, int y, double scale, int rounding, Color c0, Song song){
  22. this.x = x;
  23. this.y = y;
  24. this.scalefactor = scale;
  25. this.rounding = rounding;
  26. this.buttoncolor = c0;
  27. setSong(song);
  28. calculateButton();
  29. }
  30. public void calculateButton(){
  31. Color c1 = buttoncolor.darker();
  32. Color c2 = buttoncolor.darker().darker();
  33. Color c3 = buttoncolor.darker().darker().darker();
  34. c1 = new Color((int)(c1.getRed()*0.8), (int)(c1.getGreen()*0.8),(int)(c1.getBlue()*0.8));
  35. c2 = new Color((int)(c2.getRed()*0.8), (int)(c2.getGreen()*0.8),(int)(c2.getBlue()*0.8));
  36. c3 = new Color((int)(c3.getRed()*0.8), (int)(c3.getGreen()*0.8),(int)(c3.getBlue()*0.8));
  37. GeneralPath 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}});
  38. GeneralPath line = arrayToGeneralpath(new int[][]{{-11,55},{-40,60},{-38,78},{-358,170-rounding*3},{-358,174-rounding*3},{-37,87},{-35,102},{-5,95}});
  39. Paint gradient = new LinearGradientPaint( new Point2D.Double((-100)*scalefactor,(512)*scalefactor),
  40. new Point2D.Double((600)*scalefactor,(512)*scalefactor),
  41. new float[]{0.0f, 1.0f},
  42. new Color[]{new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), 10), buttoncolor});
  43. //Draw the generated button to the buffered image
  44. background = new BufferedImage(1280, 1024, BufferedImage.TYPE_4BYTE_ABGR);
  45. Graphics2D g2d = background.createGraphics();
  46. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  47. g2d.setRenderingHints(rh);
  48. g2d.setColor(c2);
  49. g2d.fill(arrayToGeneralpath(new int[][]{{449, 1}, {449, 68},{113, 103}, {106, 35}}));
  50. g2d.setColor(c3);
  51. g2d.fill(arrayToGeneralpath(new int[][]{{113, 103}, {106, 35},{70, 12+rounding}, {77, 88+rounding}}));
  52. g2d.setColor(c2);
  53. g2d.fill(arrayToGeneralpath(new int[][]{{70, 12+rounding}, {77, 88+rounding},{4, 104+(int)(rounding*0.5)}, {-4, 32+(int)(rounding*0.5)}}));
  54. g2d.setColor(buttoncolor);
  55. g2d.fill(arrayToGeneralpath(new int[][]{{449, 15}, {449, 54},{118, 88}, {114, 48}}));
  56. g2d.setColor(c1);
  57. g2d.fill(arrayToGeneralpath(new int[][]{{118, 88}, {114, 48},{78, 25+rounding}, {82, 73+rounding}}));
  58. g2d.setColor(buttoncolor);
  59. g2d.fill(arrayToGeneralpath(new int[][]{{78, 25+rounding}, {82, 73+rounding},{15, 88+(int)(rounding*0.5)}, {10, 43+(int)(rounding*0.5)}}));
  60. if(selected){
  61. g2d.setStroke(new BasicStroke(4));
  62. g2d.setColor(Color.BLACK);
  63. g2d.draw(border);
  64. }
  65. g2d.setPaint(gradient);
  66. g2d.fill(line);
  67. //draw text
  68. g2d.setColor(Color.BLACK);
  69. g2d.setFont(new Font("OCR A Extended", Font.BOLD, (int)(30*scalefactor)));
  70. g2d.translate((160+358)*scalefactor, (74)*scalefactor);
  71. g2d.rotate(-0.1);
  72. g2d.drawString(song.getTitle(), 0, 0);
  73. background.createGraphics();
  74. }
  75. public GeneralPath arrayToGeneralpath(int block[][]){
  76. GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, block.length);
  77. polyline.moveTo ((block[0][0]+358)*scalefactor, (block[0][1])*scalefactor);
  78. for (int index = 1; index < block.length; index++) {
  79. polyline.lineTo((block[index][0]+358)*scalefactor, (block[index][1])*scalefactor);
  80. };
  81. polyline.closePath();
  82. return polyline;
  83. }
  84. public void draw(Graphics2D g2d){
  85. g2d.drawImage(background, (int)((x-320)*scalefactor), (int) ((y)*scalefactor), 1280, 1024, null);
  86. }
  87. public void update(){
  88. }
  89. public void setSelected(boolean selected) {
  90. this.selected = selected;
  91. x += 100;
  92. y -= 40;
  93. scalefactor += 0.2;
  94. calculateButton();
  95. }
  96. public boolean isSelected(){
  97. return selected;
  98. }
  99. public void setX(int x){
  100. this.x = x;
  101. }
  102. public int getX() {
  103. return x;
  104. }
  105. public Song getSong() {
  106. return song;
  107. }
  108. public void setSong(Song song) {
  109. this.song = song;
  110. }
  111. }