import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.time.Duration;
import javax.swing.border.*;
import static x30_pkg.x30_util.DynamicObject;
import java.awt.geom.*;
import java.awt.datatransfer.StringSelection;
import java.text.*;
import java.text.NumberFormat;
import java.util.TimeZone;
import javax.swing.Icon;
class main {
static class JDraggableBoxDemo implements Swingable {
public transient JComponent visualize_cache;
public JComponent visualize() { if (visualize_cache == null) visualize_cache = visualize_load(); return visualize_cache;}
public JComponent visualize_load() { return markVisualizer(this, visualize_impl()); }
JComponent visualize_impl() {
var parent = jpanelWithoutLayoutManager();
var box = jMinSize(400, 300, jcenteredlabel("Drag or resize me!"));
setBorder(box, BorderFactory.createLineBorder(Color.green, 10));
onMouseDown(box, event -> {
if (!mouseEventIsInBorder(event)) {
printVars("ComponentDragger start");
new ComponentDragger(box, event).debug(true).start();
}
});
new ComponentResizeDragger(box, box).debug(true);
setBounds(box, 50, 50, 400, 300);
addToContainer(parent, box);
return parent;
}
}
static A markVisualizer(Object visualizer, A a) {
return setMetaSrc(a, visualizer);
}
static JPanel jpanelWithoutLayoutManager() {
return panelWithoutLayoutManager();
}
static A jMinSize(int w, int h, A c) {
return jMinWidth(w, jMinHeight(h, c));
}
static A jMinSize(A c, int w, int h) {
return jMinSize(w, h, c);
}
static A jMinSize(A c, Dimension d) {
return jMinSize(d.width, d.height, c);
}
static JLabel jcenteredlabel(String text) {
return jcenteredLabel(text);
}
static JLabel jcenteredlabel() {
return jcenteredLabel();
}
static A setBorder(Border border, A c) {
if (c != null) { swing(() -> { c.setBorder(border); }); }
return c;
}
static A setBorder(A c, Border border) {
return setBorder(border, c);
}
static MouseAdapter onMouseDown(Component c, Runnable r) {
return onMouseDown(c, runnableToIVF1(r));
}
static MouseAdapter onMouseDown(Component c, IVF1 f) {
return c == null || f == null ? null : swing(() -> {
MouseAdapter ma = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
f.get(e);
}
};
c.addMouseListener(ma);
return ma;
});
}
static boolean mouseEventIsInBorder(MouseEvent e) { return swing(() -> {
var c = optCast(JComponent.class, e.getComponent());
if (c == null) return false;
var border = c.getBorder();
if (border == null) return false;
Insets insets = border.getBorderInsets(c);
int x = e.getX(), y = e.getY();
int w = c.getWidth(), h = c.getHeight();
boolean result = x < insets.left
|| x >= w-insets.right
|| y < insets.top
|| y >= h-insets.bottom;
return result;
}); }
// Use like this: printVars(+x, +y);
// Or: printVars("bla", +x);
// Or: printVars bla(, +x);
static void printVars(Object... params) {
printVars_str(params);
}
static A setBounds(final int x, final int y, final int w, final int h, final A a) {
if (a != null) { swing(() -> {
a.setBounds(x, y, w, h);
}); }
return a;
}
static A setBounds(A a, Rect r) {
if (a != null && r != null) { swing(() -> {
a.setBounds(toRectangle(r));
}); }
return a;
}
static A setBounds(A a, Rectangle r) {
if (a != null && r != null) { swing(() -> {
a.setBounds(r);
}); }
return a;
}
static A setBounds(Rect r, A a) {
return setBounds(a, r);
}
static A setBounds(A a, int x, int y, int w, int h) {
return setBounds(x, y, w, h, a);
}
static void addToContainer(Container a, Component... b) {
if (a == null) return;
{ swing(() -> {
for (Component c : unnullForIteration(b))
if (c != null)
a.add(c);
}); }
}
static A setMetaSrc(A a, Object src) {
setMetaAndVerify(a, "src", src);
return a;
}
static A setMetaSrc(A a, Object src) {
setMetaAndVerify(a, "src", src);
return a;
}
static JPanel panelWithoutLayoutManager() {
return new JPanel(null);
}
static A jMinWidth(final int w, final A c) {
if (c == null) return null;
return swing(new F0() { public A get() { try {
Dimension size = c.getMinimumSize();
c.setMinimumSize(new Dimension(/*max(w, size.width) ??? */w, size.height));
return jPreferWidth(w, c);
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "Dimension size = c.getMinimumSize();\r\n c.setMinimumSize(new Dimension(/*ma..."; }});
}
static A jMinHeight(A c, int h) {
return jMinHeight(h, c);
}
static A jMinHeight(int h, A c) {
Dimension size = c.getMinimumSize();
c.setMinimumSize(new Dimension(size.width, max(h, size.height)));
return jPreferHeight(h, c);
}
static JLabel jcenteredLabel(String text) {
return setHorizontalAlignment(JLabel.CENTER, jLabel(text));
}
static JLabel jcenteredLabel() {
return jcenteredLabel(" ");
}
static Object swing(Object f) {
return swingAndWait(f);
}
static void swing(Runnable f) {
swingAndWait(f);
}
static A swing(F0 f) {
return (A) swingAndWait(f);
}
static A swing(IF0 f) {
return (A) swingAndWait(f);
}
static IVF1 runnableToIVF1(Runnable r) {
return r == null ? null : a -> r.run();
}
static A optCast(Class c, Object o) {
return isInstance(c, o) ? (A) o : null;
}
// Use like this: printVars_str(+x, +y);
// Or: printVars("bla", +x);
// Or: printVars bla(+x);
static void printVars_str(Object... params) {
print(renderVars_str(params));
}
static Rectangle toRectangle(Rect r) {
return r == null ? null : r.getRectangle();
}
static String unnullForIteration(String s) {
return s == null ? "" : s;
}
static Collection unnullForIteration(Collection l) {
return l == null ? immutableEmptyList() : l;
}
static List unnullForIteration(List l) { return l == null ? immutableEmptyList() : l; }
static int[] unnullForIteration(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnullForIteration(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnullForIteration(double[] l) { return l == null ? emptyDoubleArray() : l; }
static short[] unnullForIteration(short[] l) { return l == null ? emptyShortArray() : l; }
static Map unnullForIteration(Map l) {
return l == null ? immutableEmptyMap() : l;
}
static Iterable unnullForIteration(Iterable i) {
return i == null ? immutableEmptyList() : i;
}
static A[] unnullForIteration(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnullForIteration(BitSet b) {
return b == null ? new BitSet() : b;
}
static Pt unnullForIteration(Pt p) {
return p == null ? new Pt() : p;
}
//ifclass Symbol
static Symbol unnullForIteration(Symbol s) {
return s == null ? emptySymbol() : s;
}
//endif
static Pair unnullForIteration(Pair p) {
return p != null ? p : new Pair(null, null);
}
static long unnullForIteration(Long l) { return l == null ? 0L : l; }
static void setMetaAndVerify(Object o, Object key, Object value) {
setMeta(o, key, value);
assertSame(() -> "setMeta failed (class: " + className(o) + ", key: " + key + ")",
value, metaGet(o, key));
}
static void setMetaAndVerify(IMeta o, Object key, Object value) {
setMeta(o, key, value);
assertSame(() -> "setMeta failed (class: " + className(o) + ", key: " + key + ")",
value, metaGet(o, key));
}
static A jPreferWidth(int w, A c) {
{ swing(() -> {
Dimension size = c.getPreferredSize();
c.setPreferredSize(new Dimension(/*max(w, size.width) ??? */w, size.height));
}); }
return c;
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static int max(int a, int b) { return Math.max(a, b); }
static int max(int a, int b, int c) { return max(max(a, b), c); }
static long max(int a, long b) { return Math.max((long) a, b); }
static long max(long a, long b) { return Math.max(a, b); }
static double max(int a, double b) { return Math.max((double) a, b); }
static float max(float a, float b) { return Math.max(a, b); }
static double max(double a, double b) { return Math.max(a, b); }
static > A max (Iterable l) {
A max = null;
var it = iterator(l);
if (it.hasNext()) {
max = it.next();
while (it.hasNext()) {
A a = it.next();
if (cmp(a, max) > 0)
max = a;
}
}
return max;
}
/*Nah.
static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
ret x;
}*/
static double max(double[] c) {
if (c.length == 0) return Double.MIN_VALUE;
double x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static float max(float[] c) {
if (c.length == 0) return Float.MAX_VALUE;
float x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
static short max(short[] c) {
short x = -0x8000;
for (short d : c) if (d > x) x = d;
return x;
}
static int max(int[] c) {
int x = Integer.MIN_VALUE;
for (int d : c) if (d > x) x = d;
return x;
}
static > A max(A a, A b) {
return cmp(a, b) >= 0 ? a : b;
}
static A jPreferHeight(int h, A c) {
Dimension size = c.getPreferredSize();
c.setPreferredSize(new Dimension(size.width, max(h, size.height)));
return c;
}
static A setHorizontalAlignment(final int pos, final A a) {
swingCall(a, "setHorizontalAlignment", pos);
return a;
}
static A setHorizontalAlignment(final int pos, final A a) {
swingCall(a, "setHorizontalAlignment", pos);
return a;
}
static A setHorizontalAlignment(final int pos, final A a) {
swingCall(a, "setHorizontalAlignment", pos);
return a;
}
static JLabel jLabel(String text) {
return jlabel(text);
}
static JLabel jLabel() {
return jlabel();
}
static void swingAndWait(Runnable r) { try {
if (isAWTThread())
r.run();
else
EventQueue.invokeAndWait(addThreadInfoToRunnable(r));
} catch (Exception __e) { throw rethrow(__e); } }
static Object swingAndWait(final Object f) {
if (isAWTThread())
return callF(f);
else {
final Var result = new Var();
swingAndWait(new Runnable() { public void run() { try {
result.set(callF(f));
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "result.set(callF(f));"; }});
return result.get();
}
}
static boolean isInstance(Class type, Object arg) {
return type.isInstance(arg);
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal