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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
| import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import static javax.swing.SwingConstants.*;
public class InterfaceCC extends JFrame implements MouseListener{
private interface GraphicEntity{
public void paint(Graphics2D g, Point p, int cell, int border);
}
private class Carre implements GraphicEntity{
Color c = Color.GREEN; int niveau;
public Carre(int niveau){this.niveau = niveau;}
public void paint(Graphics2D g, Point p, int cell, int border){
if(niveau==3) g.setColor(Color.WHITE);
else g.setColor(this.c);
float coeff = 8.0f;
GeneralPath tour = new GeneralPath();
tour.moveTo((p.x+1)*cell-border,p.y*cell+border);
tour.curveTo((p.x+1)*cell-border,p.y*cell+border,(p.x+1)*cell-border-cell/coeff,p.y*cell+cell/2.0f,(p.x+1)*cell-border,(p.y+1)*cell-border);
tour.lineTo(p.x*cell+border,(p.y+1)*cell-border);
tour.curveTo(p.x*cell+border,(p.y+1)*cell-border,p.x*cell+border+cell/coeff,p.y*cell+cell/2.0f,p.x*cell+border,p.y*cell+border);
tour.closePath();
if(this.niveau == 1){
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(0, cell/rayures);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE};
LinearGradientPaint pai = new LinearGradientPaint(start, end, dist, colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
if(this.niveau == 2){
Point2D center = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float radius = cell/cercles;
Point2D focus = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE,};
RadialGradientPaint pai = new RadialGradientPaint(center,radius,focus,dist,colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
g.fill(tour);
}
}
private class Ovale implements GraphicEntity{
Color c = Color.ORANGE; int niveau;
public Ovale(int niveau){this.niveau = niveau;}
public void paint(Graphics2D g, Point p, int cell, int border){
if(niveau==3) g.setColor(Color.WHITE);
else g.setColor(this.c);
float coeff = 6.0f;
if(this.niveau == 1){
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(0, cell/rayures);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE};
LinearGradientPaint pai = new LinearGradientPaint(start, end, dist, colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
if(this.niveau == 2){
Point2D center = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float radius = cell/cercles;
Point2D focus = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE,};
RadialGradientPaint pai = new RadialGradientPaint(center,radius,focus,dist,colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
g.fillOval((int) (p.x*cell+border+cell/(2*coeff)),p.y*cell+border,(int) (cell-2*border-cell/coeff),cell-2*border);
}
}
private class Larme implements GraphicEntity{
Color c = Color.YELLOW; int niveau;
public Larme(int niveau){this.niveau = niveau;}
public void paint(Graphics2D g, Point p, int cell, int border){
if(niveau==3) g.setColor(Color.WHITE);
else g.setColor(this.c);
float coeff = 4.0f/5;
GeneralPath tour = new GeneralPath();
tour.moveTo(p.x*cell+cell/2.0f,p.y*cell+border);
tour.lineTo((p.x+1)*cell-border,p.y*cell+border+(cell-2*border)*coeff);
tour.curveTo((p.x+1)*cell-border,p.y*cell+border+(cell-2*border)*coeff, p.x*cell+cell/2.0f,(p.y+1)*cell,p.x*cell+border,p.y*cell+border+(cell-2*border)*coeff);
tour.closePath();
if(this.niveau == 1){
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(0, cell/rayures);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE};
LinearGradientPaint pai = new LinearGradientPaint(start, end, dist, colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
if(this.niveau == 2){
Point2D center = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float radius = cell/cercles;
Point2D focus = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE,};
RadialGradientPaint pai = new RadialGradientPaint(center,radius,focus,dist,colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
g.fill(tour);
}
}
private class Rond implements GraphicEntity{
Color c = Color.BLUE; int niveau;
public Rond(int niveau){this.niveau = niveau;}
public void paint(Graphics2D g, Point p, int cell, int border){
if(niveau==3) g.setColor(Color.WHITE);
else g.setColor(this.c);
if(this.niveau == 1){
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(0, cell/rayures);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE};
LinearGradientPaint pai = new LinearGradientPaint(start, end, dist, colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
if(this.niveau == 2){
Point2D center = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float radius = cell/cercles;
Point2D focus = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE,};
RadialGradientPaint pai = new RadialGradientPaint(center,radius,focus,dist,colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
g.fillOval(p.x*cell+border,p.y*cell+border,cell-2*border,cell-2*border);
}
}
private class Hexagone implements GraphicEntity{
Color c = new Color(121, 28, 248); int niveau;
public Hexagone(int niveau){this.niveau = niveau;}
public void paint(Graphics2D g, Point p, int cell, int border){
if(niveau==3) g.setColor(Color.WHITE);
else g.setColor(this.c);
double d = (cell-2*border)/3.0d;
GeneralPath tour = new GeneralPath();
tour.moveTo(p.x*cell+border+d,p.y*cell+border);
tour.lineTo((p.x+1)*cell-border-d,p.y*cell+border);
tour.lineTo((p.x+1)*cell-border,p.y*cell+border+d);
tour.lineTo((p.x+1)*cell-border,(p.y+1)*cell-border-d);
tour.lineTo((p.x+1)*cell-border-d,(p.y+1)*cell-border);
tour.lineTo(p.x*cell+border+d,(p.y+1)*cell-border);
tour.lineTo(p.x*cell+border,(p.y+1)*cell-border-d);
tour.lineTo(p.x*cell+border,p.y*cell+border+d);
tour.closePath();
if(this.niveau == 1){
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(0, cell/rayures);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE};
LinearGradientPaint pai = new LinearGradientPaint(start, end, dist, colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
if(this.niveau == 2){
Point2D center = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float radius = cell/cercles;
Point2D focus = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE,};
RadialGradientPaint pai = new RadialGradientPaint(center,radius,focus,dist,colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
g.fill(tour);
}
}
private class Saucisse implements GraphicEntity{
Color c = Color.RED; int niveau;
public Saucisse(int niveau){this.niveau = niveau;}
public void paint(Graphics2D g, Point p, int cell, int border){
if(niveau==3) g.setColor(Color.WHITE);
else g.setColor(this.c);
double d = (cell-2*border)/3.0d;
GeneralPath tour = new GeneralPath();
tour.moveTo(p.x*cell+border+d,p.y*cell+border);
tour.curveTo(p.x*cell+border+d,p.y*cell+border,p.x*cell+cell/2.0f,p.y*cell+cell/2.0f,(p.x+1)*cell-border,(p.y+1)*cell-border-d);
tour.curveTo((p.x+1)*cell-border,(p.y+1)*cell-border-d,(p.x+1)*cell-border,(p.y+1)*cell-border,(p.x+1)*cell-border-d,(p.y+1)*cell-border);
tour.curveTo((p.x+1)*cell-border-d,(p.y+1)*cell-border,p.x*cell+border+(cell-2*border)/2.0f-d,p.y*cell+border+d+(cell-2*border)/2.0f,p.x*cell+border,p.y*cell+border+d);
tour.curveTo(p.x*cell+border,p.y*cell+border+d,p.x*cell+border,p.y*cell+border,p.x*cell+border+d,p.y*cell+border);
if(this.niveau == 1){
Point2D start = new Point2D.Float(0,0);
Point2D end = new Point2D.Float(0, cell/rayures);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE};
LinearGradientPaint pai = new LinearGradientPaint(start, end, dist, colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
if(this.niveau == 2){
Point2D center = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float radius = cell/cercles;
Point2D focus = new Point2D.Float(p.x*cell+cell/2.0f,p.y*cell+cell/2.0f);
float[] dist = {0.45f, 0.5f};
Color[] colors = {c, Color.WHITE,};
RadialGradientPaint pai = new RadialGradientPaint(center,radius,focus,dist,colors, MultipleGradientPaint.CycleMethod.REPEAT);
g.setPaint(pai);
}
g.fill(tour);
}
}
private GraphicEntity[][] grid;
private Graphic p;
private int border = 8, cell = 75, rayures = 4, cercles = 6;
private KeyEvent ke;
private MouseEvent clic;
private JTextField jtf;
private class KEDispatcher implements KeyEventDispatcher {
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_PAUSE) ke = e;
}
return false;
}
}
/** @deprecated */
public void mouseClicked(MouseEvent e){
this.setClic(e);
synchronized(this){
this.notify();
}
}
/** @deprecated */
public void mouseEntered(MouseEvent e){}
/** @deprecated */
public void mouseExited(MouseEvent e){}
/** @deprecated */
public void mousePressed(MouseEvent e){}
/** @deprecated */
public void mouseReleased(MouseEvent e){}
/** @deprecated */
public void setClic(MouseEvent e){
this.clic = e;
}
/** Retourne un Point lorsque l'utilisateur clique sur une case. */
public synchronized Point clicCase(){
try{
this.wait();
}
catch(InterruptedException e){}
((JComponent) this.clic.getSource()).transferFocusUpCycle();
return new Point(this.clic.getX()/this.cell,this.clic.getY()/this.cell);
}
public InterfaceCC(int largeur, int hauteur){
super("CC");
this.grid = new GraphicEntity[largeur][hauteur];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[i].length;j++){
this.grid[i][j] = null;
}
}
this.p = new Graphic();
this.p.setPreferredSize(new Dimension(largeur*this.cell, hauteur*this.cell));
this.p.addMouseListener(this);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(this.p,BorderLayout.CENTER);
//this.getContentPane().add(this.p);
this.jtf = new JTextField(20);
this.getContentPane().add(this.jtf,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
this.pack();
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new KEDispatcher());
}
/* Renvoie un entier correspondant à la touche appuyee. CENTER:pause, EAST:fleche droite, WEST:fleche gauche, SOUTH:fleche bas, NORTH:fleche haut
Renvoie -1 si la touche appuyee n'est ni une fleche ni pause. */
public int toucheAppuyee(){
if(this.ke == null) return -1;
else{
int result = -1;
if(this.ke.getKeyCode() == KeyEvent.VK_RIGHT) result = EAST;
if(this.ke.getKeyCode() == KeyEvent.VK_LEFT) result = WEST;
if(this.ke.getKeyCode() == KeyEvent.VK_DOWN) result = SOUTH;
if(this.ke.getKeyCode() == KeyEvent.VK_UP) result = NORTH;
if(this.ke.getKeyCode() == KeyEvent.VK_PAUSE) result = CENTER;
this.ke = null;
return result;
}
}
/* Efface la case d'abscisse x et d'ordonnee y */
public void effaceCase(int x, int y){
this.grid[x][y] = null;
this.miseAJour();
}
public void dessinerCarre(int x, int y, int niveau){
this.grid[x][y] = new Carre(niveau);
this.miseAJour();
}
public void dessinerOvale(int x, int y, int niveau){
this.grid[x][y] = new Ovale(niveau);
this.miseAJour();
}
public void dessinerLarme(int x, int y, int niveau){
this.grid[x][y] = new Larme(niveau);
this.miseAJour();
}
public void dessinerRond(int x, int y, int niveau){
this.grid[x][y] = new Rond(niveau);
this.miseAJour();
}
public void dessinerHexagone(int x, int y, int niveau){
this.grid[x][y] = new Hexagone(niveau);
this.miseAJour();
}
public void dessinerSaucisse(int x, int y, int niveau){
this.grid[x][y] = new Saucisse(niveau);
this.miseAJour();
}
public void miseAJour(){
this.p.repaint();
}
private class Graphic extends JPanel{
public void paint(Graphics gr){
Graphics2D gr2D = (Graphics2D) gr;
gr2D.setColor(Color.BLACK);
gr2D.fillRect(0,0,this.getWidth(),this.getHeight());
gr2D.setStroke(new BasicStroke(2.0f));
//int offset = 2;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[i].length;j++){
if(grid[i][j] != null) grid[i][j].paint(gr2D, new Point(i,j), cell, border);
}
}
}
}
public void afficheMessage(String m){
JOptionPane.showMessageDialog(this,m);
}
public void afficheTexte(String t){
this.jtf.setText(t);
}
} |
Partager