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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
@SuppressWarnings("serial")
public class Panneau extends JPanel{
private int[] limits;
private Ball[] b;
private int nbOfBalls = 1+(int)(Math.random()*4);
private int[][] col;
public Panneau(){
initPanneau();
}
public void initPanneau(){
limits = new int[8];
b = new Ball[nbOfBalls];
for (int i=0;i<b.length;i++)
b[i] = new Ball();
col = new int[b.length][];
for (int i=0;i<b.length;i++)
col[i] = new int[b.length];
}
public void paintComponent(Graphics g){
limits[0]= 0;
limits[1]= getWidth();
limits[2]= 0;
limits[3]= getHeight();
limits[4]= (int)(getWidth()/2)-100;
limits[5]= (int)(getWidth()/2)+100;
limits[6]= (int)(getHeight()/2)-300;
limits[7]= (int)(getHeight()/2)+300;
for (int i=0;i<b.length;i++){
for (int j=0;j<b.length;j++){
if(col[i][j]>0)
col[i][j]--;
}
}
draw(g);
for (int i=0;i<b.length;i++)
b[i].paintComponent(g);
//System.out.println(nbOfBalls);
}
public void move(){
for (int i=0;i<b.length;i++)
b[i].moveBall(limits);
Collisions(b,b.length,(b.length-1));
}
private void Collisions(Ball[] balls, int i, int j){
if (i==1)
return;
boolean boolCol=false;
if (col[i-1][j-1]==0)
boolCol = testCollision(balls[i-1],balls[j-1]);
if (boolCol==true)
col[i-1][j-1]=10;
if (j-1>0)
Collisions(balls,i,j-1);
else
Collisions(balls,i-1,i-2);
}
private boolean testCollision(Ball one, Ball two){
int r1=one.getRadius();
int r2=two.getRadius();
int x1=one.getPosX()+(int)(java.lang.Math.sqrt(2)*r1);
int x2=two.getPosX()+(int)(java.lang.Math.sqrt(2)*r2);
int y1=one.getPosY();
int y2=two.getPosY();
int vx1=one.getVx();
int vy1=one.getVy();
int vx2=two.getVx();
int vy2=two.getVy();
if (java.lang.Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))<(r1+r2)){
/* if ((x2-x1)>(y2-y1)){
two.setVy(vy1);
}
if ((x2-x1)<=(y2-y1)){
two.setVx(vx1);
}
*/
one.setVx(-vx1);
one.setVy(-vy1);
two.setVx(-vx2);
two.setVy(-vy2);
return true;
}
else
return false;
}
private void draw(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
//PONG
//Fond
g.setColor(Color.black);
g.fillRect(limits[4], limits[6],200,600);
//P
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+40, 20, 100);
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+40, 100, 20);
g.setColor(Color.white);
g.fillRect(limits[4]+130, limits[6]+40, 20, 50);
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+70, 100, 20);
//O
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+180, 20, 100);
g.setColor(Color.white);
g.fillRect(limits[4]+130, limits[6]+180, 20, 100);
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+180, 100, 20);
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+260, 100, 20);
//N
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+320, 20, 100);
g.setColor(Color.white);
g.fillRect(limits[4]+130, limits[6]+330, 20, 90);
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+330, 100, 20);
//G
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+460, 20, 100);
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+460, 100, 20);
g.setColor(Color.white);
g.fillRect(limits[4]+50, limits[6]+540, 100, 20);
g.setColor(Color.white);
g.fillRect(limits[4]+130, limits[6]+500, 20, 40);
g.setColor(Color.white);
g.fillRect(limits[4]+110, limits[6]+500, 40, 20);
}
public int getNbOfBalls() {
return nbOfBalls;
}
public void setNbOfBalls(int nbOfBalls) {
this.nbOfBalls = nbOfBalls;
}
public Ball[] getB() {
return b;
}
public void setB(Ball[] b) {
this.b = b;
}
} |
Partager