D
deddy
Da ich am 29.01.2010 diese kleines Speil abgeben muss, brauche dringend Hilfe..
Die Oberfläche:
wie kann ich die Bilder vertauschen?.. wenn z.b: ich klicke ein bildteil von dem linken puzzle und ein Bildteil von dem rechten puzzle, sollten die bilder ihre Position tauschen.
gruß
Die Oberfläche:
Java:
package spiel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import javax.swing.*;
public class puzzle extends JFrame implements ActionListener{
private JPanel panel1, panel2;
private Image source;
//für panel1
private JButton button0,button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11;
private JButton button_0,button_1,button_2,button_3,button_4,button_5,button_6,button_7,button_8,button_9,button_10,button_11;
//für panel2
private Image image0,image1,image2,image3,image4,image5,image6,image7,image8,image9,image10,image11;
private Image image_0,image_1,image_2,image_3,image_4,image_5,image_6,image_7,image_8,image_9,image_10,image_11;
int width, height;
public puzzle(){
panel1 = new JPanel();
panel2 = new JPanel();
panel1.setLayout(new GridLayout(4,4,0,0));
panel2.setLayout(new GridLayout(4,4,0,0));
ImageIcon theMonsters = new ImageIcon(puzzle.class.getResource("front.jpg"));
source = theMonsters.getImage();
width = theMonsters.getIconWidth();
height = theMonsters.getIconHeight();
add(Box.createRigidArea(new Dimension(0,5)), BorderLayout.NORTH);
add(panel1, BorderLayout.WEST);
button0 = new JButton();
button0.addActionListener(this);
panel1.add(button0);
image0 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 0*height/4, (width/3)+1, height/4)));
button0.setIcon(new ImageIcon(image0));
button1 = new JButton();
button1.addActionListener(this);
panel1.add(button1);
image1 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 0*height/4, (width/3)+1, height/4)));
button1.setIcon(new ImageIcon(image1));
button2 = new JButton();
button2.addActionListener(this);
panel1.add(button2);
image2 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 0*height/4, (width/3)+1, height/4)));
button2.setIcon(new ImageIcon(image2));
button3 = new JButton();
button3.addActionListener(this);
panel1.add(button3);
image3 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 1*height/4, (width/3)+1, height/4)));
button3.setIcon(new ImageIcon(image3));
button4 = new JButton();
button4.addActionListener(this);
panel1.add(button4);
image4 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 1*height/4, (width/3)+1, height/4)));
button4.setIcon(new ImageIcon(image4));
button5 = new JButton();
button5.addActionListener(this);
panel1.add(button5);
image5 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 1*height/4, (width/3)+1, height/4)));
button5.setIcon(new ImageIcon(image5));
button6 = new JButton();
button6.addActionListener(this);
panel1.add(button6);
image6 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 2*height/4, (width/3)+1, height/4)));
button6.setIcon(new ImageIcon(image6));
button7 = new JButton();
button7.addActionListener(this);
panel1.add(button7);
image7 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 2*height/4, (width/3)+1, height/4)));
button7.setIcon(new ImageIcon(image7));
button8 = new JButton();
button8.addActionListener(this);
panel1.add(button8);
image8 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 2*height/4, (width/3)+1, height/4)));
button8.setIcon(new ImageIcon(image8));
button9 = new JButton();
button9.addActionListener(this);
panel1.add(button9);
image9 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 3*height/4, (width/3)+1, height/4)));
button9.setIcon(new ImageIcon(image9));
button10 = new JButton();
button10.addActionListener(this);
panel1.add(button10);
image10 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 3*height/4, (width/3)+1, height/4)));
button10.setIcon(new ImageIcon(image10));
button11 = new JButton();
button11.addActionListener(this);
panel1.add(button11);
image11 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 3*height/4, (width/3)+1, height/4)));
button11.setIcon(new ImageIcon(image11));
//ende für Panel 1, anfang panel 2
add(Box.createRigidArea(new Dimension(0,5)), BorderLayout.NORTH);
add(panel2, BorderLayout.EAST);
button_0 = new JButton();
button_0.addActionListener(this);
panel2.add(button_0);
image_0 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 0*height/4, (width/3)+1, height/4)));
button_0.setIcon(new ImageIcon(image_0));
button_1 = new JButton();
button_1.addActionListener(this);
panel2.add(button_1);
image_1 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 0*height/4, (width/3)+1, height/4)));
button_1.setIcon(new ImageIcon(image_1));
button_2 = new JButton();
button_2.addActionListener(this);
panel2.add(button_2);
image_2 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 0*height/4, (width/3)+1, height/4)));
button_2.setIcon(new ImageIcon(image_2));
button_3 = new JButton();
button_3.addActionListener(this);
panel2.add(button_3);
image_3 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 1*height/4, (width/3)+1, height/4)));
button_3.setIcon(new ImageIcon(image_3));
button_4 = new JButton();
button_4.addActionListener(this);
panel2.add(button_4);
image_4 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 1*height/4, (width/3)+1, height/4)));
button_4.setIcon(new ImageIcon(image_4));
button_5 = new JButton();
button_5.addActionListener(this);
panel2.add(button_5);
image_5 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 1*height/4, (width/3)+1, height/4)));
button_5.setIcon(new ImageIcon(image_5));
button_6 = new JButton();
button_6.addActionListener(this);
panel2.add(button_6);
image_6 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 2*height/4, (width/3)+1, height/4)));
button_6.setIcon(new ImageIcon(image_6));
button_7 = new JButton();
button_7.addActionListener(this);
panel2.add(button_7);
image_7 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 2*height/4, (width/3)+1, height/4)));
button_7.setIcon(new ImageIcon(image_7));
button_8 = new JButton();
button_8.addActionListener(this);
panel2.add(button_8);
image_8 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 2*height/4, (width/3)+1, height/4)));
button_8.setIcon(new ImageIcon(image_8));
button_9 = new JButton();
button_9.addActionListener(this);
panel2.add(button_9);
image_9 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(0*width/3, 3*height/4, (width/3)+1, height/4)));
button_9.setIcon(new ImageIcon(image_9));
button_10 = new JButton();
button_10.addActionListener(this);
panel2.add(button_10);
image_10 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(1*width/3, 3*height/4, (width/3)+1, height/4)));
button_10.setIcon(new ImageIcon(image_10));
button_11 = new JButton();
button_11.addActionListener(this);
panel2.add(button_11);
image_11 = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(2*width/3, 3*height/4, (width/3)+1, height/4)));
button_11.setIcon(new ImageIcon(image_11));
setSize(1100, 480);
setTitle("Puzzle");
setResizable(true);
setLocation(100,100);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new puzzle();
}
}
wie kann ich die Bilder vertauschen?.. wenn z.b: ich klicke ein bildteil von dem linken puzzle und ein Bildteil von dem rechten puzzle, sollten die bilder ihre Position tauschen.
gruß