Guest User

Untitled

a guest
Apr 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public static void setTooltip(Component cmp, String t) {
  2. cmp.putClientProperty("tooltip", t);
  3. }
  4.  
  5. public static void showTooltip(Component cmp, Form f) {
  6. final String t = (String)cmp.getClientProperty("tooltip");
  7. if(t == null) {
  8. f.setGlassPane(null);
  9. } else {
  10. int offset = Display.getInstance().convertToPixels(2) + 10;
  11. final Font fnt = cmp.getUIManager().getComponentStyle("Label").getFont();
  12. int width = fnt.stringWidth(t);
  13. int height = fnt.getHeight();
  14.  
  15. int x = cmp.getAbsoluteX();
  16. if(x > f.getWidth() / 2) {
  17. x -= (width + offset);
  18. } else {
  19. x += cmp.getWidth() + offset;
  20. }
  21. int y = cmp.getAbsoluteY();
  22. if(y > f.getHeight() / 2) {
  23. y -= (height + offset);
  24. } else {
  25. y += cmp.getHeight() + offset;
  26. }
  27.  
  28. final Rectangle rect = new Rectangle(x, y, width + 10, height + 10);
  29. final Rectangle shadow = new Rectangle(x + 5, y + 5, width + 10, height + 10);
  30. final Stroke s = new Stroke(3, Stroke.CAP_ROUND, Stroke.JOIN_ROUND, 1);
  31. f.setGlassPane(new Painter() {
  32. @Override
  33. public void paint(Graphics g, Rectangle r) {
  34. g.setColor(0);
  35. g.setAlpha(120);
  36. g.fillShape(shadow);
  37. g.setAlpha(255);
  38. g.setColor(0xeeeeee);
  39. g.fillShape(rect);
  40. g.setColor(0);
  41. g.drawShape(rect, s);
  42. g.setColor(0);
  43. g.setFont(fnt);
  44. g.drawString(t, rect.getX() + 5, rect.getY() + 5);
  45. }
  46. });
  47. }
  48. }
  49.  
  50. @Override
  51. public void pointerHover(int[] x, int[] y) {
  52. Component c = getComponentAt(x[0], y[0]);
  53. if(c != null) {
  54. GuiBuilderUtils.showTooltip(c, this);
  55. }
  56. }
Add Comment
Please, Sign In to add comment