source: other-projects/rsyntax-textarea/src/java/org/fife/ui/rtextarea/ImageBackgroundPainterStrategy.java@ 25584

Last change on this file since 25584 was 25584, checked in by davidb, 12 years ago

Initial cut an a text edit area for GLI that supports color syntax highlighting

File size: 4.5 KB
Line 
1/*
2 * 01/22/2005
3 *
4 * ImageBackgroundPainterStrategy.java - Renders an RTextAreaBase's
5 * background as an image.
6 *
7 * This library is distributed under a modified BSD license. See the included
8 * RSyntaxTextArea.License.txt file for details.
9 */
10package org.fife.ui.rtextarea;
11
12import java.awt.Graphics;
13import java.awt.Image;
14import java.awt.MediaTracker;
15import java.awt.Rectangle;
16import java.awt.image.BufferedImage;
17import java.net.URL;
18import javax.imageio.ImageIO;
19
20
21/**
22 * A strategy for painting the background of an <code>RTextAreaBase</code>
23 * as an image. The image is always stretched to completely fill the
24 * <code>RTextAreaBase</code>.<p>
25 *
26 * You can set the scaling hint used when stretching/skewing the image
27 * to fit in the <code>RTextAreaBase</code>'s background via the
28 * <code>setScalingHint</code> method, but keep in mind the more
29 * accurate the scaling hint, the less responsive your application will
30 * be when stretching the window (as that's the only time the image's
31 * size is recalculated).
32 *
33 * @author Robert Futrell
34 * @version 0.1
35 * @see org.fife.ui.rtextarea.BufferedImageBackgroundPainterStrategy
36 * @see org.fife.ui.rtextarea.VolatileImageBackgroundPainterStrategy
37 */
38public abstract class ImageBackgroundPainterStrategy
39 implements BackgroundPainterStrategy {
40
41 protected MediaTracker tracker;
42
43 private RTextAreaBase textArea;
44 private Image master;
45 private int oldWidth, oldHeight;
46 private int scalingHint;
47
48
49 /**
50 * Constructor.
51 *
52 * @param textArea The text area using this image as its background.
53 */
54 public ImageBackgroundPainterStrategy(RTextAreaBase textArea) {
55 this.textArea = textArea;
56 tracker = new MediaTracker(textArea);
57 scalingHint = Image.SCALE_FAST;
58 }
59
60
61 /**
62 * Returns the text area using this strategy.
63 *
64 * @return The text area.
65 */
66 public RTextAreaBase getRTextAreaBase() {
67 return textArea;
68 }
69
70
71 /**
72 * Returns the "master" image; that is, the original, unscaled image.
73 * When the image needs to be rescaled, scaling should be done from
74 * this image, to prevent repeated scaling from distorting the image.
75 *
76 * @return The master image.
77 */
78 public Image getMasterImage() {
79 return master;
80 }
81
82
83 /**
84 * Returns the scaling hint being used.
85 *
86 * @return The scaling hint to use when scaling an image.
87 * @see #setScalingHint
88 */
89 public int getScalingHint() {
90 return scalingHint;
91 }
92
93
94 /**
95 * Paints the image at the specified location and at the specified size.
96 *
97 * @param g The graphics context.
98 * @param bounds The bounds in which to paint the image. The image
99 * will be scaled to fit exactly in these bounds if necessary.
100 */
101 public final void paint(Graphics g, Rectangle bounds) {
102 if (bounds.width!=oldWidth || bounds.height!=oldHeight) {
103 rescaleImage(bounds.width, bounds.height, getScalingHint());
104 oldWidth = bounds.width;
105 oldHeight = bounds.height;
106 }
107 paintImage(g, bounds.x,bounds.y);
108 }
109
110
111 /**
112 * Paints the image at the specified location. This method assumes
113 * scaling has already been done, and simply paints the background
114 * image "as-is."
115 *
116 * @param g The graphics context.
117 * @param x The x-coordinate at which to paint.
118 * @param y The y-coordinate at which to paint.
119 */
120 protected abstract void paintImage(Graphics g, int x, int y);
121
122
123 /**
124 * Rescales the displayed image to be the specified size.
125 *
126 * @param width The new width of the image.
127 * @param height The new height of the image.
128 * @param hint The scaling hint to use.
129 */
130 protected abstract void rescaleImage(int width, int height,
131 int hint);
132
133
134 /**
135 * Sets the image this background painter displays.
136 *
137 * @param imageURL URL of a file containing the image to display.
138 */
139 public void setImage(URL imageURL) {
140 BufferedImage image = null;
141 try {
142 image = ImageIO.read(imageURL);
143 } catch (Exception e) {
144 e.printStackTrace();
145 }
146 setImage(image);
147 }
148
149 /**
150 * Sets the image this background painter displays.
151 *
152 * @param image The new image to use for the background.
153 */
154 public void setImage(Image image) {
155 master = image;
156 oldWidth = -1; // To trick us into fixing bgImage.
157 }
158
159
160 /**
161 * Sets the scaling hint to use when scaling the image.
162 *
163 * @param hint The hint to apply; e.g. <code>Image.SCALE_DEFAULT</code>.
164 * @see #getScalingHint
165 */
166 public void setScalingHint(int hint) {
167 this.scalingHint = hint;
168 }
169
170
171}
Note: See TracBrowser for help on using the repository browser.