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
| package whiteboard;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.lang.Object;
import javax.imageio.ImageIO;
import java.io.*;
public class Whiteboard extends JFrame implements MouseListener, MouseMotionListener, WindowFocusListener {
private JToggleButton buttonPen, buttonLine, buttonRectangle, buttonCircle, buttonText;
private JButton buttonArray, buttonErase, buttonRepaint, buttonVider, buttonColor, buttonUndo;
private JComboBox choiceThickness;
private JToolBar toolBar;
private JPanel drawArea;
private ButtonGroup group;
private int lastX=0, lastY=0;
private int x1, y1, x2, y2;
private ArrayList<Shape> tab = new ArrayList<Shape>(30);
GeneralPath path;
/** Constructeur de test */
public Whiteboard() {
//titre de la fenetre
super("SIP whiteboard");
this.setSize(new Dimension(650,500));
this.setResizable(false);
this.setLocationRelativeTo(this.getParent());
Color newColor;
// Container
Container content = getContentPane();
content.setBackground(Color.lightGray);
// Panels
JPanel controlArea = new JPanel(new GridLayout(12,0,0,8));
drawArea = new JPanel();
controlArea.setPreferredSize(new Dimension(100,200));
drawArea.setBackground(Color.WHITE);
drawArea.setPreferredSize(new Dimension(540,0));
drawArea.addMouseListener(this);
drawArea.addMouseMotionListener(this);
this.addWindowFocusListener(this);
content.add(controlArea,BorderLayout.WEST);
content.add(drawArea,BorderLayout.EAST);
JColorChooser AllColors = new JColorChooser();
// Boutons
buttonPen = new JToggleButton("Stylo",true);
buttonLine = new JToggleButton("Ligne");
buttonRectangle = new JToggleButton("Rectangle");
buttonCircle = new JToggleButton("Cercle");
buttonText = new JToggleButton("Texte");
buttonArray = new JButton("nb Objets");
buttonErase = new JButton("Effacer");
buttonRepaint = new JButton("Redessiner");
buttonVider = new JButton("Vider tableau");
buttonColor = new JButton("Couleurs");
buttonUndo = new JButton("Annuler");
choiceThickness = new JComboBox(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" });
toolBar = new JToolBar();
try {
buttonPen.setIcon( new ImageIcon (ImageIO.read (getClass ().getResourceAsStream ("resources/line.png"))));
buttonLine.setIcon( new ImageIcon (ImageIO.read (getClass ().getResourceAsStream ("resources/straight_line.png"))));
buttonRectangle.setIcon( new ImageIcon (ImageIO.read (getClass ().getResourceAsStream ("resources/square.png"))));
buttonCircle.setIcon( new ImageIcon (ImageIO.read (getClass ().getResourceAsStream ("resources/circle.png"))));
buttonText.setIcon( new ImageIcon (ImageIO.read (getClass ().getResourceAsStream ("resources/text.png"))));
} catch(IOException e) {
System.out.println("Erreur de chargement : " + e.toString());
}
buttonPen.addActionListener(new TraitementbuttonPen());
buttonLine.addActionListener(new TraitementbuttonLine());
buttonRectangle.addActionListener(new TraitementbuttonLine());
buttonCircle.addActionListener(new TraitementbuttonLine());
buttonText.addActionListener(new TraitementbuttonLine());
buttonArray.addActionListener(new TraitementbuttonArray());
buttonErase.addActionListener(new TraitementbuttonErase());
buttonRepaint.addActionListener(new TraitementbuttonRepaint());
buttonVider.addActionListener(new TraitementbuttonVider());
buttonColor.addActionListener(new TraitementbuttonColor());
choiceThickness.addActionListener(new TraitementchoiceThickness());
// Groupes les boutons
group = new ButtonGroup();
group.add(buttonPen);
group.add(buttonLine);
group.add(buttonRectangle);
group.add(buttonCircle);
group.add(buttonText);
// Les ajouter au JPanel
controlArea.add(buttonPen);
controlArea.add(buttonLine);
controlArea.add(buttonRectangle);
controlArea.add(buttonCircle);
controlArea.add(buttonText);
controlArea.add(buttonArray);
controlArea.add(buttonRepaint);
controlArea.add(buttonErase);
controlArea.add(buttonVider);
controlArea.add(buttonColor);
controlArea.add(choiceThickness);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//pack(); //permet de mettre une bonne dimension a la fenetre
setVisible(true);
}
public void windowGainedFocus(WindowEvent e){
drawArea.repaint();
}
public void windowLostFocus(WindowEvent e){
drawArea.repaint();
}
public void mouseEntered(MouseEvent event) {
requestFocus();
}
public void mousePressed(MouseEvent event) {
x1 = event.getX();
y1 = event.getY();
record(x1,y1);
if(buttonPen.isSelected()==true)path = new GeneralPath();
}
public void mouseReleased(MouseEvent event){
Graphics g = drawArea.getGraphics();
Graphics2D g2 = (Graphics2D)g;
// Crayon
if (buttonPen.isSelected() == true){
g2.setStroke(new BasicStroke(8,BasicStroke.CAP_ROUND,BasicStroke.CAP_ROUND));
tab.add(path); //on ajoute le tracé au tableau d'éléments
//g2.draw(path);
// Segment
} else if (buttonLine.isSelected() == true){
Line2D l = new Line2D.Float(x1,y1,event.getX(),event.getY());
tab.add(l);
g2.draw(l);
// Rectangle
} else if (buttonRectangle.isSelected() == true) {
g2.setStroke(new BasicStroke(8,BasicStroke.CAP_ROUND,BasicStroke.CAP_ROUND));
Rectangle2D r = new Rectangle2D.Float(x1,y1,Math.abs(event.getX() - x1), Math.abs(event.getY() - y1));
tab.add(r);
g2.draw(r);
// Cercle
} else if (buttonCircle.isSelected() == true) {
Ellipse2D c = new Ellipse2D.Float(x1,y1,event.getX() - x1,event.getX() - x1);
tab.add(c);
g2.draw(c);
}
}
public void mouseDragged(MouseEvent event) {
int x = event.getX();
int y = event.getY();
Graphics g = drawArea.getGraphics();
Graphics2D g2 = (Graphics2D)g;
if (buttonPen.isSelected() == true){
g2.setPaint(Color.blue);
g2.setStroke(new BasicStroke(8,BasicStroke.CAP_ROUND,BasicStroke.CAP_ROUND));
path.moveTo(lastX,lastY);
path.lineTo(x,y);
g2.draw(path);
}
else if(buttonLine.isSelected() == true) {
}
record(x,y);
}
private void record(int x, int y) {
lastX = x;
lastY = y;
}
public void mouseMoved(MouseEvent evt){}
public void mouseExited(MouseEvent evt){}
public void mouseClicked(MouseEvent event){}
public static void main(String args[]) {
new Whiteboard();
}
public class TraitementchoiceThickness implements ActionListener {
public void actionPerformed(ActionEvent e){
}
}
public class TraitementbuttonPen implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Ici !");
}
}
public class TraitementbuttonLine implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
public class TraitementbuttonRepaint implements ActionListener {
public void actionPerformed(ActionEvent e) {
paintComponent(drawArea.getGraphics());
}
}
public class TraitementbuttonErase implements ActionListener {
public void actionPerformed(ActionEvent e) {
//tab.clear();
drawArea.repaint();
}
}
public class TraitementbuttonArray implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Nombre d'éléments : " + tab.size());
}
}
public class TraitementbuttonVider implements ActionListener {
public void actionPerformed(ActionEvent e) {
tab.clear();
drawArea.repaint();
}
}
public class TraitementbuttonColor implements ActionListener {
public void actionPerformed(ActionEvent e) {
Color newColor = JColorChooser.showDialog(new JFrame(), "Choisissez la Couleur",Color.white);
}
}
protected void paintComponent(Graphics g){
super.paintComponent(g); // ne trouve pas la methode paintComponent!
Graphics2D g2 = (Graphics2D) g;
int i;
for (i=0;i<tab.size();i++){
System.out.println("elt " + i + " -> " + tab.get(i));
Object a = tab.get(i);
g2.draw((Shape)a);
}
}
} |