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
| public class PanelPaint extends JPanel{
private int pointCount=0;
private Color currentColor;
//array of 10000java.awt.Point references
private DrawingPoint []points = new DrawingPoint [10000];
public PanelPaint(){
addMouseMotionListener(
new MouseMotionAdapter(){
public void mouseDragged(MouseEvent event){
if(pointCount<points.length){
points[ pointCount ] = new DrawingPoint (event.getPoint(), currentColor ); // find point
++pointCount; // increment number of points in array
repaint(); // repaint JFrame
} // end if
} // end method mouseDragged
}
);
}
// draw ovals in a 4-by-4 bounding box at specified locations on window
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // clears drawing area
// draw all points in array
for ( int i = 0; i < pointCount; i++ )
g.setColor( points[i].color );
g.fillOval( points[i].x, points[i].y, 4, 4 );
} // end method paintComponent
// la méthode dont je te parlais, qu'il faut appeler dans l'ActionListener, mais aussi au moins une fois au début pour initialiser la première couleur
public void setDrawingColor(Color color) {
currentColor=color;
}
} // end class PaintPanel |