MenuButton.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. public class MenuButton {
  12. private ArrayList<GeneralPath> buttonparts;
  13. private GeneralPath border, line;
  14. private ArrayList<Color> colors;
  15. private Paint gradient;
  16. private int x, y, rounding;
  17. private double scalefactor;
  18. private String text;
  19. private boolean selected;
  20. private int fadecounter;
  21. public MenuButton(int x, int y, double scale, String text, int rounding, Color color){
  22. this.x = x;
  23. this.y = y;
  24. this.scalefactor = scale;
  25. this.text = text;
  26. this.rounding = rounding;
  27. calculateButton();
  28. line = arrayToGeneralpath(new int[][]{{-11,55},{-40,60},{-38,78},{-358,170-rounding*3},{-358,174-rounding*3},{-37,87},{-35,102},{-5,95}});
  29. gradient = new LinearGradientPaint(
  30. new Point2D.Double((-358+x)*scalefactor,(167+y)*scalefactor),
  31. new Point2D.Double((-7+x)*scalefactor,(70+y)*scalefactor),
  32. new float[]{0.0f, 1.0f},
  33. new Color[]{new Color(color.getRed(), color.getGreen(), color.getBlue(), 10), color});
  34. colors = new ArrayList<Color>();
  35. // colors.add(new Color(47,153,21,255));
  36. // colors.add(new Color(36,120,16,255));
  37. // colors.add(new Color(48,154,22,255));
  38. // colors.add(new Color(77,254,41,255));
  39. // colors.add(new Color(61,201,32,255));
  40. // colors.add(new Color(77,254,41,255));
  41. colors.add(color.darker().darker());
  42. colors.add(color.darker().darker().darker());
  43. colors.add(color.darker().darker());
  44. colors.add(color);
  45. colors.add(color.darker());
  46. colors.add(color);
  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. }
  58. public GeneralPath arrayToGeneralpath(int block[][]){
  59. GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, block.length);
  60. polyline.moveTo ((block[0][0]+x)*scalefactor, (block[0][1]+y)*scalefactor);
  61. for (int index = 1; index < block.length; index++) {
  62. polyline.lineTo((block[index][0]+x)*scalefactor, (block[index][1]+y)*scalefactor);
  63. };
  64. polyline.closePath();
  65. return polyline;
  66. }
  67. public void draw(Graphics2D g2d){
  68. for(int i = 0; i < buttonparts.size(); i++){ //fill every button part
  69. g2d.setColor(colors.get(i));
  70. g2d.fill((buttonparts.get(i)));
  71. }
  72. if(selected){
  73. g2d.setStroke(new BasicStroke(4));
  74. g2d.setColor(Color.BLACK);
  75. g2d.draw(border);
  76. }
  77. g2d.setPaint(gradient);
  78. g2d.fill(line);
  79. //draw text
  80. g2d.setColor(Color.BLACK);
  81. Font textFont = new Font("OCR A Extended", Font.BOLD, (int)(30*scalefactor));
  82. g2d.setFont(textFont);
  83. g2d.translate((x+160)*scalefactor, (y+74)*scalefactor);
  84. g2d.rotate(-0.1);
  85. g2d.drawString(text, 0, 0);
  86. g2d.rotate(0.1);
  87. g2d.translate(-(x+160)*scalefactor, -(y+74)*scalefactor);
  88. }
  89. public void update(){
  90. if(selected && fadecounter < 5){
  91. x -= 8;
  92. y -=8;
  93. scalefactor += 0.04;
  94. fadecounter++;
  95. calculateButton();
  96. }else if(!selected && fadecounter >0){
  97. x += 8;
  98. y += 8;
  99. fadecounter--;
  100. scalefactor -= 0.04;
  101. calculateButton();
  102. }
  103. }
  104. public boolean isSelected() {
  105. return selected;
  106. }
  107. public void setSelected(boolean selected) {
  108. this.selected = selected;
  109. }
  110. }