Bonjour,
je travail sur un projet juste pour m'occuper et j'ai besoin d'aide :
import java.awt.Color;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.*;
import java.util.Arrays;
import java.awt.event.*;
public class PJ extends Applet implements KeyListener,Runnable{
boolean gagne;
int largeur=600, bordure=50,xClick=-1000,yClick=-1000,Score;
int nbBilles = 25,hits;
int xposj, yposj;
Color coloriage[]=new Color[nbBilles];
Color temp;
int xPos[]=new int[nbBilles],yPos[]=new int[nbBilles],rayon[]=new int[nbBilles];
boolean droite[]=new boolean[nbBilles],haut[]=new boolean[nbBilles],aff[]=new boolean[nbBilles];
String s="Jhon Project";
Thread bille;
Image sprite;
Graphics h;
public void init() {
setBackground(Color.white);
xposj= 10;
yposj= 10;
setSize(largeur+2*bordure,largeur+2*bordure);
sprite=createImage(largeur+2*bordure,largeur+2*bordure);
h=sprite.getGraphics();
bille=null;
for(int i=0;i<nbBilles;i++)
{
coloriage[i] = new Color((int)(Math.round(Math.random()*250)),(int)(Math.round(Math.random()*250)),(int)(Math.round(Math.random()*250)) );
rayon[i]=i+1;
xPos[i]=(int)(Math.round(Math.random()*(largeur-2*rayon[i])+bordure));
yPos[i]=(int)(Math.round(Math.random()*(largeur-2*rayon[i])+bordure));
droite[i]=i%2==0?true:false;
haut[i]=i%2==0?true:false;
aff[i]=true;
}
gagne=false;
addKeyListener ( this );
}
public void start(){
if(bille==null){
bille=new Thread(this);
bille.start();
}
}
public void stop() {
if(bille!=null) {
bille.stop();
bille = null;
}
}
public void run(){
while(true){
gagne=true;
for(int i=0; i<nbBilles; i++) if (aff[i])
gagne=false;
for(int i=0;i<nbBilles;i++){
if(xPos[i]+rayon[i]>=bordure+largeur)droite[i]=false;
if(yPos[i]+rayon[i]>=bordure+largeur)haut[i]=false;
if(xPos[i]-rayon[i]<=bordure)droite[i]=true;
if(yPos[i]-rayon[i]<=bordure)haut[i]=true;
if(droite[i])xPos[i]+=1*(1+i%1);else xPos[i]+=-1*(1+i%1);
if(haut[i])yPos[i]+=1*(1+i%1);else yPos[i]+=-1*(1+i%1);
}
try{
repaint();
Thread.sleep(50);
}
catch(InterruptedException e){
stop();
}
}
}
public void keyPressed ( KeyEvent e ) {}
public void keyReleased( KeyEvent e ) {}
public void keyTyped( KeyEvent e ) {
char c=e.getKeyChar();
if ( c != KeyEvent.CHAR_UNDEFINED){
/*s = s + c;*/
if (c=='z') yposj -=5;
if (c=='q') xposj -=5;
if (c=='s') yposj +=5;
if (c=='d') xposj +=5;
repaint();
e.consume();
}
}
/*public boolean mouseDown(Event evt, int x, int y){
hits =0;
if(bille!=null){
for (int i=0;i<nbBilles;i++){
if(Math.sqrt(Math.pow(xPos[i]-x,2)+Math.pow(yPos[i]-y,2))<=rayon[i]){
gagne=false;
if (aff[i]==true){
hits++;
aff[i]=false;
}
}
if(hits != 0)
Score += hits;
else
Score += -1;
}
xClick=x;
yClick=y;
return true
}*/
public void update (Graphics g){
paint(g);
}
public void paint (Graphics g){
g.drawImage(sprite,0,0,Color.white,this);
g.drawString(" Jhon",xposj,yposj);
g.drawString("I",xposj,yposj+11);
g.drawString("o",xposj-2,yposj+6);
g.drawString("]",xposj-1,yposj+16);
g.drawString("[",xposj+1,yposj+16);
g.drawString("_", xposj-2,yposj+4);
g.drawString(s,300,300);
h.clearRect(0,0,largeur+2*bordure,largeur+2*bordure);
h.setColor(Color.black);
h.drawLine(bordure, bordure, bordure, bordure+largeur);
h.drawLine(bordure, bordure, bordure+largeur, bordure);
h.drawLine(bordure+largeur, bordure, bordure+largeur,bordure+largeur);
h.drawLine(bordure,bordure+largeur, bordure+largeur,bordure+largeur);
h.drawString("Score : "+Score, 100,100);
for (int i=0;i<nbBilles;i++){
h.setColor(coloriage[i]);
if(aff[i])h.fillOval(xPos[i]-rayon[i],yPos[i]-rayon[i],2*rayon[i], 2*rayon[i]);
}
h.setColor(Color.black);
if (gagne){
h.setFont(new Font("default",Font.BOLD,40));
h.drawString("Gagné!",largeur/2,bordure+largeur/2);
}
repaint();
}
public void destroy(){
h.dispose();
sprite.flush();
}
}
Le code si dessus permet d'afficher plusieurs Billes mais le petit personnage clignote et si vous avez une solution je suis preneur !
Merci Kissan.