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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
//Classe Ping-Pong
import java.awt.*;
import javax.swing.*;
public class PingPong {
public static void main(String[] args) {
FenetreT2 fen = new FenetreT2();
}
}
-----------------------------------------------
//Classe Fenetre
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Fenetre extends JFrame {
Table tab = new Table();
public Fenetre() {
this.setTitle("Salut");
this.setSize(859, 568);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addKeyListener(new ClavierListener());
this.setContentPane(tab);
this.setBackground(new Color(200,200,200));
}
class ClavierListener implements KeyListener {
private boolean touchea = false;
private boolean toucheq = false;
private boolean touchep = false;
private boolean touchem = false;
public void keyTyped(KeyEvent e) {
if(touchea == true) {
tab.setPosYHRG(tab.getPosYHRG()-10); }
if(toucheq == true) {
tab.setPosYHRG(tab.getPosYHRG()+10); }
if(touchep == true) {
tab.setPosYHRD(tab.getPosYHRD()-10); }
if(touchem == true) {
tab.setPosYHRD(tab.getPosYHRD()+10); }
tab.repaint();
try { Thread.sleep(1); }
catch (InterruptedException ie) { ie.printStackTrace(); }
}
public void keyPressed(KeyEvent e) {
if (e.getKeyChar()=='a') {
touchea = true; }
else if (e.getKeyChar() == 'q') {
toucheq = true; }
else if (e.getKeyChar()=='p') {
touchep = true; }
else if (e.getKeyChar() == 'm') {
touchem = true; }
}
public void keyReleased(KeyEvent e) {
if (e.getKeyChar()=='a') {
touchea = false; }
else if (e.getKeyChar() == 'q') {
toucheq = false; }
else if (e.getKeyChar()=='p') {
touchep = false; }
else if (e.getKeyChar() == 'm') {
touchem = false; }
}
}
}
-----------------------------------------------
//Classe Table
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
public class Table extends JPanel {
private int posXLT = 10;
private int posYHT = 10;
private int posXLRG = 20;
private int posYHRG;
private int posXLRD = 20;
private int posYHRD;
public int getPosYHRG() {
return posYHRG;
}
public void setPosYHRG(int posYHRG) {
this.posYHRG = posYHRG;
}
public int getPosYHRD() {
return posYHRD;
}
public void setPosYHRD(int posYHRD) {
this.posYHRD = posYHRD;
}
public void paintComponent(Graphics g) {
g.setColor(new Color(240,240,240));
g.fillRect(posXLT, posYHT, (this.getWidth() - posXLT*2), (this.getHeight() - posYHT*2));
g.setColor(new Color(0,0,0));
g.fillRect(posXLRG, posYHRG+10, (this.getHeight()/8/4), (this.getHeight()/8));
g.fillRect(this.getWidth()-20-(this.getHeight()/8/4), posYHRD+10, (this.getHeight()/8/4), (this.getHeight()/8));
}
} |
Partager