KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > paint > PaintCanvas


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.paint;
21
22 import java.awt.BasicStroke JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.Paint JavaDoc;
28 import java.awt.Point JavaDoc;
29 import java.awt.RenderingHints JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import java.awt.event.MouseListener JavaDoc;
32 import java.awt.event.MouseMotionListener JavaDoc;
33 import java.awt.geom.AffineTransform JavaDoc;
34 import java.awt.image.BufferedImage JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36
37 /**
38  * @author Timothy Boudreau
39  */

40 public class PaintCanvas extends JComponent JavaDoc implements MouseListener JavaDoc, MouseMotionListener JavaDoc {
41     
42     private int diam = 10;
43     private Paint JavaDoc paint = Color.BLUE;
44     private BufferedImage JavaDoc backingImage;
45     private Point JavaDoc last;
46     
47     public PaintCanvas() {
48         addMouseListener(this);
49         addMouseMotionListener(this);
50         setBackground(Color.WHITE);
51     }
52     
53     public void setBrush(int diam) {
54         this.diam = diam;
55     }
56     
57     public void setDiam(int val) {
58         this.diam = val;
59     }
60     
61     public int getDiam() {
62         return diam;
63     }
64     
65     public void setPaint(Paint JavaDoc c) {
66         this.paint = c;
67     }
68     
69     public Paint JavaDoc getPaint() {
70         return paint;
71     }
72     
73     public Color JavaDoc getColor() {
74         if (paint instanceof Color JavaDoc) {
75             return (Color JavaDoc) paint;
76         } else {
77             return Color.BLACK;
78         }
79     }
80     
81     public void clear() {
82         backingImage = null;
83         repaint();
84     }
85     
86     public BufferedImage JavaDoc getImage() {
87         int width = Math.min(getWidth(), 1600);
88         int height = Math.min(getHeight(),1200);
89         if (backingImage == null || backingImage.getWidth() != width || backingImage.getHeight() != height) {
90             BufferedImage JavaDoc old = backingImage;
91             backingImage = new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
92             Graphics JavaDoc g = backingImage.getGraphics();
93             g.setColor(Color.WHITE);
94             g.fillRect(0, 0, width, height);
95             if (old != null) {
96                 ((Graphics2D JavaDoc) backingImage.getGraphics()).drawRenderedImage(old,
97                         AffineTransform.getTranslateInstance(0, 0));
98             }
99         }
100         return backingImage;
101     }
102     
103     public void paint(Graphics JavaDoc g) {
104         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
105         g2d.drawRenderedImage(getImage(), AffineTransform.getTranslateInstance(0,0));
106     }
107     
108     public void mouseClicked(MouseEvent JavaDoc e) {
109         Point JavaDoc p = e.getPoint();
110         Graphics2D JavaDoc g = getImage().createGraphics();
111         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
112         g.setPaint(getPaint());
113         g.setStroke(new BasicStroke JavaDoc(diam, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
114         if (last == null) {
115             last = p;
116         }
117         g.drawLine(last.x, last.y, p.x, p.y);
118         repaint(Math.min(last.x, p.x) - diam / 2 - 1,
119                 Math.min(last.y, p.y) - diam / 2 - 1,
120                 Math.abs(last.x - p.x) + diam + 2,
121                 Math.abs(last.y - p.y) + diam + 2);
122         last = p;
123     }
124     
125     public void mousePressed(MouseEvent JavaDoc e) {
126     }
127     
128     public void mouseReleased(MouseEvent JavaDoc e) {
129     }
130     
131     public void mouseEntered(MouseEvent JavaDoc e) {
132     }
133     
134     public void mouseExited(MouseEvent JavaDoc e) {
135     }
136     
137     public void mouseDragged(MouseEvent JavaDoc e) {
138         mouseClicked(e);
139     }
140     
141     public void mouseMoved(MouseEvent JavaDoc e) {
142         last = null;
143     }
144     
145     JComponent JavaDoc createBrushSizeView() {
146         return new BrushSizeView();
147     }
148     
149     
150     private class BrushSizeView extends JComponent JavaDoc {
151         
152         public boolean isOpaque() {
153             return true;
154         }
155         
156         public void paint(Graphics JavaDoc g) {
157             ((Graphics2D JavaDoc) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
158             g.setColor(getBackground());
159             g.fillRect(0,0,getWidth(),getHeight());
160             Point JavaDoc p = new Point JavaDoc(getWidth() / 2, getHeight() / 2);
161             int half = getDiam() / 2;
162             int diam = getDiam();
163             g.setColor(getColor());
164             g.fillOval(p.x - half, p.y - half, diam, diam);
165         }
166         
167         public Dimension JavaDoc getPreferredSize() {
168             return new Dimension JavaDoc(32, 32);
169         }
170         
171         public Dimension JavaDoc getMinimumSize() {
172             return getPreferredSize();
173         }
174         
175     }
176     
177 }
178
Popular Tags