import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
public class MyImage extends JFrame {
int width = 600;
int heigth = 400;
mapPanel map = new mapPanel("images/bird.gif");
Rectangle drag = new Rectangle(-1, 0, 0, 0);
Rectangle rc = new Rectangle(0, 0, 0, 0);
JPanel pan = new JPanel();
JButton cut = new JButton("Ausschneiden");
JButton copy = new JButton("Kopieren");
JButton paste = new JButton("Einfügen");
public MyImage() {
super("Simples Ausschneiden, Kopieren und Einfügen in Bildern");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
dispose();
System.exit(0);
}
});
setBounds(10, 10, width, heigth);
getContentPane().setLayout(new BorderLayout());
addButtons();
getContentPane().add(BorderLayout.SOUTH, pan);
getContentPane().add(BorderLayout.CENTER, map);
setVisible(true);
map.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent m) {
drag.x = -1;
}
public void mouseReleased(MouseEvent m) {
}
});
map.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent m) {
if (drag.x == -1) {
drag.setBounds(m.getX(), m.getY(), 0, 0);
}
drag.width = m.getX() - drag.x;
drag.height = m.getY() - drag.y;
repaint();
}
});
}
public void addButtons() {
pan.add(cut);
pan.add(copy);
pan.add(paste);
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent be) {
if (drag.x != -1)
map.cut(drag);
}
});
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent be) {
if (drag.x != -1)
map.copy(drag);
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent be) {
if (drag.x != -1)
map.paste(drag);
}
});
}
public class mapPanel extends JPanel {
Image map;
BufferedImage b1 = new BufferedImage(width, heigth,
BufferedImage.TYPE_3BYTE_BGR);
Graphics g1 = b1.getGraphics();
BufferedImage b2 = new BufferedImage(width, heigth,
BufferedImage.TYPE_3BYTE_BGR);
Graphics g2 = b2.getGraphics();
public mapPanel(String name) {
map = getToolkit().createImage(name);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(map, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
}
g1.setColor(Color.pink);
g1.fillRect(0, 0, width, heigth);
g1.drawImage(map, 0, 0, null);
}
public void cut(Rectangle r) {
copy(r);
g1.setColor(Color.white);
((Graphics2D) g1).fill(drag);
repaint();
}
public void copy(Rectangle r) {
g2.drawImage(b1, 0, 0, drag.width, drag.height, drag.x, drag.y,
drag.x + drag.width, drag.y + drag.height, null);
}
public void paste(Rectangle r) {
g1.drawImage(b2, drag.x, drag.y, drag.x + drag.width, drag.y
+ drag.height, 0, 0, drag.width, drag.height, null);
repaint();
}
public void paint(Graphics g) {
g.drawImage(b1, 0, 0, null);
Graphics2D g2 = (Graphics2D) g;
if (drag.x != -1) {
g2.setColor(Color.black);
g2.draw(drag);
}
}
}
public static void main(String[] args) {
new MyImage();
}
}