1 package org.joshy.html.painter; 2 3 import java.awt.Image ; 4 import java.awt.Graphics ; 5 import java.awt.Color ; 6 import java.awt.Rectangle ; 7 import java.awt.Shape ; 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 box = new Rectangle ( 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 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 yoff = c.canvas.getLocation().y; 36 c.graphics.setColor(Color.GREEN); 37 c.graphics.setClip(c.canvas.getVisibleRect()); 40 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 oldclip = c.getGraphics().getClip(); 65 c.getGraphics().setClip(left_insets, top_insets, back_width, back_height); 66 67 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 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 97 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 104 110 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 ( left_insets, top_insets, back_width, back_height ), 131 0, -yoff, horiz, vert); 132 149 } else { 150 153 tileFill(c.getGraphics(), block.background_image, 154 new Rectangle ( left_insets, top_insets, back_width, back_height ), 155 0, -yoff, horiz, vert); 156 168 } 170 c.getGraphics().setClip(oldclip); 171 } 172 173 } 174 175 private static void tileFill(Graphics g, Image img, Rectangle 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 |