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
| import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TestShape {
/**
* Test: Génère une série d'étoile...
*/
public static void main(String[] args) {
List<Shape> shapes=new ArrayList<Shape>();
/*
* Génèrer une liste d'étoiles
*/
for (int i=3;i<=8;i++) {
shapes.add(new Etoile(i,new Point(5*(i*i),200),i*4,i*2));
}
/*
* ...Et les afficher
*/
MyFrame frame=new MyFrame(shapes);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
frame.setSize(400, 400);
frame.setVisible(true);
}
/**
* Frame qui représente une liste de formes
*/
static
public class MyFrame extends Frame {
private List<Shape> shapes;
public MyFrame(List<Shape> shapes) {
this.shapes=shapes;
}
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
for (Shape shape:shapes) {
ga.draw(shape);
}
}
}
/**
* Nouveau Shape...Etoile
*/
static
public class Etoile extends Polygon {
public Etoile(int nombrePointes,Point centre,int rayonExterne,int rayonInterne) {
double offsetAngle=2*Math.PI/nombrePointes;
double angleExterne=0;
double angleInterne=offsetAngle/2;
for (int i=0;i<nombrePointes;i++) {
Point p=getPoint(centre, angleExterne,rayonExterne);
addPoint(p.x, p.y);
p=getPoint(centre, angleInterne,rayonInterne);
addPoint(p.x, p.y);
angleInterne+=offsetAngle;
angleExterne+=offsetAngle;
}
}
private Point getPoint(Point centre,double angle, double rayon) {
int x=(int) Math.round(centre.x +rayon*Math.cos(angle));
int y = (int) Math.round(centre.y + rayon*Math.sin(angle));
return new Point(x,y);
}
}
} |
Partager