Skip to content

Commit

Permalink
fix tooltip ui
Browse files Browse the repository at this point in the history
  • Loading branch information
akmsw committed Jun 24, 2024
1 parent 152df7b commit f65b096
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
import armameeldoparti.models.Error;
import armameeldoparti.utils.common.CommonFunctions;
import armameeldoparti.utils.common.Constants;
import java.awt.AlphaComposite;
import armameeldoparti.utils.common.custom.graphical.ui.CustomToolTipUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.IllegalComponentStateException;
import java.awt.Insets;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ToolTipUI;

/**
* A custom tooltip that fits the overall program aesthetics.
Expand Down Expand Up @@ -74,53 +68,4 @@ public void addNotify() {
public Insets getInsets() {
return Constants.INSETS_TOOLTIP;
}

// ---------- Private inner classes ----------------------------------------------------------------------------------------------------------------

/**
* Private, internal class that establishes the tooltip UI.
*/
private class CustomToolTipUI extends ToolTipUI {

// ---------- Public methods ---------------------------------------------------------------------------------------------------------------------

@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D) g.create();

RoundRectangle2D roundedRect = new RoundRectangle2D.Float(0,
0,
(c.getWidth() - 1),
(c.getHeight() - 1),
Constants.ROUNDED_BORDER_ARC_TOOLTIP,
Constants.ROUNDED_BORDER_ARC_TOOLTIP);

// Round rectangle configuration
g2.setRenderingHints(Constants.MAP_RENDERING_HINTS);
g2.setComposite(AlphaComposite.Clear);
g2.fill(roundedRect);

// Background painting
g2.setComposite(AlphaComposite.Src);
g2.setColor(Constants.COLOR_GREEN_DARK_MEDIUM);
g2.fill(roundedRect);

// Text painting
FontMetrics fm = g2.getFontMetrics();

String text = ((JToolTip) c).getTipText();

g2.setColor(Color.WHITE);
g2.drawString(text, (c.getWidth() - fm.stringWidth(text)) / 2, (c.getHeight() - fm.getHeight()) / 2 + fm.getAscent());
g2.dispose();
}

@Override
public Dimension getPreferredSize(JComponent c) {
FontMetrics fm = c.getFontMetrics(c.getFont());

return new Dimension(fm.stringWidth(((JToolTip) c).getTipText()) + getInsets().left + getInsets().right,
fm.getHeight() + getInsets().top + getInsets().bottom);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package armameeldoparti.utils.common.custom.graphical.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.plaf.ToolTipUI;

import armameeldoparti.utils.common.Constants;

public class CustomToolTipUI extends ToolTipUI {

// ---------- Public methods ---------------------------------------------------------------------------------------------------------------------

@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D) g.create();

RoundRectangle2D roundedRect = new RoundRectangle2D.Float(0, 0, c.getWidth(), c.getHeight(),
Constants.ROUNDED_BORDER_ARC_TOOLTIP,
Constants.ROUNDED_BORDER_ARC_TOOLTIP);

// Round rectangle configuration
g2.setRenderingHints(Constants.MAP_RENDERING_HINTS);
g2.setComposite(AlphaComposite.Clear);
g2.fill(roundedRect);

// Background painting
g2.setComposite(AlphaComposite.Src);
g2.setColor(Constants.COLOR_GREEN_DARK_MEDIUM);
g2.fill(roundedRect);

// Text painting
FontMetrics fm = g2.getFontMetrics();

String text = ((JToolTip) c).getTipText();

g2.setColor(Color.WHITE);
g2.drawString(text, (c.getWidth() - fm.stringWidth(text)) / 2, (c.getHeight() - fm.getHeight()) / 2 + fm.getAscent());
g2.dispose();
}

@Override
public Dimension getPreferredSize(JComponent c) {
FontMetrics fm = c.getFontMetrics(c.getFont());

return new Dimension(fm.stringWidth(((JToolTip) c).getTipText()) + c.getInsets().left + c.getInsets().right,
fm.getHeight() + c.getInsets().top + c.getInsets().bottom);
}
}

0 comments on commit f65b096

Please sign in to comment.