1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| import javax.swing.border.AbstractBorder; import javax.swing.ImageIcon; import java.awt.Insets;
public class PictureBorder extends AbstractBorder { private ImageIcon north; private ImageIcon south; private ImageIcon east; private ImageIcon west; private Insets insets = new Insets(25,25,25,25); private boolean topBottom = true; private boolean constrained; public PictureBorder(String str) { north = south = east = west = new ImageIcon(str); constrained = false; } public PictureBorder(String s, Insets i) { north = south = east = west = new ImageIcon(s); insets = i; constrained = true; } public PictureBorder(String n, String s, String e, String w) { north = new ImageIcon(n); south = new ImageIcon(s); east = new ImageIcon(e); west = new ImageIcon(w); constrained = false; } public PictureBorder(String n, String s, String e, String w, Insets i) { north = new ImageIcon(n); south = new ImageIcon(s); east = new ImageIcon(e); west = new ImageIcon(w); insets = i; constrained = true; } public void setDrawTopBottomLast(boolean b) { topBottom = b; } public void setConstrained(boolean b) { constrained = b; } public boolean drawsTopBottomLast() { return topBottom; } public boolean isConstrained() { return constrained; } public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { if (topBottom) { int hi = insets.top + insets.bottom; if (!constrained) { insets.top = (int) ((width+0.0)/north.getIconWidth() * north.getIconHeight()); insets.bottom = (int) ((width+0.0)/south.getIconWidth() * south.getIconHeight()); hi = insets.top + insets.bottom; insets.right = (int) ((height+0.0-hi)/east.getIconHeight() * east.getIconWidth()); insets.left = (int) ((height+0.0-hi)/west.getIconHeight() * west.getIconWidth()); } g.drawImage(east.getImage(),x+width-insets.right,y+insets.top,insets.right,height-hi,east.getImageObserver()); g.drawImage(west.getImage(),x,y+insets.top,insets.left,height-hi,west.getImageObserver()); g.drawImage(north.getImage(),x,y,width,insets.top,north.getImageObserver()); g.drawImage(south.getImage(),x,y+height-insets.bottom,width,insets.bottom,south.getImageObserver()); } else { int wi = insets.left + insets.right; if (!constrained) { insets.right = (int) ((height+0.0)/east.getIconHeight() * east.getIconWidth()); insets.left = (int) ((height+0.0)/west.getIconHeight() * west.getIconWidth()); wi = insets.left + insets.right; insets.top = (int) ((width+0.0-wi)/north.getIconWidth() * north.getIconHeight()); insets.bottom = (int) ((width+0.0-wi)/south.getIconWidth() * south.getIconHeight()); } g.drawImage(north.getImage(),x+insets.left,y,width-wi,insets.top,north.getImageObserver()); g.drawImage(south.getImage(),x+insets.left,y+height-insets.bottom,width-wi,insets.bottom,south.getImageObserver()); g.drawImage(east.getImage(),x+width-insets.right,y,insets.right,height,east.getImageObserver()); g.drawImage(west.getImage(),x,y,insets.left,height,west.getImageObserver()); } } public Insets getBorderInsets(Component c) { return insets; } } |