LightFilterのSpotLightを使ってみた。
2009/04/04
image
java
SpotLightというのがあって使い方がよくわからなくて、試行錯誤中です。
LightFilterってすごくよくできてるなぁと思うのですが、使う勘所が思い浮かばず。
光ものってweb2.0っぽいのかな。もしかして、web2.0って言い方はもう死語!?
スクリーンキャスト
使っているライブラリは、Jerry's Java Image Processing Pagesにあります。
あとユーティリティ、GUI周りは、quicklunch - Google Codeとかにパーツとして置いていこうかなと思います。
以下コード
package ifif;
import static quicklunch.e2.goodies.utils.ObjectUtils.eq;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JPanel;
import quicklunch.e2.goodies.swingy.MyFrame;
import quicklunch.e2.goodies.swingy.SlidersPanel;
import com.jhlabs.image.FillFilter;
import com.jhlabs.image.LightFilter;
public class TestMain {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
final MyPanel panel = new MyPanel();
myFrame.getContentPane().setLayout(new BorderLayout());
myFrame.getContentPane().add(new SlidersPanel() {
{
addSlider("centerX", "cenx");
addSlider("centerY", "ceny");
addSlider("cornAngle", "cornang");
addSlider("distance", "distance");
addSlidersPanelEventListener(new ISlidersPanelEventListener() {
@Override
public void slidersPanelEvent(SildersPanelEvent event) {
System.out.println(event);
String name = event.getName();
if (eq(name, "cenx")) {
panel.cenx = event.getFloat();
panel.gen();
panel.repaint();
return;
}
if (eq(name, "ceny")) {
panel.ceny = event.getFloat();
panel.gen();
panel.repaint();
return;
}
if (eq(name, "cornang")) {
panel.cornAng = event.getFloat();
panel.gen();
panel.repaint();
return;
}
if (eq(name, "distance")) {
panel.distance = 1000 * event.getFloat();
panel.gen();
panel.repaint();
return;
}
}
});
}
}, BorderLayout.NORTH);
myFrame.getContentPane().add(panel, BorderLayout.CENTER);
myFrame.setVisible(true);
}
static class MyPanel extends JPanel {
BufferedImage image;
public float cenx;
public float ceny;
public float cornAng;
public float distance;
{
gen();
}
public void gen() {
FillFilter fillFilter = new FillFilter();
fillFilter.setFillColor(new Color(100, 190, 28)
.getRGB());
fillFilter.setDimensions(400, 400);
BufferedImage image2 = fillFilter.filter(new BufferedImage(400,
400, BufferedImage.TYPE_INT_ARGB), null);
LightFilter lightFilter = new LightFilter() {
{
Vector<Light> vector = this.getLights();
Iterator<Light> iterator = vector.iterator();
removeLight(vector.firstElement());
SpotLight spotLight = new SpotLight();
spotLight.setCentreX(cenx);
spotLight.setCentreY(ceny);
spotLight.setConeAngle(cornAng);
spotLight.setDistance(distance);
addLight(spotLight);
}
};
try {
this.image = lightFilter.filter(image2, image2);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(this.image, 0, 0, null);
}
}
}
import static quicklunch.e2.goodies.utils.ObjectUtils.eq;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JPanel;
import quicklunch.e2.goodies.swingy.MyFrame;
import quicklunch.e2.goodies.swingy.SlidersPanel;
import com.jhlabs.image.FillFilter;
import com.jhlabs.image.LightFilter;
public class TestMain {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
final MyPanel panel = new MyPanel();
myFrame.getContentPane().setLayout(new BorderLayout());
myFrame.getContentPane().add(new SlidersPanel() {
{
addSlider("centerX", "cenx");
addSlider("centerY", "ceny");
addSlider("cornAngle", "cornang");
addSlider("distance", "distance");
addSlidersPanelEventListener(new ISlidersPanelEventListener() {
@Override
public void slidersPanelEvent(SildersPanelEvent event) {
System.out.println(event);
String name = event.getName();
if (eq(name, "cenx")) {
panel.cenx = event.getFloat();
panel.gen();
panel.repaint();
return;
}
if (eq(name, "ceny")) {
panel.ceny = event.getFloat();
panel.gen();
panel.repaint();
return;
}
if (eq(name, "cornang")) {
panel.cornAng = event.getFloat();
panel.gen();
panel.repaint();
return;
}
if (eq(name, "distance")) {
panel.distance = 1000 * event.getFloat();
panel.gen();
panel.repaint();
return;
}
}
});
}
}, BorderLayout.NORTH);
myFrame.getContentPane().add(panel, BorderLayout.CENTER);
myFrame.setVisible(true);
}
static class MyPanel extends JPanel {
BufferedImage image;
public float cenx;
public float ceny;
public float cornAng;
public float distance;
{
gen();
}
public void gen() {
FillFilter fillFilter = new FillFilter();
fillFilter.setFillColor(new Color(100, 190, 28)
.getRGB());
fillFilter.setDimensions(400, 400);
BufferedImage image2 = fillFilter.filter(new BufferedImage(400,
400, BufferedImage.TYPE_INT_ARGB), null);
LightFilter lightFilter = new LightFilter() {
{
Vector<Light> vector = this.getLights();
Iterator<Light> iterator = vector.iterator();
removeLight(vector.firstElement());
SpotLight spotLight = new SpotLight();
spotLight.setCentreX(cenx);
spotLight.setCentreY(ceny);
spotLight.setConeAngle(cornAng);
spotLight.setDistance(distance);
addLight(spotLight);
}
};
try {
this.image = lightFilter.filter(image2, image2);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(this.image, 0, 0, null);
}
}
}
: