1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans-Common.
5 *
6 * CosyBeans-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.components.util;
21
22 import java.awt.Color;
23 import java.awt.TexturePaint;
24 import java.awt.geom.Rectangle2D;
25 import java.awt.image.BufferedImage;
26
27 /**
28 * A paint which renders a 1px dither pattern of the specified color.
29 * Density of the pattern is specified by the level constructor parameter.
30 * All pixels that are not colored, are transparent.
31 *
32 * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
33 */
34 public class DitherPaint extends TexturePaint {
35 /**
36 * 25% dither
37 */
38 public static final int DITHER_25=25;
39 /**
40 * 50% dither
41 */
42 public static final int DITHER_50=50;
43 /**
44 * 75% dither
45 */
46 public static final int DITHER_75=75;
47
48 /**
49 * Creates a new paint with the specified color and density level
50 * @param color The opaque pixels' color
51 * @param ditherLevel DITHER_25, DITHER_50 or DITHER_75
52 */
53 public DitherPaint(Color color,int ditherLevel) {
54 super(createImage(color,ditherLevel),new Rectangle2D.Double(0,0,2,2));
55 }
56
57 private static final BufferedImage createImage(Color color, int ditherLevel) {
58 BufferedImage bufImg=new BufferedImage(2,2,BufferedImage.TYPE_INT_ARGB);
59 switch (ditherLevel) {
60 case DITHER_75:
61 bufImg.setRGB(1,0,color.getRGB());
62 break;
63 case DITHER_50:
64 bufImg.setRGB(1,1,color.getRGB());
65 break;
66 case DITHER_25:
67 bufImg.setRGB(0,0,color.getRGB());
68 break;
69 default:
70 break;
71 }
72 return bufImg;
73 }
74 }