Paint
Brush
Instructions: 1. Select
a color. 2. Drag the mouse cursor on the white area to draw a curve.
Complete Source
Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PaintBrush {
private static PaintBrushPanel
myPanel;
private static PaintBrushListener
listener;
private static JFrame myFrame;
private static int[][] xCoord;
private static int[][] yCoord;
private static int[] breakpoint;
private static int position;
private static int curve;
private static Color[] color = {Color.BLACK,
Color.RED, Color.GREEN, Color.BLUE,
Color.CYAN, Color.MAGENTA, Color.YELLOW};
private static Color[] colorRecord;
private static Color currentColor;
private static boolean dragging;
private static boolean mouseOver;
private static Font font;
public static class
PaintBrushPanel extends
JPanel {
public void
paintComponent(Graphics g) {
super.paintComponent(g);
for (int k = 0; k < 7; k++) {
g.setColor(color[k]);
g.fillRect(500,63*k,100,63);
g.setColor(Color.GRAY);
g.drawRect(500,63*k,100,63);
if (currentColor == color[k]) {
g.setColor(new Color(150,0,0));
g.drawRect(500,63*k,100,63);
g.drawRect(501,63*k+1,98,61);
g.drawRect(502,63*k+2,96,59);
g.drawRect(503,63*k+3,94,57);
}
}
g.setColor(Color.GRAY);
g.drawRect(500,441,100,58);
if (mouseOver) {
g.fillRect(500,441,100,58);
g.setColor(Color.BLACK);
}
g.setFont(font);
g.drawString("CLEAR",520,478);
for (int i = 0; i < curve; i++) {
g.setColor(colorRecord[i]);
for (int j = 0; j < breakpoint[i]; j++) {
g.drawLine(xCoord[i][j],yCoord[i][j],xCoord[i][j+1],yCoord[i][j+1]);
}
}
g.setColor(currentColor);
for (int j = 0; j < position; j++) {
g.drawLine(xCoord[curve][j],yCoord[curve][j],xCoord[curve][j+1],yCoord[curve][j+1]);
}
}
}
public static class
PaintBrushListener implements
MouseListener,
MouseMotionListener {
public void
mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (x > 0 && x < 500) {
dragging = true;
colorRecord[curve] = currentColor;
}
for (int k = 0; k < 7; k++) {
if (x > 500 && x < 600 && y > 63*k && y < 63*(k+1)) {
currentColor = color[k];
myPanel.repaint(500,0,100,500);
}
}
if (x >500 && x < 600 && y > 441 && y < 500) {
for (int i = 0; i < curve; i++) {
for (int j = 0; j < breakpoint[i]; j++) {
xCoord[i][j] = 0;
yCoord[i][j] = 0;
}
breakpoint[i] = 0;
colorRecord[i] = null;
}
curve = 0;
myPanel.repaint(0,0,500,500);
}
}
public void
mouseReleased(MouseEvent e) {
dragging = false;
if (position > -1) {
breakpoint[curve] = position;
position = -1;
curve++;
}
}
public void
mouseClicked(MouseEvent e) {}
public void
mouseEntered(MouseEvent e) {}
public void
mouseExited(MouseEvent e) {}
public void
mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (x > 500 && x < 600 && y > 441 && y < 500) {
mouseOver = true;
myPanel.repaint(500,441,100,58);
}
else {
mouseOver = false;
myPanel.repaint(500,441,100,58);
}
}
public void
mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (dragging) {
position++;
if (x < 500 && x > 0) xCoord[curve][position] = x;
else if (x >= 500) xCoord[curve][position] = 499;
else if (x <= 0) xCoord[curve][position] = 0;
if (y < 500 && y > 0) yCoord[curve][position] = y;
else if (y >= 500) yCoord[curve][position] = 499;
else if (y <= 0) yCoord[curve][position] = 0;
myPanel.repaint(0,0,500,500);
}
}
}
public static void
main(String[] args) {
myPanel = new
PaintBrushPanel();
listener = new
PaintBrushListener();
myFrame = new
JFrame("PaintBrush");
xCoord = new
int[50][5000];
yCoord = new
int[50][5000];
breakpoint = new
int[50];
colorRecord = new
Color[50];
position = -1;
curve = 0;
currentColor =
color[0];
dragging = false;
mouseOver =
false;
font = new
Font("Ariel",Font.PLAIN,18);
myPanel.setBackground(Color.WHITE);
myPanel.addMouseListener(listener);
myPanel.addMouseMotionListener(listener);
myFrame.add(myPanel);
myFrame.setSize(606,528);
myFrame.setLocation(100,100);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setResizable(false);
myFrame.setVisible(true);
}
}
|