Skip to content

Instantly share code, notes, and snippets.

@hageldave
Created October 2, 2016 15:57
Show Gist options
  • Save hageldave/8137f325d7652682b885df008ee91c5b to your computer and use it in GitHub Desktop.
Save hageldave/8137f325d7652682b885df008ee91c5b to your computer and use it in GitHub Desktop.
little gui for testing the different blending functions of imagingkit
public static void main(String[] args) throws IOException {
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BorderLayout());
ImagePanel imgpanel = new ImagePanel();
imgpanel.setPreferredSize(new Dimension(200, 200));
JComboBox<Blending> blendselector = new JComboBox<>(Blending.values());
final JLabel renderlabel = new JLabel();
blendselector.setRenderer((list,blend,idx,selected,focused)->{
renderlabel.setText(blend.name());
renderlabel.setBackground(selected?Color.gray:Color.lightGray);
return renderlabel;
});
JSlider opacityslider = new JSlider(JSlider.HORIZONTAL, 0, 255, 255);
JButton swapButton = new JButton("swap");
mainpanel.add(imgpanel, BorderLayout.CENTER);
mainpanel.add(blendselector, BorderLayout.SOUTH);
mainpanel.add(opacityslider, BorderLayout.NORTH);
mainpanel.add(swapButton, BorderLayout.EAST);
Img img1 = loadImg("http://sipi.usc.edu/database/preview/misc/4.2.05.png");
Img img2 = loadImg("http://sipi.usc.edu/database/preview/misc/4.2.06.png");
Img renderImg = img1.copy();
Img[] imgArray = {img1,img2};
imgpanel.setImg(renderImg);
int[] topIdx = {1};
Runnable performBlending = () -> {
int t = topIdx[0]%2;
int b = (t+1)%2;
Blending blend = (Blending) blendselector.getSelectedItem();
float opacity = opacityslider.getValue()/(255.0f);
imgArray[b].copyArea(0, 0, imgArray[b].getWidth(), imgArray[b].getHeight(), renderImg, 0, 0);
renderImg.forEach(blend.getAlphaBlendingWith(imgArray[t],opacity));
imgpanel.repaint();
};
blendselector.addActionListener((a)->{
performBlending.run();
});
opacityslider.addChangeListener((e)->{
performBlending.run();
});
swapButton.addActionListener((a)->{
topIdx[0]++; performBlending.run();
});
JFrame frame = new JFrame("blending demo");
frame.setContentPane(mainpanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
performBlending.run();
}
static Img loadImg(String url) throws IOException{
URL u = new URL(url);
return Img.createRemoteImg(ImageLoader.loadImage(u.openStream(), BufferedImage.TYPE_INT_ARGB));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment