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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class BubblePlay extends JFrame {
public Image im = null;
public MediaTracker mt = null;
private Container cont = new Container();
private PanRoundButton pan = new PanRoundButton();
private PersoManager perso = new PersoManager("perso.png");
public BubblePlay() {
super("Déplacer le personnage");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800,600);
setLocationRelativeTo(null);
cont = getContentPane();
cont.setLayout(null);
cont.add(perso);
cont.add(pan);
pan.setBounds(0,0,800,600);
perso.setBounds(200,200,40,40);
addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == 'z') {
perso.setLocation(perso.getX(),perso.getY()-3); // a modif
}
else if(e.getKeyChar() == 'q') { // a modif
perso.setLocation(perso.getX()-3,perso.getY());
perso.persoRepaint("persoLeft1.png");
perso.persoRepaint("persoLeft2.png");
}
else if(e.getKeyChar() == 's') {
perso.setLocation(perso.getX(),perso.getY()+3); // a modif
}
else if(e.getKeyChar() == 'd') {
perso.setLocation(perso.getX()+3,perso.getY()); // a modif
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
setVisible(true);
}
public class PersoManager extends JLabel {
public PersoManager(String str) {
im = Toolkit.getDefaultToolkit().getImage(str);
mt = new MediaTracker(this);
mt.addImage(im,0);
try {
mt.waitForID(0);
}
catch(InterruptedException e) {
System.out.println("Erreur MediaTracker");
}
}
public void paint(Graphics gg) {
Graphics2D g = (Graphics2D) gg;
g.drawImage(im,0,0,this);
}
public void persoRepaint(String str) {
if(str.equals("persoLeft1.png")) {str = "persoLeft2.png";}
else if(str.equals("persoLeft2.png")) {str = "persoLeft1.png";}
im = Toolkit.getDefaultToolkit().getImage(str);
mt = new MediaTracker(this);
mt.addImage(im,0);
try {
mt.waitForID(0);
}
catch(InterruptedException e) {
System.out.println("Erreur MediaTracker");
}
}
}
public static void main(String[] args) {
new BubblePlay();
}
} |
Partager