Skip to content

Commit

Permalink
add: TranslucentThumbDivider
Browse files Browse the repository at this point in the history
  • Loading branch information
aterai committed Nov 12, 2018
1 parent 4b054e8 commit 32bf35b
Show file tree
Hide file tree
Showing 11 changed files with 862 additions and 0 deletions.
545 changes: 545 additions & 0 deletions TranslucentThumbDivider/build.xml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions TranslucentThumbDivider/config.gradle
@@ -0,0 +1,6 @@
vendor = 'Java Swing Tips - https://ateraimemo.com/'
version = '1.0.0'
year = '2003-2017'

main.class = 'example.MainPanel'
runtime.classpath = '.'
8 changes: 8 additions & 0 deletions TranslucentThumbDivider/config/jnlp.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<property name="jnlp.title" value="JST TranslucentThumbDivider" />
<property name="jnlp.Name" value="TranslucentThumbDivider" />
<property name="jnlp.name" value="translucentthumbdivider" />
<property name="jnlp.codebase" value="https://ateraimemo.com/swing/translucentthumbdivider/" />
<property name="jnlp.homepage" value="https://ateraimemo.com/Swing/TranslucentThumbDivider.html" />
</project>
52 changes: 52 additions & 0 deletions TranslucentThumbDivider/config/properties.xml
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<!-- project -->
<property name="Name" value="Example" />
<property name="name" value="example" />
<property name="vendor" value="Java Swing Tips - https://ateraimemo.com/" />
<property name="version" value="1.0.0" />
<property name="year" value="2003-2018" />

<!-- class -->
<property name="main.class" value="example.MainPanel" />
<property name="runtime.classpath" value="." />

<!-- compile -->
<property name="compile.debug" value="on" />
<property name="compile.optimize" value="on" />
<property name="compile.nowarn" value="off" />
<property name="compile.deprecation" value="on" />
<property name="compile.verbose" value="off" />
<property name="compile.source" value="1.8" />
<property name="compile.encoding" value="UTF-8" />

<!-- javadoc -->
<property name="javadoc.encoding" value="UTF-8" />
<property name="javadoc.docencoding" value="UTF-8" />
<property name="javadoc.pkgs" value="*" />
<property name="javadoc.locale" value="ja" />
<property name="javadoc.link" value="https://docs.oracle.com/javase/jp/8/docs/api/" />

<!-- signjar -->
<property name="signjar.keystore" value="myKeyStore" />
<property name="signjar.alias" value="myAlias" />

<!-- directory -->
<property name="lib.dir" value="lib" />
<property name="src.dir" value="src/java" />
<property name="res.dir" value="src/conf" />
<property name="web.dir" value="src/web" />
<property name="conf.dir" value="src/conf" />
<property name="bkup.dir" value="bkuptmp" />

<!-- target -->
<property name="build.dir" value="target" />
<property name="build.src" value="${build.dir}/src" />
<property name="build.dest" value="${build.dir}/classes" />
<property name="build.docs" value="${build.dir}/apidocs" />
<property name="build.reports" value="${build.dir}/reports" />
<property name="build.correctreports" value="${build.dir}/correctreports" />
<property name="build.res" value="${build.dest}/resources" />
<property name="build.conf" value="${build.dest}/conf" />

</project>
21 changes: 21 additions & 0 deletions TranslucentThumbDivider/run.bat
@@ -0,0 +1,21 @@
@echo off
setlocal
echo Swing Example
echo -------------------

if "%JAVA_HOME%" == "" goto :error
set MAIN_CLASS=example.MainPanel
set CLASSPATH=target\classes;lib\*

echo Running with classpath "%CLASSPATH%"
echo Starting...
"%JAVA_HOME%\bin\java.exe" -classpath "%CLASSPATH%" %MAIN_CLASS% %*

goto :end

:error
echo ERROR: JAVA_HOME not found in your environment.
echo Please, set the JAVA_HOME variable in your environment to match the
echo location of the Java Virtual Machine you want to use.

:end
30 changes: 30 additions & 0 deletions TranslucentThumbDivider/run.sh
@@ -0,0 +1,30 @@
#!/bin/sh

echo
echo "Swing Example"
echo "-------------------"
echo

if [ "$JAVA_HOME" = "" ] ; then
echo "ERROR: JAVA_HOME not found in your environment."
echo
echo "Please, set the JAVA_HOME variable in your environment to match the"
echo "location of the Java Virtual Machine you want to use."
exit 1
fi

if [ `uname | grep -n CYGWIN` ]; then
PS=";"
elif [ `uname | grep -n Windows` ]; then
PS=";"
else
PS=":"
fi

