1 package org.joshy.html; 2 3 import org.joshy.html.box.BlockBox; 4 import org.joshy.html.box.Box; 5 import org.joshy.html.box.InlineBox; 6 import javax.swing.ImageIcon ; 7 import java.awt.Image ; 8 import java.awt.Rectangle ; 9 import org.w3c.dom.Element ; 10 import org.w3c.dom.Node ; 11 import java.net.URL ; 12 import org.joshy.u; 13 14 public class ImageLayout extends BoxLayout { 15 public Box createBox(Context c, Node node) { 17 BlockBox box = new BlockBox(); 18 box.node = node; 19 return box; 20 } 21 22 private Image getImage(Context c, Node node) { 23 if(node.getNodeType() != node.ELEMENT_NODE) { 24 return null; 25 } 26 String src = ((Element )node).getAttribute("src"); 27 Image img = null; 28 try { 29 img = ImageUtil.loadImage(c,src); 30 } catch (Exception ex) { 31 u.p(ex); 32 } 33 return img; 34 } 35 public Box layout(Context c, Element elem) { 36 BlockBox block = (BlockBox)createBox(c,elem); 37 39 Border border = getBorder(c, block); 40 Border padding = getPadding(c, block); 41 Border margin = getMargin(c, block); 42 43 44 Image img = getImage(c,elem); 45 if(img != null) { 47 block.width = img.getWidth(null); 48 block.height = img.getHeight(null); 49 } else { 50 block.width = 50; 51 block.height = 50; 52 } 53 62 block.width = margin.left + border.left + padding.left + block.width + 64 padding.right + border.right + margin.right; 65 block.height = margin.top + border.top + padding.top + block.height + 66 padding.bottom + border.bottom + margin.bottom; 67 block.x = c.getExtents().x; 70 block.y = c.getExtents().y; 71 return block; 74 } 75 76 77 118 119 public void paint(Context c, Box box) { 120 InlineBox block = (InlineBox)box; 121 124 Border border = getBorder(c,block); 126 Border padding = getPadding(c,block); 127 Border margin = getMargin(c, block); 128 129 int top_inset = margin.top + border.top + padding.top; 131 int left_inset = margin.left + border.left + padding.left; 132 133 c.getExtents().width = block.width; 135 136 paintBackground(c,block); 138 c.getGraphics().translate(left_inset,top_inset); 140 paintComponent(c,block); 142 c.getGraphics().translate(-left_inset,-top_inset); 143 paintBorder(c,block); 145 146 c.getExtents().y = c.getExtents().y + block.height; 148 } 149 150 public void paintComponent(Context c, Box box) { 151 Image img = getImage(c,box.node); 152 if(img != null) { 153 c.getGraphics().drawImage(img,box.x,box.y,null); 154 } 155 } 156 157 162 163 164 } 165 | Popular Tags |