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
| package tp.dessinons;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author pinps
*/
public class FenPrincipal extends JFrame {
private JButton btn_triangle;
private JButton btn_carre;
private JButton btn_cercle;
private JButton btn_raz;
private JLabel lbl_forme;
private JLabel lbl_couleur;
private JButton btn_bleu;
private JButton btn_jaune;
private JButton btn_rouge;
private JButton btn_vert;
private JButton btn_noir;
private Dessin zone_centre;
public static void main(String[] args) {
//Description de la fenetre principal
FenPrincipal fen=new FenPrincipal();
fen.setTitle("Dessinons");
fen.setSize(520,300);
fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fen.setVisible(true);
}
public enum FORME{
RECTANGLE, CERCLE, TRIANGLE;
}
public class Dessin extends JPanel implements MouseListener{
ArrayList<Forme> liste;
public Dessin(){
liste=new ArrayList<>();
}
private FORME formeCourante;
public void setFormeCourante(FORME formeCourante){
this.formeCourante=formeCourante;
}
@Override
public void mouseClicked(MouseEvent e){
e.getX();
e.getY();
abstract Forme forme;
switch(formeCourante){
case RECTANGLE :
forme=new FormeRectangle(260,150,20,20);
break;
case CERCLE :
forme=new FormeEllipse(260,150,20,20);
break;
case TRIANGLE :
forme= new FormeTriangle(260,150,20,20);
break;
default :
return;
}
liste.add(forme);
};
@Override
public void mouseEntered(MouseEvent e){};
@Override
public void mouseExited(MouseEvent e){};
@Override
public void mousePressed(MouseEvent e){};
@Override
public void mouseReleased(MouseEvent e){};
public abstract class Forme{
protected int x;
protected int y;
protected int width;
protected int height;
public Forme(int x, int y, int width, int height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public abstract void draw(Graphics2D g);
public abstract void fill(Graphics2D g);
}
public class FormeRectangle extends Forme{
public FormeRectangle (int x, int y, int width, int height){
super(x, y, width, height);
}
@Override
public void draw(Graphics2D g){
g.drawRect(x, y, width, height);
}
@Override
public void fill(Graphics2D g){
g.fillRect(x, y, width, height);
}
}
public class FormeEllipse extends Forme{
public FormeEllipse(int x, int y, int width, int height){
super(x, y, width, height);
}
@Override
public void draw(Graphics2D g){
g.drawOval(x, y, width, height);
}
@Override
public void fill(Graphics2D g){
g.fillOval(x, y, width, height);
}
}
public class FormeTriangle extends Forme{
private int[] xtriangle;
private int[] ytriangle;
public FormeTriangle(int x, int y, int width, int height){
super(x, y, width, height);
xtriangle=new int[]{x, x+width/2, x+width};
ytriangle=new int[]{y+height, y, y+height};
}
@Override
public void draw(Graphics2D g){
g.drawPolygon(xtriangle, ytriangle, 3);
}
@Override
public void fill(Graphics2D g){
g.fillPolygon(xtriangle, ytriangle, 3);
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
for(Forme forme : liste){
forme.draw(g2d);
}
}
}
public FenPrincipal(){
//String forme="";
BoutonForme boutonforme=new BoutonForme();
//Création de la zonne du haut
JPanel zone_haut=new JPanel();
//Création de boutons et libéllés
btn_triangle=new JButton("Triangle");
btn_carre=new JButton("Carré");
btn_cercle=new JButton("Cercle");
btn_raz=new JButton("RAZ");
JLabel lbl_formetxt=new JLabel("Forme : ");
lbl_forme=new JLabel();
JLabel lbl_couleurtxt=new JLabel("Couleur : ");
lbl_couleur=new JLabel();
//Ajout des boutons et libellés à la zonne du haut
zone_haut.add(btn_triangle);
zone_haut.add(btn_carre);
zone_haut.add(btn_cercle);
zone_haut.add(btn_raz);
zone_haut.add(lbl_formetxt);
zone_haut.add(lbl_forme);
zone_haut.add(lbl_couleurtxt);
zone_haut.add(lbl_couleur);
//Enregistrement du recepteur d'action
btn_triangle.addActionListener(boutonforme);
btn_carre.addActionListener(boutonforme);
btn_cercle.addActionListener(boutonforme);
btn_raz.addActionListener(boutonforme);
//Placement de la zone du haut
getContentPane().add(BorderLayout.NORTH, zone_haut);
//Création de la zone du bas
JPanel zone_bas=new JPanel();
//Création des boutons de la zone du bas
btn_bleu=new JButton("Bleu");
btn_bleu.setBackground(Color.blue);
btn_jaune=new JButton("Jaune");
btn_jaune.setBackground(Color.yellow);
btn_rouge=new JButton("Rouge");
btn_rouge.setBackground(Color.red);
btn_vert=new JButton("Vert");
btn_vert.setBackground(Color.green);
btn_noir=new JButton("Noir");
btn_noir.setBackground(Color.black);
//Ajout des boutons à la zone du bas
zone_bas.add(btn_bleu);
zone_bas.add(btn_jaune);
zone_bas.add(btn_rouge);
zone_bas.add(btn_vert);
zone_bas.add(btn_noir);
//Enregistrement des recepteurs d'action
btn_bleu.addActionListener(boutonforme);
btn_jaune.addActionListener(boutonforme);
btn_rouge.addActionListener(boutonforme);
btn_vert.addActionListener(boutonforme);
btn_noir.addActionListener(boutonforme);
//Placement de la zone du bas
getContentPane().add(BorderLayout.SOUTH, zone_bas);
//Création de la zone du centre
zone_centre=new Dessin();
zone_centre.addMouseListener(zone_centre);
zone_centre.setBackground(Color.white);
//Placement de la zone du centre
getContentPane().add(BorderLayout.CENTER, zone_centre);
}
public class BoutonForme extends JButton implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//Action à effectuer sur click du bouton
if (e.getSource()== btn_triangle){
lbl_forme.setText("Triangle");
}else if (e.getSource()== btn_carre){
lbl_forme.setText("Carre");
}else if (e.getSource()== btn_cercle){
lbl_forme.setText("Cercle");
}
if (e.getSource()== btn_bleu){
lbl_couleur.setText("Bleu");
}else if (e.getSource()== btn_jaune){
lbl_couleur.setText("Jaune");
}else if (e.getSource()== btn_rouge){
lbl_couleur.setText("Rouge");
}else if (e.getSource()== btn_vert){
lbl_couleur.setText("Vert");
}else if (e.getSource()== btn_noir){
lbl_couleur.setText("Noir");
}
}
}
} |