LOCALCLASSPATH=target/classes${PS}lib/*

echo Running with classpath $LOCALCLASSPATH
echo Starting...
echo

"$JAVA_HOME/bin/java" -classpath "$LOCALCLASSPATH" example.MainPanel $*
168 changes: 168 additions & 0 deletions TranslucentThumbDivider/src/java/example/MainPanel.java
@@ -0,0 +1,168 @@
package example;
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.util.Objects;
import javax.swing.*;
import javax.swing.plaf.LayerUI;

public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());

JSplitPane split = new JSplitPane();
split.setContinuousLayout(true);
split.setResizeWeight(.5);
split.setDividerSize(0);

ImageIcon icon = new ImageIcon(getClass().getResource("test.png"));

BufferedImage source = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = source.createGraphics();
g.drawImage(icon.getImage(), 0, 0, null);
g.dispose();
ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage destination = colorConvert.filter(source, null);

Component beforeCanvas = new JComponent() {
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(icon.getImage(), 0, 0, icon.getIconWidth(), icon.getIconHeight(), this);
}
};
split.setLeftComponent(beforeCanvas);

Component afterCanvas = new JComponent() {
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
int iw = icon.getIconWidth();
int ih = icon.getIconHeight();
Point pt = getLocation();
Insets ins = split.getBorder().getBorderInsets(split);
g2.translate(-pt.x + ins.left, 0);
g2.drawImage(destination, 0, 0, iw, ih, this);
g2.dispose();
}
};
split.setRightComponent(afterCanvas);

add(new JLayer<>(split, new DividerLocationDragLayerUI()));
setOpaque(false);
setPreferredSize(new Dimension(320, 240));
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGui();
}
});
}
public static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

class DividerLocationDragLayerUI extends LayerUI<JSplitPane> {
private static final int R = 25;
private final Point startPt = new Point();
private final Cursor dc = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
private final Cursor wc = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
private final Ellipse2D thumb = new Ellipse2D.Double();
private int dividerLocation;
private boolean isDragging;
private boolean isEnter;

@Override public void installUI(JComponent c) {
super.installUI(c);
if (c instanceof JLayer) {
((JLayer<?>) c).setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
}
@Override public void uninstallUI(JComponent c) {
if (c instanceof JLayer) {
((JLayer<?>) c).setLayerEventMask(0);
}
super.uninstallUI(c);
}
@Override public void paint(Graphics g, JComponent c) {
super.paint(g, c);
if ((isEnter || isDragging) && c instanceof JLayer) {
updateThumbLocation(((JLayer<?>) c).getView(), thumb);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new Color(255, 100, 100, 100));
g2.fill(thumb);
g2.dispose();
}
}
@Override protected void processMouseEvent(MouseEvent e, JLayer<? extends JSplitPane> l) {
JSplitPane splitPane = l.getView();
switch (e.getID()) {
case MouseEvent.MOUSE_ENTERED:
isEnter = true;
break;
case MouseEvent.MOUSE_EXITED:
isEnter = false;
break;
case MouseEvent.MOUSE_RELEASED:
isDragging = false;
break;
case MouseEvent.MOUSE_PRESSED:
Component c = e.getComponent();
if (isDraggableComponent(splitPane, c)) {
Point pt = SwingUtilities.convertPoint(c, e.getPoint(), splitPane);
isDragging = thumb.contains(pt);
startPt.setLocation(SwingUtilities.convertPoint(c, e.getPoint(), splitPane));
dividerLocation = splitPane.getDividerLocation();
}
break;
default:
break;
}
splitPane.repaint();
}
@Override protected void processMouseMotionEvent(MouseEvent e, JLayer<? extends JSplitPane> l) {
JSplitPane splitPane = l.getView();
Component c = e.getComponent();
Point pt = SwingUtilities.convertPoint(c, e.getPoint(), splitPane);
if (e.getID() == MouseEvent.MOUSE_MOVED) {
splitPane.setCursor(thumb.contains(e.getPoint()) ? wc : dc);
} else if (isDragging && isDraggableComponent(splitPane, c) && e.getID() == MouseEvent.MOUSE_DRAGGED) {
int delta = splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT ? pt.x - startPt.x : pt.y - startPt.y;
splitPane.setDividerLocation(Math.max(0, dividerLocation + delta));
}
}
private static boolean isDraggableComponent(JSplitPane splitPane, Component c) {
return Objects.equals(splitPane, c) || Objects.equals(splitPane, SwingUtilities.getUnwrappedParent(c));
}
private static void updateThumbLocation(Component c, Ellipse2D thumb) {
if (c instanceof JSplitPane) {
JSplitPane splitPane = (JSplitPane) c;
int pos = splitPane.getDividerLocation();
if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
thumb.setFrame(pos - R, splitPane.getHeight() / 2 - R, R + R, R + R);
} else {
thumb.setFrame(splitPane.getWidth() / 2 - R, pos - R, R + R, R + R);
}
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions TranslucentThumbDivider/src/web/example.jnlp
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="@codebase@" href="example.jnlp">
<information>
<title>@title@</title>
<vendor>@vendor@</vendor>
<homepage href="@homepage@" />
<description>Swing Example</description>
<description kind="short">example</description>
</information>
<update check="timeout" policy="always" />
<resources>
<java version="@compile.source@+" />
<jar href="example.jar" main="true" />
</resources>
<application-desc main-class="@main.class@" />
</jnlp>
16 changes: 16 additions & 0 deletions TranslucentThumbDivider/src/web/index.html
@@ -0,0 +1,16 @@
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>@title@</title>
<link rel="canonical" href="@homepage@" />
<script type="text/javascript">
location.replace('@homepage@');
</script>
<meta http-equiv="Refresh" content="0; URL=@homepage@" />
</head>
<body>
<noscript>
<p><a href="@homepage@">@title@</a></p>
</noscript>
</body>
</html>
Binary file added TranslucentThumbDivider/src/web/screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 32bf35b

Please sign in to comment.