Paint Tools
Instead of using the Graphics class, I used its subclass, Graphics2D, to create the graphical output. If the pencil tool used the Graphics' fillOval() to simulate a round curvy line, the pencil would draw lines with several unwanted gaps. Often, the program would have difficulty drawing ovals between certain coordinates at the rate that the user would be dragging the mouse. By using the Graphics2D's setStroke(), the pencil tool has a rounder stroke and fewer gaps in between.
36 37 38 39 | private void pencil( int oldX, int oldY, int newX, int newY) { // Pencil tool with a rounded stroke cgBuffer.setStroke( new BasicStroke(menuBar.getStrokeSize(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); cgBuffer.drawLine(oldX,oldY,newX,newY); } |
private void pencil(int oldX, int oldY, int newX, int newY) { // Pencil tool with a rounded stroke cgBuffer.setStroke(new BasicStroke(menuBar.getStrokeSize(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); cgBuffer.drawLine(oldX,oldY,newX,newY); }
The setStroke() also gave the brush tool its unique edges by creating a BasicStroke object with a capped butt (ends lines with no added decoration) and a miter join (extending the outside edges until they meet).
41 42 43 44 | private void brush( int oldX, int oldY, int newX, int newY) { // Brush tool (has "missing" lines in its stroke at a curve) cgBuffer.setStroke( new BasicStroke(menuBar.getStrokeSize(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); cgBuffer.drawLine(oldX,oldY,newX,newY); } |
private void brush(int oldX, int oldY, int newX, int newY) { // Brush tool (has "missing" lines in its stroke at a curve) cgBuffer.setStroke(new BasicStroke(menuBar.getStrokeSize(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); cgBuffer.drawLine(oldX,oldY,newX,newY); }
The spray tool consists of several tiny circles drawn randomly within the user requested stroke size around the mouse's current coordinates.
46 47 48 49 50 51 52 53 54 | private void spray( int newX, int newY) { // Spray can tool Random rand = new Random(); for ( int k= 1 ; k< 2 *menuBar.getStrokeSize(); k++) { // Several tiny random circles are painted around the current coordinate randX = rand.nextInt(menuBar.getStrokeSize()) + newX - ( int ) menuBar.getStrokeSize()/ 2 ; randY = rand.nextInt(menuBar.getStrokeSize()) + newY - ( int ) menuBar.getStrokeSize()/ 2 ; cgBuffer.fillOval(randX, randY, ( int ) 3 , ( int ) 3 ); } } |
private void spray(int newX, int newY) { // Spray can tool Random rand = new Random(); for (int k=1; k<2*menuBar.getStrokeSize(); k++) { // Several tiny random circles are painted around the current coordinate randX = rand.nextInt(menuBar.getStrokeSize()) + newX - (int) menuBar.getStrokeSize()/2; randY = rand.nextInt(menuBar.getStrokeSize()) + newY - (int) menuBar.getStrokeSize()/2; cgBuffer.fillOval(randX, randY, (int) 3, (int) 3); } }