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

投稿

11月, 2013の投稿を表示しています

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