001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * * Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * * Neither the name of Sun Microsystems, Inc. nor the names of its contributors
015: * may be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.netbeans.paint;
032:
033: import java.awt.BasicStroke;
034: import java.awt.Color;
035: import java.awt.Dimension;
036: import java.awt.Graphics;
037: import java.awt.Graphics2D;
038: import java.awt.Paint;
039: import java.awt.Point;
040: import java.awt.RenderingHints;
041: import java.awt.event.MouseEvent;
042: import java.awt.event.MouseListener;
043: import java.awt.event.MouseMotionListener;
044: import java.awt.geom.AffineTransform;
045: import java.awt.image.BufferedImage;
046: import javax.swing.JComponent;
047:
048: /**
049: * @author Timothy Boudreau
050: */
051: public class PaintCanvas extends JComponent implements MouseListener,
052: MouseMotionListener {
053:
054: private int diam = 10;
055: private Paint paint = Color.BLUE;
056: private BufferedImage backingImage;
057: private Point last;
058:
059: public PaintCanvas() {
060: addMouseListener(this );
061: addMouseMotionListener(this );
062: setBackground(Color.WHITE);
063: }
064:
065: public void setBrush(int diam) {
066: this .diam = diam;
067: }
068:
069: public void setDiam(int val) {
070: this .diam = val;
071: }
072:
073: public int getDiam() {
074: return diam;
075: }
076:
077: public void setPaint(Paint c) {
078: this .paint = c;
079: }
080:
081: public Paint getPaint() {
082: return paint;
083: }
084:
085: public Color getColor() {
086: if (paint instanceof Color) {
087: return (Color) paint;
088: } else {
089: return Color.BLACK;
090: }
091: }
092:
093: public void clear() {
094: backingImage = null;
095: repaint();
096: }
097:
098: public BufferedImage getImage() {
099: int width = Math.min(getWidth(), 1600);
100: int height = Math.min(getHeight(), 1200);
101: if (backingImage == null || backingImage.getWidth() != width
102: || backingImage.getHeight() != height) {
103: BufferedImage old = backingImage;
104: backingImage = new BufferedImage(width, height,
105: BufferedImage.TYPE_INT_ARGB_PRE);
106: Graphics g = backingImage.getGraphics();
107: g.setColor(Color.WHITE);
108: g.fillRect(0, 0, width, height);
109: if (old != null) {
110: ((Graphics2D) backingImage.getGraphics())
111: .drawRenderedImage(old, AffineTransform
112: .getTranslateInstance(0, 0));
113: }
114: }
115: return backingImage;
116: }
117:
118: public void paint(Graphics g) {
119: Graphics2D g2d = (Graphics2D) g;
120: g2d.drawRenderedImage(getImage(), AffineTransform
121: .getTranslateInstance(0, 0));
122: }
123:
124: public void mouseClicked(MouseEvent e) {
125: Point p = e.getPoint();
126: Graphics2D g = getImage().createGraphics();
127: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
128: RenderingHints.VALUE_ANTIALIAS_ON);
129: g.setPaint(getPaint());
130: g.setStroke(new BasicStroke(diam, BasicStroke.CAP_ROUND,
131: BasicStroke.JOIN_ROUND));
132: if (last == null) {
133: last = p;
134: }
135: g.drawLine(last.x, last.y, p.x, p.y);
136: repaint(Math.min(last.x, p.x) - diam / 2 - 1, Math.min(last.y,
137: p.y)
138: - diam / 2 - 1, Math.abs(last.x - p.x) + diam + 2, Math
139: .abs(last.y - p.y)
140: + diam + 2);
141: last = p;
142: }
143:
144: public void mousePressed(MouseEvent e) {
145: }
146:
147: public void mouseReleased(MouseEvent e) {
148: }
149:
150: public void mouseEntered(MouseEvent e) {
151: }
152:
153: public void mouseExited(MouseEvent e) {
154: }
155:
156: public void mouseDragged(MouseEvent e) {
157: mouseClicked(e);
158: }
159:
160: public void mouseMoved(MouseEvent e) {
161: last = null;
162: }
163:
164: JComponent createBrushSizeView() {
165: return new BrushSizeView();
166: }
167:
168: private class BrushSizeView extends JComponent {
169:
170: public boolean isOpaque() {
171: return true;
172: }
173:
174: public void paint(Graphics g) {
175: ((Graphics2D) g).setRenderingHint(
176: RenderingHints.KEY_ANTIALIASING,
177: RenderingHints.VALUE_ANTIALIAS_ON);
178: g.setColor(getBackground());
179: g.fillRect(0, 0, getWidth(), getHeight());
180: Point p = new Point(getWidth() / 2, getHeight() / 2);
181: int half = getDiam() / 2;
182: int diam = getDiam();
183: g.setColor(getColor());
184: g.fillOval(p.x - half, p.y - half, diam, diam);
185: }
186:
187: public Dimension getPreferredSize() {
188: return new Dimension(32, 32);
189: }
190:
191: public Dimension getMinimumSize() {
192: return getPreferredSize();
193: }
194:
195: }
196:
197: }
|