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 70 71 72 73 74 75 76 77 78 79 80 81 82
| import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.*;
class bu extends JFrame {
File file = new File("dix.gif");
Image im=null;
File file1 = new File("100.jpg");
Image im1=null;
public bu()
{
super("new");
setBounds(100,100,300,500);
try {
im=ImageIO.read(file);// throw IOException;
} catch (Exception e){};
try {
im1=ImageIO.read(file1);// throw IOException;
} catch (Exception e){};
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ChangePanel p=new ChangePanel();
JButton b=new JButton("change");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
p.setxy(100,100);
p.setChange(im); /10 DA
}
});
p.add(b);
JButton b1=new JButton("change");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
p.setxy(200,300);
p.setChange(im1); // 100 Da
}
});
p.add(b1);
setContentPane(p);
setVisible(true);
}
public static void main (String[] args) {
bu dd=new bu();
}
}
class ChangePanel extends JPanel {
int x,y;
public void setxy(int n,int p)
{
x=n;
y=p;
}
private Image change; // objet qui représente un ensemble de pièces.
private Image changes[];
public ChangePanel() {
change= null;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D s;
Color c=Color.white;
this.setBackground(c);
if(change != null) {
g.drawImage(change,x,y,this);
}
// g.drawImage(changes[1],10,20,this);
}
public void setChange(Image c) {
change=c;
repaint();
}
public void setChanges(Image c[],int i) {
change= c[i];
repaint();
}
} |