1 19 20 package org.netbeans.paint; 21 22 import java.awt.BasicStroke ; 23 import java.awt.Color ; 24 import java.awt.Dimension ; 25 import java.awt.Graphics ; 26 import java.awt.Graphics2D ; 27 import java.awt.Paint ; 28 import java.awt.Point ; 29 import java.awt.RenderingHints ; 30 import java.awt.event.MouseEvent ; 31 import java.awt.event.MouseListener ; 32 import java.awt.event.MouseMotionListener ; 33 import java.awt.geom.AffineTransform ; 34 import java.awt.image.BufferedImage ; 35 import javax.swing.JComponent ; 36 37 40 public class PaintCanvas extends JComponent implements MouseListener , MouseMotionListener { 41 42 private int diam = 10; 43 private Paint paint = Color.BLUE; 44 private BufferedImage backingImage; 45 private Point 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 c) { 66 this.paint = c; 67 } 68 69 public Paint getPaint() { 70 return paint; 71 } 72 73 public Color getColor() { 74 if (paint instanceof Color ) { 75 return (Color ) paint; 76 } else { 77 return Color.BLACK; 78 } 79 } 80 81 public void clear() { 82 backingImage = null; 83 repaint(); 84 } 85 86 public BufferedImage 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 old = backingImage; 91 backingImage = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB_PRE); 92 Graphics g = backingImage.getGraphics(); 93 g.setColor(Color.WHITE); 94 g.fillRect(0, 0, width, height); 95 if (old != null) { 96 ((Graphics2D ) backingImage.getGraphics()).drawRenderedImage(old, 97 AffineTransform.getTranslateInstance(0, 0)); 98 } 99 } 100 return backingImage; 101 } 102 103 public void paint(Graphics g) { 104 Graphics2D g2d = (Graphics2D ) g; 105 g2d.drawRenderedImage(getImage(), AffineTransform.getTranslateInstance(0,0)); 106 } 107 108 public void mouseClicked(MouseEvent e) { 109 Point p = e.getPoint(); 110 Graphics2D g = getImage().createGraphics(); 111 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 112 g.setPaint(getPaint()); 113 g.setStroke(new BasicStroke (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 e) { 126 } 127 128 public void mouseReleased(MouseEvent e) { 129 } 130 131 public void mouseEntered(MouseEvent e) { 132 } 133 134 public void mouseExited(MouseEvent e) { 135 } 136 137 public void mouseDragged(MouseEvent e) { 138 mouseClicked(e); 139 } 140 141 public void mouseMoved(MouseEvent e) { 142 last = null; 143 } 144 145 JComponent createBrushSizeView() { 146 return new BrushSizeView(); 147 } 148 149 150 private class BrushSizeView extends JComponent { 151 152 public boolean isOpaque() { 153 return true; 154 } 155 156 public void paint(Graphics g) { 157 ((Graphics2D ) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 158 g.setColor(getBackground()); 159 g.fillRect(0,0,getWidth(),getHeight()); 160 Point p = new Point (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 getPreferredSize() { 168 return new Dimension (32, 32); 169 } 170 171 public Dimension getMinimumSize() { 172 return getPreferredSize(); 173 } 174 175 } 176 177 } 178 | Popular Tags |