MenuButton.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 c0){
  22. this.x = x;
  23. this.y = y;
  24. this.scalefactor = scale;
  25. this.text = text;
  26. this.rounding = rounding;
  27. calculateButton();
  28. colors = new ArrayList<Color>();
  29. Color c1 = c0.darker();
  30. Color c2 = c0.darker().darker();
  31. Color c3 = c0.darker().darker().darker();
  32. c1 = new Color((int)(c1.getRed()*0.8), (int)(c1.getGreen()*0.8),(int)(c1.getBlue()*0.8));
  33. c2 = new Color((int)(c2.getRed()*0.8), (int)(c2.getGreen()*0.8),(int)(c2.getBlue()*0.8));
  34. c3 = new Color((int)(c3.getRed()*0.8), (int)(c3.getGreen()*0.8),(int)(c3.getBlue()*0.8));
  35. colors.add(c2);
  36. colors.add(c3);
  37. colors.add(c2);
  38. colors.add(c0);
  39. colors.add(c1);
  40. colors.add(c0);
  41. gradient = new LinearGradientPaint(
  42. new Point2D.Double((-100)*scalefactor,(512)*scalefactor),
  43. new Point2D.Double((600)*scalefactor,(512)*scalefactor),
  44. new float[]{0.0f, 1.0f},
  45. new Color[]{new Color(c1.getRed(), c1.getGreen(), c1.getBlue(), 10), c0});
  46. }
  47. public void calculateButton(){
  48. buttonparts = new ArrayList<GeneralPath>();
  49. buttonparts.add(arrayToGeneralpath(new int[][]{{449, 1}, {449, 68},{113, 103}, {106, 35}})); //background right
  50. buttonparts.add(arrayToGeneralpath(new int[][]{{113, 103}, {106, 35},{70, 12+rounding}, {77, 88+rounding}})); //background middle
  51. 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
  52. buttonparts.add(arrayToGeneralpath(new int[][]{{449, 15}, {449, 54},{118, 88}, {114, 48}})); //foreground right
  53. buttonparts.add(arrayToGeneralpath(new int[][]{{118, 88}, {114, 48},{78, 25+rounding}, {82, 73+rounding}})); //foreground middle
  54. 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
  55. 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}});
  56. line = arrayToGeneralpath(new int[][]{{-11,55},{-40,60},{-38,78},{-358,170-rounding*3},{-358,174-rounding*3},{-37,87},{-35,102},{-5,95}});
  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 part of the button with a specific color
  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 void setSelected(boolean selected) {
  105. this.selected = selected;
  106. }
  107. public void setX(int x){
  108. this.x = x;
  109. calculateButton();
  110. }
  111. public int getX() {
  112. return x;
  113. }
  114. }