KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > painter > BackgroundPainter


1 package org.joshy.html.painter;
2
3 import java.awt.Image JavaDoc;
4 import java.awt.Graphics JavaDoc;
5 import java.awt.Color JavaDoc;
6 import java.awt.Rectangle JavaDoc;
7 import java.awt.Shape JavaDoc;
8 import org.joshy.u;
9 import org.joshy.html.*;
10 import org.joshy.html.box.Box;
11
12 public class BackgroundPainter {
13     public static void paint(Context c, Box block) {
14         
15          Rectangle JavaDoc box = new Rectangle JavaDoc(
16                 block.x + block.margin.left + block.border.left,
17                 block.y + block.margin.top + block.border.top,
18                 block.width - block.margin.left - block.margin.right - block.border.left - block.border.right,
19                 block.height - block.margin.top - block.border.top - block.border.bottom - block.margin.bottom
20                 );
21                 
22         //u.p("BackgroundPainter.paint(" + block + " bounds = " + box);
23
//u.p("bg color = " + block.background_color);
24
//u.p("canvas = " + c.canvas);
25
// paint the background
26
if(block.background_color != null) {
27             c.getGraphics().setColor(block.background_color);
28             c.getGraphics().fillRect(box.x,box.y,box.width,box.height);
29         }
30         
31         int xoff = 0;
32         int yoff = 0;
33         if(block.attachment != null && block.attachment.equals("fixed")) {
34             //u.p("fixed: offset = " + c.canvas.getLocation());
35
yoff = c.canvas.getLocation().y;
36             c.graphics.setColor(Color.GREEN);
37             //u.p("drawing line at " + (50 - yoff));
38
//c.graphics.setClip(c.canvas.getX(),c.canvas.getY(),c.canvas.getWidth(),c.canvas.getHeight());
39
c.graphics.setClip(c.canvas.getVisibleRect());
40             //c.graphics.drawLine(0,0-yoff,50,50-yoff);
41
/*
42             u.p("clip bounds = " + c.graphics.getClipBounds());
43             u.p("bounds = " + c.canvas.getBounds());
44             u.p("vis rec t= " + c.canvas.getVisibleRect());
45             if(c.graphics.getClipBounds() != null) {
46             if(c.graphics.getClipBounds().equals(c.canvas.getVisibleRect())) {
47                 u.p("equal");
48             } else {
49                 u.p("not equal");
50                 //c.canvas.repaint(c.canvas.getVisibleRect());
51                 //RepaintManager.currentManager(c.canvas).markCompletelyDirty(c.canvas);
52             }
53             } else {
54                 u.p("null");
55             }
56             */

57         }
58         
59         if(block.background_image != null) {
60             int left_insets = box.x;
61             int top_insets = box.y;
62             int back_width = box.width;
63             int back_height = box.height;
64             Shape JavaDoc oldclip = c.getGraphics().getClip();
65             c.getGraphics().setClip(left_insets, top_insets, back_width, back_height);
66             
67             // calculate repeat indecies
68
int repeatx = 1;
69             int repeaty = 1;
70             
71             if(block.repeat == null) {
72                 repeatx = 1;
73                 repeaty = 1;
74             } else if (block.repeat.equals("repeat-x")) {
75                 repeatx = back_width;
76             } else if (block.repeat.equals("repeat-y")) {
77                 repeaty = back_height;
78             } else if (block.repeat.equals("repeat")) {
79                 repeatx = back_width;
80                 repeaty = back_height;
81             }
82             
83             
84             double iwd = block.background_image.getWidth(null);
85             double ihd = block.background_image.getHeight(null);
86             int iw = block.background_image.getWidth(null);
87             int ih = block.background_image.getHeight(null);
88             
89             // handle image position offsets
90
//u.p("doing vertical bottom align");
91
//u.p("yoff = " + yoff);
92
//u.p("vert = " + block.background_position_vertical);
93
yoff -= (int)((double)(back_height - ih)*(double)((double)block.background_position_vertical/(double)100));
94             xoff -= (int)((double)(back_width - iw)*(double)((double)block.background_position_horizontal/(double)100));
95             //u.p("yoff = " + yoff);
96

97             // calculations for fixed tile images
98
int starty = (int) Math.ceil((double)(top_insets+yoff)/ih);
99             int endy = (int) Math.ceil((double)(back_height+top_insets+yoff)/ih);
100             int startx = (int) Math.ceil((double)(left_insets)/iw);
101             int endx = (int) Math.ceil((double)(back_width+left_insets)/iw);
102             //u.p("startx = " + startx + " endx = " + endx);
103

104             //u.p("back height = " + back_height + " img height = " + img.getHeight(null));
105
//u.p("div = " + ((double)back_height+c.getExtents().y/(double)img.getHeight(null)));
106
//u.p("start y = " + starty + " end y = " + endy);
107
//u.p("clip = " + c.getGraphics().getClip());
108
// tile the image as appropriate
109

110             //u.p("got here 3");
111
// do fixed tile image
112

113             boolean horiz = false;
114             boolean vert = false;
115             if(block.repeat.equals("repeat-x")) {
116                 horiz = true;
117                 vert = false;
118             }
119             if(block.repeat.equals("repeat-y")) {
120                 horiz = false;
121                 vert = true;
122             }
123             if(block.repeat.equals("repeat")) {
124                 horiz = true;
125                 vert = true;
126             }
127
128             if(block.attachment != null && block.attachment.equals("fixed")) {
129                 tileFill(c.getGraphics(), block.background_image,
130                     new Rectangle JavaDoc( left_insets, top_insets, back_width, back_height ),
131                     0, -yoff, horiz, vert);
132                 /*
133                 for(int i=startx; i<=endx; i++) {
134                     for(int j=starty; j<=endy; j++) {
135                         int fx = iw * i;
136                         //u.p("yoff = " + yoff);
137                         int fy = block.background_image.getHeight(null) * j - yoff;
138                         //u.p("drawing at: " + fx + " " + fy + " " +
139                         // img.getWidth(null) + " " + img.getHeight(null));
140                         c.graphics.drawImage(
141                             block.background_image,
142                             fx-block.background_image.getWidth(null),
143                             fy-block.background_image.getHeight(null),null);
144                         //c.graphics.fillRect(fx-img.getWidth(null),fy-img.getHeight(null),
145                         // img.getWidth(null),img.getHeight(null));
146                     }
147                 }
148                 */

149             } else {
150             // do normal tile image
151
//u.p("normal looping");
152

153                 tileFill(c.getGraphics(), block.background_image,
154                     new Rectangle JavaDoc( left_insets, top_insets, back_width, back_height ),
155                     0, -yoff, horiz, vert);
156                 /*
157                 for(int i=0; i<repeatx; i+=block.background_image.getWidth(null)) {
158                     for(int j=0; j<repeaty; j+=block.background_image.getHeight(null)) {
159                         //u.p("width = " + block.background_image.getWidth(null));
160                         int fx = left_insets+i;
161                         int fy = top_insets+j-yoff;
162                         //u.p("fx = " + fx + " fy = " + fy);
163                         c.graphics.drawImage(block.background_image,fx,fy,null);
164                         //c.getGraphics().drawImage(img,fx,fy,null);
165                     }
166                 }
167                 */

168                 //u.p("finished loop");
169
}
170             c.getGraphics().setClip(oldclip);
171         }
172
173     }
174     
175     private static void tileFill(Graphics JavaDoc g, Image JavaDoc img, Rectangle JavaDoc rect, int xoff, int yoff, boolean horiz, boolean vert) {
176         int iwidth = img.getWidth(null);
177         int iheight = img.getHeight(null);
178         int rwidth = rect.width;
179         int rheight = rect.height;
180         
181         if(!horiz) {
182             rwidth = iwidth;
183         }
184         if(!vert) {
185             rheight = iheight;
186         }
187         
188         if(horiz) {
189             xoff = xoff%iwidth-iwidth;
190             rwidth += iwidth;
191         }
192         if(vert) {
193             yoff = yoff%iheight-iheight;
194             rheight += iheight;
195         }
196         
197         for(int i=0; i<rwidth; i+=iwidth) {
198             for(int j=0; j<rheight; j+=iheight) {
199                 g.drawImage(img, i+rect.x+xoff, j+rect.y+yoff, null);
200             }
201         }
202         
203     }
204     
205     
206     
207     
208 }
209
Popular Tags