chenilles.java

Telechargé par Claudia Seugnou
import java.awt.*;
//import java.swing.*;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JPanel;
class Anneau {
// public static void main(String[] args){
//attributs
protected int xInit;
protected int yInit;
protected int r;
public Anneau(int xInit, int yInit, int r) {
this.xInit = xInit;
this.yInit = yInit;
this.r = r;
}
public int getx() {
return this.xInit;
}
public int gety() {
return this.yInit;
}
public void placerA(int px, int py) {
this.xInit = px;
this.yInit = py;
}
public void dessiner(Graphics g) {
g.drawOval(this.getx(), this.gety(), this.r, this.r);
}
}
class Tete extends Anneau {
// attributs
private double cap;
// constructeur
public Tete(int xInit, int yInit, int r, double uncap) {
super(xInit, yInit, r);
this.cap = uncap;
}
public int getR() {
return this.r;
}
// affiche l'anneau en le matérialisant par un cercle noir
public void dessiner(Graphics g) {
g.fillOval(this.getx(), this.gety(), this.r, this.r);
}
// modifier la cap de la tete en lui ajoutant la deviation definie par le
// parametre deltac
public void devierCap(double deltaC) {
this.cap += deltaC;
}
// retuorne true si le centre de la tete est à une distance <= R de l'un des
// bords de le fenetre
// dde taille xMax, yMax
public boolean atteintBord(int xMax, int yMax) {
return (this.xInit >= xMax - this.r || this.yInit >= yMax - this.r || this.xInit <= 0 || this.yInit <= 0);
}
// modifie le centre de la tete en lui appliquant un deplacement de longueur R
// dans la directiondefinie par le cap
public void deplacerSelonCap() {
this.xInit = (int) (this.getx() + (this.r / 2 * Math.cos(this.cap)));
this.yInit = (int) (this.gety() + (this.r / 2 * Math.sin(this.cap)));
this.placerA(this.xInit, this.yInit);
}
}
class Chenille {
// attribut
private Tete t;
private Anneau[] listeA;
private Dessin d;
// constructeur
public Chenille(int nbA, Dessin unDessin) {
this.d = unDessin;
this.listeA = new Anneau[nbA];
int tx = d.getLargeur() / 2;
int ty = d.getHauteur() / 2;
this.t = new Tete(tx, ty, 50, 0);
int ax = t.getx() - t.getR() / 2;
int ay = t.gety();
for (int i = 0; i < nbA; i++) {
listeA[i] = new Anneau(ax, ay, 50);
ax = listeA[i].getx() - t.getR() / 2;
}
}
public void dessiner(Graphics g) {
t.dessiner(g);
for (Anneau a : this.listeA) {
a.dessiner(g);
}
}
// fait se deplacer la chenille
public void deplacer() {
int aCourant = listeA.length - 1;
for (int i = aCourant; i > 0; i--) {
int aPre = i - 1;
listeA[i].placerA(listeA[aPre].getx(), listeA[aPre].gety());
}
listeA[0].placerA(t.getx(), t.gety());
int dh = d.getLargeur();
int dl = d.getHauteur();
if (this.t.atteintBord(dh, dl))
this.t.devierCap(Math.toRadians(90));
else {
double capRdm = (Math.random() * 60) - 30;
1 / 11 100%

chenilles.java

Telechargé par Claudia Seugnou
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !