1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
public class CopyPanel extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
Panel1 source = new Panel1();
JPanel destination = new JPanel();
JButton bouton = new JButton("copier");
BufferedImage img;
public CopyPanel() {
setSize(650,500);
JPanel panel = new JPanel();
destination.setPreferredSize(new Dimension(400,400));
destination.setBackground(Color.blue);
bouton.addActionListener(this);
add(bouton,BorderLayout.NORTH);
source.add(new JTextArea("coucou",5,3));
panel.add(source);
panel.add(destination);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new CopyPanel();
}
public void copier() {
BufferedImage image = new BufferedImage( source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
source.paintAll( g);
g.dispose();
Graphics g2 = destination.getGraphics();
g2.drawImage( image, 50, 50, image.getWidth(), image.getHeight(), null);
g2.dispose();
}
public void actionPerformed(ActionEvent a) {
copier();
}
public class Panel1 extends JPanel {
BufferedImage img;
public Panel1() {
setPreferredSize(new Dimension(200,200));
}
public void paintComponent(Graphics g) {
try {
img = ImageIO.read(new File("test.png"));
}
catch(IOException e) {}
g.drawImage(img,0,0,img.getWidth(),img.getHeight(),this);
}
}
} |
Partager