[java]ドラッグして動かせるパネル
2009/02/04
2009/02/04
java
swingx
swingxのDropShadowBorderをためしてみました。
あわせて、ドラッグして動かせるパネルです。
パネルにイメージファイルをドロップできるようにしてみました。
コンポーネントを置くコンテナのレイアウトマネジャーをnullにすると簡単に実現できますね。
コンポーネントを置けるとコンポーネントにいろいろリスナーを設定したり効果をつけやすいので、重宝するかも。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.border.DropShadowBorder;
public class Test_drop_shadow_border {
static JXPanel createDragablleJPanel() {
return new JXPanel() {
@Override
protected void paintComponent(Graphics g) {
if (image != null) {
Insets insets = getBorder().getBorderInsets(this);
g.drawImage(image, insets.top, insets.left, getWidth()
- (insets.left + insets.right), getHeight()
- (insets.top + insets.bottom), null);
g.setColor(Color.WHITE);
g.drawRect(insets.top, insets.left, getWidth()
- (insets.left + insets.right), getHeight()
- (insets.top + insets.bottom));
}
super.paintComponent(g);
}
{
setOpaque(false);
setPreferredSize(new Dimension((int)(200*1.33), 200));
setSize(new Dimension((int)(200*1.33), 200));
DropShadowBorder border = new DropShadowBorder(Color.DARK_GRAY,
20, .2f, 12, false, false, true, true);
setLocation(100, 10);
setBorder(border);
setTransferHandler(new TransferHandler() {
@Override
public boolean importData(TransferSupport support) {
DataFlavor[] dataFlavors = support.getDataFlavors();
for (DataFlavor dataFlavor : dataFlavors) {
// System.out.println(dataFlavor);
// System.out.println(dataFlavor
// .getDefaultRepresentationClass());
if (dataFlavor.isFlavorJavaFileListType()) {
Object object;
try {
object = support.getTransferable()
.getTransferData(dataFlavor);
List list = (List) object;
for (Object o : list) {
if (o instanceof File) {
image = ImageIO.read((File) o);
repaint();
break;
}
}
// System.out.println(object.getClass());
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
@Override
public boolean canImport(TransferSupport support) {
return true;
}
});
addMouseMotionListener(new MouseAdapter() {
{
addMouseListener(this);
}
Point origin = null;
boolean isPressed = false;
@Override
public void mouseReleased(MouseEvent e) {
isPressed = false;
origin = null;
}
@Override
public void mousePressed(MouseEvent e) {
isPressed = true;
origin = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
// System.out.println(e);
if (!isPressed)
return;
Point nowPoint = e.getPoint();
int dx = nowPoint.x - origin.x;
int dy = nowPoint.y - origin.y;
setLocation(getX() + dx, getY() + dy);
}
});//
}
Image image;
};// return end...
}
public static void main(String[] args) {
JFrame frame = new JFrame() {
{
getContentPane().add(new JPanel() {
{
setLayout(null);
setBackground(Color.LIGHT_GRAY);
setPreferredSize(new Dimension(200, 200));
add(createDragablleJPanel());
add(createDragablleJPanel());
add(createDragablleJPanel());
add(createDragablleJPanel());
add(createDragablleJPanel());
add(createDragablleJPanel());
add(createDragablleJPanel());
add(createDragablleJPanel());
add(createDragablleJPanel());
}
});
}
};
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
}
: