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 java.lang.invoke.VarHandle;
import java.lang.invoke.MethodHandles;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.text.SimpleDateFormat;
import java.nio.charset.Charset;
import static x30_pkg.x30_util.DynamicObject;
import java.awt.geom.AffineTransform;
import java.awt.geom.*;
import java.text.*;
import java.text.NumberFormat;
import java.util.TimeZone;
class main {
static class JThumbnailButton extends JButton {
ThumbnailCache thumbnailCache;
File imageFile;
JThumbnailButton(ThumbnailCache thumbnailCache, File imageFile) {
this.imageFile = imageFile;
this.thumbnailCache = thumbnailCache;
setButtonImage(this, whiteImage(thumbnailCache.thumbnailSize()));
thumbnailCache.get(imageFile, img -> {
setButtonImage(this, centerImageOnBackground(Color.gray, thumbnailCache.thumbnailSize(), img));
});
}
}
static JButton setButtonImage(Icon img, JButton btn) {
btn.setIcon(img);
return btn;
}
static JButton setButtonImage(Image img, JButton btn) {
btn.setIcon(imageIcon(img));
return btn;
}
static A setButtonImage(Image img, A btn) {
btn.setIcon(imageIcon(img));
return btn;
}
static A setButtonImage(A btn, Image img) {
return setButtonImage(img, btn);
}
static A setButtonImage(A btn, String imageID) {
btn.setIcon(imageIcon(imageID));
return btn;
}
static JButton setButtonImage(JButton btn, Image img) {
return setButtonImage(img, btn);
}
static JButton setButtonImage(JButton btn, Icon img) {
return setButtonImage(img, btn);
}
static BufferedImage whiteImage(int w, int h) {
return newBufferedImage(w, h, Color.white);
}
static BufferedImage whiteImage(int size) {
return whiteImage(size, size);
}
static BufferedImage whiteImage(WidthAndHeight size) {
return whiteImage(size.getWidth(), size.getHeight());
}
static BufferedImage centerImageOnBackground(Color bg, WidthAndHeight size, BufferedImage img) {
if (img == null) return null;
BufferedImage canvas = newBufferedImage(size, bg);
drawImageOnImage(img, canvas,
(size.w()-img.getWidth())/2,
(size.h()-img.getHeight())/2);
return canvas;
}
static int imageIcon_cacheSize = 10;
static boolean imageIcon_verbose = false;
static Map imageIcon_cache;
static Lock imageIcon_lock = lock();
static ThreadLocal imageIcon_fixGIF = new ThreadLocal();
// not going through BufferedImage preserves animations
static ImageIcon imageIcon(String imageID) { try {
if (imageID == null) return null;
Lock __0 = imageIcon_lock; lock(__0); try {
if (imageIcon_cache == null)
imageIcon_cache = new MRUCache(imageIcon_cacheSize);
imageID = fsI(imageID);
ImageIcon ii = imageIcon_cache.get(imageID);
if (ii == null) {
if (imageIcon_verbose) print("Loading image icon: " + imageID);
File f = loadBinarySnippet(imageID);
Boolean b = imageIcon_fixGIF.get();
if (!isFalse(b))
ii = new ImageIcon(loadBufferedImageFixingGIFs(f));
else
ii = new ImageIcon(f.toURI().toURL());
} else
imageIcon_cache.remove(imageID); // move to front of cache on access
imageIcon_cache.put(imageID, ii);
return ii;
} finally { unlock(__0); } } catch (Exception __e) { throw rethrow(__e); } }
// doesn't fix GIFs
static ImageIcon imageIcon(File f) { try {
return new ImageIcon(f.toURI().toURL());
} catch (Exception __e) { throw rethrow(__e); } }
static ImageIcon imageIcon(Image img) {
return img == null ? null : new ImageIcon(img);
}
// undefined color, seems to be all black in practice
// This is without alpha?
static BufferedImage newBufferedImage(int w, int h) {
return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}
static BufferedImage newBufferedImage(int w, int h, Color color) {
BufferedImage img = newBufferedImage(w, h);
Graphics2D g = img.createGraphics();
g.setColor(or(color, Color.white));
g.fillRect(0, 0, w, h);
return img;
}
static BufferedImage newBufferedImage(Pt p, Color color) {
return newBufferedImage(p.x, p.y, color);
}
static BufferedImage newBufferedImage(WidthAndHeight size, Color color) {
return newBufferedImage(size.w(), size.h(), color);
}
// This one is with alpha...
static BufferedImage newBufferedImage(int w, int h, int[] pixels) {
return intArrayToBufferedImage(pixels, w, h);
}
// changes & returns canvas
static BufferedImage drawImageOnImage(Image img, BufferedImage canvas, int x, int y) {
createGraphics(canvas).drawImage(img, x, y, null);
return canvas;
}
static BufferedImage drawImageOnImage(Image img, BufferedImage canvas) {
return drawImageOnImage(img, canvas, 0, 0);
}
static void lock(Lock lock) { try {
ping();
if (lock == null) return;
try {
vmBus_send("locking", lock, "thread" , currentThread());
lock.lockInterruptibly();
vmBus_send("locked", lock, "thread" , currentThread());
} catch (InterruptedException e) {
Object reason = vm_threadInterruptionReasonsMap().get(currentThread());
print("Locking interrupted! Reason: " + strOr(reason, "Unknown"));
printStackTrace(e);
rethrow(e);
}
// NO call to ping here! Make sure lock is always released.
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
static String fsI(String id) {
return formatSnippetID(id);
}
static String fsI(long id) {
return formatSnippetID(id);
}
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