//package applets;
/*
* MovePicture.java
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MovePicture extends JApplet {
private JPanel selectedBildPanel;
private boolean moveMode;
private int count;
public void init() {
System.out.println("BITTE WARTEN..");
add(new BilderContainer());
System.out.println("OK");
}
class BilderContainer extends JPanel implements MouseListener, MouseMotionListener{
private List<Bild> bilderListe;
private Cursor myPointer;
public BilderContainer(){
addMouseListener(this);
addMouseMotionListener(this);
setLayout(new GridLayout(4, 4));
bilderListe = new ArrayList<Bild>();
for (int i = 0; i < 8; i++) {
bilderListe.add(new Bild("http://www.nzz.ch/images/wetter/radargross.jpg"));
bilderListe.add(new Bild("http://images.thetimes.co.uk/TGD/picture/0,,223788,00.jpg"));
}
bilderAnzeigen();
}
public void mouseClicked(MouseEvent evt) { }
public void mousePressed(MouseEvent evt) {
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
buildMyPointer(((JPanel)evt.getSource()).getComponentAt(evt.getPoint()));
selectedBildPanel = (JPanel) ((JPanel)evt.getSource()).getComponentAt(evt.getPoint());
}
public void mouseReleased(MouseEvent evt) {
if( moveMode ) move(evt);
setCursor(Cursor.getDefaultCursor());
moveMode = false;
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseDragged(MouseEvent evt) {
setCursor(myPointer);
moveMode = true;
}
public void mouseMoved(MouseEvent evt) { }
private void move(MouseEvent evt){
Component[] components = getComponents();
JPanel panelFrom = selectedBildPanel;
JPanel panelTo = (JPanel)((JPanel)evt.getSource()).getComponentAt(evt.getPoint());
int positionFrom=0;
int positionTo=0;
for( int i=0; i<components.length; i++ ){
if( components[i] == panelFrom ) positionFrom=i;
if( components[i] == panelTo ) positionTo=i;
}
Bild bild = bilderListe.get(positionFrom);
bilderListe.remove(positionFrom);
bilderListe.add(positionTo, bild);
bilderAnzeigen();
}
private void bilderAnzeigen(){
removeAll();
for (Bild bild : bilderListe) add(bild);
validate();
}
private void buildMyPointer(Component component){
BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(component.getBackground());
g.fillRect(0, 0, image.getWidth(), image.getHeight());
component.print(g);
g.dispose();
Toolkit tk = Toolkit.getDefaultToolkit();
myPointer= tk.createCustomCursor(image, new Point(10,10), "myPointer");
}
}
class Bild extends JPanel{
public Bild(String pfad){
System.out.println(++count);
try{
URL url = new URL(pfad);
image = ImageIO.read(url);
}catch(Exception ex){
ex.printStackTrace();
}
}
public void paintComponent(final Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,null);
}
private BufferedImage image;
}
}