What about trying to shape a tab that is not selected into a pentagon?


Note: This example does not test when JTabbedPane#setTabLayoutPolicy (JTabbedPane.WRAP_TAB_LAYOUT)
or JTabbedPane#setTabPlacement (JTabbedPane.BOTTOM)
is set:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
public class TabsOverlapTest {
private JComponent makeUI() {
Color selectedTabColor = UIManager.getColor("TabbedPane.selected");
Color tabBackgroundColor = Color.LIGHT_GRAY;
Color tabBorderColor = Color.GRAY;
UIManager.put("TabbedPane.highlight", tabBorderColor);
JTabbedPane tabs = new JTabbedPane();
tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabs.setUI(new BasicTabbedPaneUI() {
@Override protected void paintTabBorder(
Graphics g, int tabPlacement, int tabIndex,
int x, int y, int w, int h, boolean isSelected) {
}
@Override protected void paintFocusIndicator(
Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
Rectangle iconRect, Rectangle textRect, boolean isSelected) {
}
@Override protected void paintContentBorderTopEdge(
Graphics g, int tabPlacement, int selectedIndex,
int x, int y, int w, int h) {
super.paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h);
Rectangle selRect = getTabBounds(selectedIndex, calcRect);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(selectedTabColor);
g2.drawLine(selRect.x - 2, y, selRect.x + selRect.width + 2, y);
g2.dispose();
}
@Override protected void paintTabBackground(
Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h,
boolean isSelected) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int a = isSelected ? 0 : 1;
GeneralPath shape = new GeneralPath();
shape.moveTo(x - 3, y + h);
shape.lineTo(x + 3, y + a);
shape.lineTo(x + w - 3, y + a);
shape.lineTo(x + w + 3, y + h);
shape.closePath();
g2.setColor(isSelected ? selectedTabColor : tabBackgroundColor);
g2.fill(shape);
GeneralPath border = new GeneralPath();
if (isSelected || tabIndex == 0) {
border.moveTo(x - 3, y + h - 1);
} else {
border.moveTo(x + 3, y + h - 1);
border.lineTo(x, (y + h - 1) / 2);
}
border.lineTo(x + 3, y + a);
border.lineTo(x + w - 3, y + a);
border.lineTo(x + w + 3, y + h - 1);
g2.setColor(tabBorderColor);
g2.draw(border);
g2.dispose();
}
});
tabs.addTab("JTextArea", new JScrollPane(new JTextArea()));
tabs.addTab("JTree", new JScrollPane(new JTree()));
tabs.addTab("JButton", new JButton("button"));
tabs.addTab("JSplitPane", new JSplitPane());
return tabs;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TabsOverlapTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
paintTabArea
andpaintTab
methods inBasicTabbedPaneUI
grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…