From 4a2d6f913bfe2e6b64fa154fdd0f8e74fb558543 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 14 Dec 2019 19:10:45 +0100 Subject: [PATCH] Custom scroll bar * Added PrimaryScrollBar class * Implemented PrimaryScrollBar in ChatWindow for the ScrollPanes vertical scroll bar --- src/main/java/envoy/client/ui/ChatWindow.java | 11 +++ .../envoy/client/ui/PrimaryScrollBar.java | 95 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/main/java/envoy/client/ui/PrimaryScrollBar.java diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 1c562b6..a6510ec 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -1,5 +1,6 @@ package envoy.client.ui; +import java.awt.Color; import java.awt.ComponentOrientation; import java.awt.Font; import java.awt.GridBagConstraints; @@ -252,6 +253,16 @@ public class ChatWindow extends JFrame { // scrollPane scrollPane.setForeground(theme.getBackgroundColor()); scrollPane.setBackground(theme.getCellColor()); + // scrollPane.getVerticalScrollBar() + // .setBackground( + // new Color(theme.getBackgroundColor().getRed() + 50, + // theme.getBackgroundColor().getGreen() + 50, + // theme.getBackgroundColor().getBlue() + 50)); + scrollPane.getVerticalScrollBar().setBackground(theme.getCellColor()); + scrollPane.getVerticalScrollBar() + .setUI(new PrimaryScrollBar(5, theme.getInteractableBackgroundColor(), + new Color(theme.getInteractableBackgroundColor().getRGB() - 50), + new Color(theme.getInteractableBackgroundColor().getRGB() + 170))); // messageEnterTextArea messageEnterTextArea.setCaretColor(theme.getTypingMessageColor()); messageEnterTextArea.setForeground(theme.getTypingMessageColor()); diff --git a/src/main/java/envoy/client/ui/PrimaryScrollBar.java b/src/main/java/envoy/client/ui/PrimaryScrollBar.java new file mode 100644 index 0000000..aaaa0a0 --- /dev/null +++ b/src/main/java/envoy/client/ui/PrimaryScrollBar.java @@ -0,0 +1,95 @@ +package envoy.client.ui; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.RenderingHints; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JScrollBar; +import javax.swing.plaf.basic.BasicScrollBarUI; + +import envoy.client.Settings; + +/** + * Project: envoy-client
+ * File: PrimaryScrollBar.javaEvent.java
+ * Created: 14.12.2019
+ * + * @author Maximilian Käfer + * @since Envoy v0.2-alpha + */ +public class PrimaryScrollBar extends BasicScrollBarUI{ + + private final Dimension d = new Dimension(); + private int arcSize; + private Color scrollBarColor; + private Color hoverColor; + private Color draggingColor; + + public PrimaryScrollBar(int arcSize, Color scrollBarColor, Color hoverColor, Color draggingColor) { + this.arcSize = arcSize; + this.scrollBarColor = scrollBarColor; + this.hoverColor = hoverColor; + this.draggingColor = draggingColor; + } + + @Override + protected JButton createDecreaseButton(int orientation) { + return new JButton() { + private static final long serialVersionUID = 1032443171070235890L; + + @Override + public Dimension getPreferredSize() { + return d; + } + }; + } + + @Override + protected JButton createIncreaseButton (int orientation) { + return new JButton() { + + private static final long serialVersionUID = 7575774542623215803L; + + @Override + public Dimension getPreferredSize() { + return d; + } + }; + } + @Override + protected void paintTrack(Graphics g, JComponent c, Rectangle r) { + } + + @Override + protected void paintThumb(Graphics g, JComponent c, Rectangle r) { + Graphics2D g2 = (Graphics2D) g.create(); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + Color color = null; + JScrollBar sb = (JScrollBar) c; + if (!sb.isEnabled() || r.width > r.height) { + return; + } else if (isDragging) { + color = draggingColor; + } else if (isThumbRollover()) { + color = hoverColor; + } else { + color = scrollBarColor; + } + g2.setPaint(color); + g2.fillRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.setPaint(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor()); + g2.drawRoundRect(r.x + 9, r.y, r.width - 10, r.height, arcSize, arcSize); + g2.dispose(); + } + + @Override + protected void setThumbBounds(int x, int y, int width, int height) { + super.setThumbBounds(x, y, width, height); + scrollbar.repaint(); + } +}