スキップしてメイン コンテンツに移動

投稿

Java: Shuffling Elements in Array

I will show you java code for shuffling order of elements in array randomly. Only one thing you should notice is generating random is quite sensitive problem in a precise sense. I use java Random class which is provided by JDK. However if you need to use more proper randomness, you may implement class for generate random number sequence yourself. Anyway, the code is below. package com.dukesoftware.utils.math; import java.util.Random; public class Shuffler { private final Random random; public Shuffler(Random random) { this.random = random; } public void shuffle(int[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i int swap = a[i]; a[i] = a[j]; a[j] = swap; } } public void shuffle(double[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i double swap =

Javascript: Lazy Load Image

I realized that most of the modern browser download the image which is set on src attribute even if the img element is hidden. This sometimes goes to performance problem because even if there are a lot of hidden image which are only shown user interacts, all images are downloaded initial page load! So I wrote a simple javascript for reflesh the src attribute and download image only when user interacts somethig on window - like press button etc. I know you can use JQuery LazyLoad plugin , but I would like to take much more faster solution - no external javascript. Here is the demo. When you push the button, the image is loaded lazyly Load image Here is the key method. (maybe you can check the logic by viewing the src directly though.) Replace the src attribute with data-original attribute i the img tag. function loadImage(elem) { if(elem.attr("src") != elem.attr("data-original")) { elem.attr("src", elem.attr("data-original&qu

Drag & Drop Image File onto Java Swing Component

In this post I will show you how to achieve drag and drop image file onto Java Swing Component. In short, implment TransferHandler which can transfer image file. Code Here is the code I wrote. I defined common closure interface in order to give implementation flexibility. The exec method of imageAcceptor will be called with Image object when drag & drop is succeeded. package com.dukesoftware.utils.swing.drag; import java.awt.Image; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.imageio.ImageIO; import javax.swing.TransferHandler; import com.dukesoftware.utils.common.Closure; public class ImageFileTransferHandler extends TransferHandler { private final static Set<String> SUPPORTED_SUFIXES = new HashSet<>(Arr

Java Customize Serialization - Using Externalizable

I will show you how to customize Java serialization (Externalization), performance comparison default serialization vs my externalization implmentation in this post. In the most cases, you don't have to care about the serialization performance of default implementation. But the default implementation uses reflection so it might be a problem if you develop performance critical application. Externalizable Implement your own serialization is very simple. Just implements Externailizable interface into the class. Here is the example code - simple pojo class and TreeMap which implements Externalizable. package com.dukesoftware.demos.externalizable; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.ArrayList; import java.util.List; public class ExternalizableClass implements Externalizable{ private String field1; private Double field2; private List<String> field3; public S

Render FPS Text on Java Swing Component

In the previous post , I showed helper class for 2D animation on Java Swing component. If we can measure the frame rate of animation, it is very great, isn't it? In this post, I will show you that kind of class - rendering FPS text on swing component. Showing code is fast and snappy :p Here is the code - count frame per 1 sec. package com.dukesoftware.utils.awt.graphics; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.text.DecimalFormat; public class FPSText { protected static final String FPS = "FPS: "; private static final int FONT_SIZE = 24; private static final int FONT_STYLE = Font.BOLD; private static final String FONT_NAME = "SansSerif"; protected final static Color COLOR = new Color(0.55f,0.55f,0.55f); protected double frameCount; protected final DecimalFormat format = new DecimalFormat("####.00"); protected String fpsText; protected fin

Java 2D Animation on Swing

In this post, I will show you some tips (or tools) for 2D animation on Java Swing component. There are 2 ways to rendering animation on Swing compinent - Thread vs Timer. I think Swing rendering is based on single thread model, so we should use Timer. But I have used separate thread for executing animation and it works fine, so should be fine for not critical application. Animator class Ok then, I can show you a simple animator canvas class I created. The class takes Model2D for actual animate model and render it. One thing you should remember - AnimateCanvas calculate model of animation (coodinates or position or whatever) on animation rendering process, so if the calculation takes takes log time, the animation cannot meet the frame per second passed to constructor. package com.dukesoftware.utils.swing.others; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.aw