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 83 84
|
package essai;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class fenetre extends JFrame implements MouseListener
{
private static final long serialVersionUID = 1L;
public monPanel monFond = new monPanel("/image/fond.jpg");
public monPanel monMobil = new monPanel("/image/mobile.jpg");
public JButton bouton = new JButton();
public fenetre()
{
this.setLayout(null);
this.setSize(1000,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
// --------- panelFond ----------
monFond.add(monMobil);
monMobil.setLocation(0, 0);
this.add(monFond);
monFond.setLocation(0, 0);
// -------- bouton -------------
bouton.setSize(40, 40);
bouton.setLocation(800, 20);
bouton.addMouseListener(this);
this.add(bouton);
}
public void tempPause(int tps)
{
try {Thread.sleep(tps);}
catch (InterruptedException e) {System.out.println(e.getMessage());}
}
public void mouseClicked(MouseEvent e)
{
for (int i=1; i<=10;i++)
{
monMobil.setLocation(monMobil.getX()+10, 0);
tempPause(200) ;
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
// --------------------------- panelFond --------------------
class monPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private BufferedImage myPicture;
public monPanel(String chemin)
{
this.setLayout(null);
try {myPicture= ImageIO.read(getClass().getResource(chemin)); }
catch (IOException e) {e.printStackTrace();}
this.setSize(myPicture.getWidth(),myPicture.getHeight());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(myPicture, 0, 0, null);
}
} |
Partager