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
|
import java.awt.*;
import java.util.Vector;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Dessin.java
*
* Created on 5 janv. 2009, 11:04:53
*/
/**
*
* @author x
*/
public class Dessin extends javax.swing.JPanel {
Vector figuresList;
int figure;
Point start;
/** Creates new form Dessin */
public Dessin() {
initComponents();
figuresList = new Vector();
start = new Point();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setBackground(new java.awt.Color(255, 255, 255));
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
formMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
formMouseReleased(evt);
}
});
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
formMouseDragged(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
private void formMouseDragged(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
//dessin à main levée
if (figure == 1){
Trace t = (Trace)figuresList.lastElement();
t.add(evt.getX(), evt.getY());
t.draw(this.getGraphics());
}
//Rectangle à dessiner
else if (figure == 2){
repaint();
draw(this.getGraphics());
Rect r = new Rect(Color.BLACK,start.x, start.y,
evt.getX(), evt.getY());
r.draw(this.getGraphics());
this.getGraphics().dispose();
}
}
private void formMousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if (figure == 1){
figuresList.add(new Trace(Color.BLACK));
}
else if(figure == 2){
start.x = evt.getX();
start.y = evt.getY();
}
}
private void formMouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(figure == 2){
figuresList.add(new Rect(Color.BLACK,
start.x, start.y, evt.getX(), evt.getY() ));
repaint();
}
}
public void draw(Graphics g){
Figure f;
for(int i = 0 ; i < figuresList.size() ; i++) {
f = (Figure)figuresList.elementAt(i);
f.draw(g);
}
}
// Variables declaration - do not modify
// End of variables declaration
}
abstract class Figure {
abstract void draw(Graphics g);
}
/**
*
* @author x
*/
class Trace extends Figure {
Color c;
Vector pointsArray;
Trace(Color c) {
this.c = c;
pointsArray = new Vector();
}
void add(int x, int y) {
pointsArray.addElement(new Point(x , y));
}
Point elementAt(int n) {
if (pointsArray.capacity() < n) return null;
return (Point) pointsArray.elementAt(n);
}
int nbPoints() {
return pointsArray.size();
}
void draw(Graphics g) {
int n = nbPoints();
if(n <= 1) return;
g.setColor(c);
int x_last = elementAt(0).x;
int y_last = elementAt(0).y;
int x,y;
for(int i = 1 ; i < n ; i++) {
x = elementAt(i).x;
y = elementAt(i).y;
g.drawLine(x_last, y_last, x, y);
x_last = x;
y_last = y;
}
}
}
class Rect extends Figure{
Color c;
Point p1, p2;
Rect(Color c, int x1, int y1, int x2, int y2) {
this.c = c;
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}
void draw(Graphics g) {
int x, y, width, height;
g.setColor(c);
x = Math.min(p1.x, p2.x);
y = Math.min(p1.y, p2.y);
width = Math.abs(p1.x - p2.x);
height = Math.abs(p1.y - p2.y);
g.drawRect(x, y, width, height);
}
} |
Partager