1 | /*
|
---|
2 | * 01/22/2005
|
---|
3 | *
|
---|
4 | * VolatileImageBackgroundPainterStrategy.java - Renders an RTextAreaBase's
|
---|
5 | * background as an image using VolatileImages.
|
---|
6 | *
|
---|
7 | * This library is distributed under a modified BSD license. See the included
|
---|
8 | * RSyntaxTextArea.License.txt file for details.
|
---|
9 | */
|
---|
10 | package org.fife.ui.rtextarea;
|
---|
11 |
|
---|
12 | import java.awt.Graphics;
|
---|
13 | import java.awt.Image;
|
---|
14 | import java.awt.image.VolatileImage;
|
---|
15 |
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * A strategy for painting the background of an <code>RTextAreaBase</code>
|
---|
19 | * as an image. The image is always stretched to completely fill the
|
---|
20 | * <code>RTextAreaBase</code>.<p>
|
---|
21 | *
|
---|
22 | * A <code>java.awt.image.VolatileImage</code> is used for rendering;
|
---|
23 | * theoretically, this should be the best image format for performance.<p>
|
---|
24 | *
|
---|
25 | * You can set the scaling hint used when stretching/skewing the image
|
---|
26 | * to fit in the <code>RTextAreaBase</code>'s background via the
|
---|
27 | * <code>setScalingHint</code> method, but keep in mind the more
|
---|
28 | * accurate the scaling hint, the less responsive your application will
|
---|
29 | * be when stretching the window (as that's the only time the image's
|
---|
30 | * size is recalculated).
|
---|
31 | *
|
---|
32 | * @author Robert Futrell
|
---|
33 | * @version 0.1
|
---|
34 | * @see org.fife.ui.rtextarea.ImageBackgroundPainterStrategy
|
---|
35 | * @see org.fife.ui.rtextarea.BufferedImageBackgroundPainterStrategy
|
---|
36 | */
|
---|
37 | public class VolatileImageBackgroundPainterStrategy
|
---|
38 | extends ImageBackgroundPainterStrategy {
|
---|
39 |
|
---|
40 | private VolatileImage bgImage;
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructor.
|
---|
45 | *
|
---|
46 | * @param ta The text area whose background we'll be painting.
|
---|
47 | */
|
---|
48 | public VolatileImageBackgroundPainterStrategy(RTextAreaBase ta) {
|
---|
49 | super(ta);
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Paints the image at the specified location. This method assumes
|
---|
55 | * scaling has already been done, and simply paints the background
|
---|
56 | * image "as-is."
|
---|
57 | *
|
---|
58 | * @param g The graphics context.
|
---|
59 | * @param x The x-coordinate at which to paint.
|
---|
60 | * @param y The y-coordinate at which to paint.
|
---|
61 | */
|
---|
62 | protected void paintImage(Graphics g, int x, int y) {
|
---|
63 | if (bgImage != null) {
|
---|
64 | do {
|
---|
65 | int rc = bgImage.validate(null);//getGraphicsConfiguration());
|
---|
66 | if (rc==VolatileImage.IMAGE_RESTORED) {
|
---|
67 | // FIXME: If the image needs to be restored are its width
|
---|
68 | // and height still valid?? If not, we'll need to cache
|
---|
69 | // these values...
|
---|
70 | renderImage(bgImage.getWidth(), bgImage.getHeight(),
|
---|
71 | getScalingHint());
|
---|
72 | }
|
---|
73 | g.drawImage(bgImage, x,y, null);
|
---|
74 | } while (bgImage.contentsLost());
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Renders the image at the proper size into <code>bgImage</code>.
|
---|
81 | * This method assumes that <code>bgImage</code> is not
|
---|
82 | * <code>null</code>.
|
---|
83 | *
|
---|
84 | * @param width The width of the volatile image to render into.
|
---|
85 | * @param height The height of the volatile image to render into.
|
---|
86 | * @param hint The scaling hint to use.
|
---|
87 | */
|
---|
88 | protected void renderImage(int width, int height, int hint) {
|
---|
89 | Image master = getMasterImage();
|
---|
90 | if (master!=null) {
|
---|
91 | do {
|
---|
92 | Image i = master.getScaledInstance(width,height, hint);
|
---|
93 | tracker.addImage(i, 1);
|
---|
94 | try {
|
---|
95 | tracker.waitForID(1);
|
---|
96 | } catch (InterruptedException e) {
|
---|
97 | e.printStackTrace();
|
---|
98 | bgImage = null;
|
---|
99 | return;
|
---|
100 | } finally {
|
---|
101 | tracker.removeImage(i, 1);
|
---|
102 | }
|
---|
103 | bgImage.getGraphics().drawImage(i, 0,0, null);
|
---|
104 | tracker.addImage(bgImage, 0);
|
---|
105 | try {
|
---|
106 | tracker.waitForID(0);
|
---|
107 | } catch (InterruptedException e) {
|
---|
108 | e.printStackTrace();
|
---|
109 | bgImage = null;
|
---|
110 | return;
|
---|
111 | } finally {
|
---|
112 | tracker.removeImage(bgImage, 0);
|
---|
113 | }
|
---|
114 | } while (bgImage.contentsLost());
|
---|
115 | } // End of if (master!=null).
|
---|
116 | else {
|
---|
117 | bgImage = null;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Rescales the displayed image to be the specified size.
|
---|
124 | *
|
---|
125 | * @param width The new width of the image.
|
---|
126 | * @param height The new height of the image.
|
---|
127 | * @param hint The scaling hint to use.
|
---|
128 | */
|
---|
129 | protected void rescaleImage(int width, int height, int hint) {
|
---|
130 | bgImage = getRTextAreaBase().createVolatileImage(width, height);
|
---|
131 | if (bgImage!=null)
|
---|
132 | renderImage(width, height, hint);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | } |
---|