KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > html > Image


1 // ===========================================================================
2
// Copyright (c) 1996 Mort Bay Consulting Pty. Ltd. All rights reserved.
3
// $Id: Image.java,v 1.1 2004/03/23 13:59:49 laurent Exp $
4
// ---------------------------------------------------------------------------
5

6 package org.objectweb.jac.aspects.gui.web.html;
7
8 // Laurent Martelli <laurent@aopsys.com> - 21 mar. 2004:
9
//
10

11 import java.awt.Dimension JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import org.mortbay.util.Code;
16
17 /* ---------------------------------------------------------------- */
18 /** HTML Image Tag.
19  * @see org.mortbay.html.Block
20  * @version $Id: Image.java,v 1.1 2004/03/23 13:59:49 laurent Exp $
21  * @author Greg Wilkins
22 */

23 public class Image extends Tag
24 {
25     /* ------------------------------------------------------------ */
26     public Image(String JavaDoc src, String JavaDoc alt)
27     {
28         super("img");
29         attribute("src",src);
30         alt(alt);
31     }
32
33     /* ------------------------------------------------------------ */
34     public Image(String JavaDoc src, String JavaDoc alt, Dimension JavaDoc size)
35     {
36         super("img");
37         attribute("src",src);
38         alt(alt);
39         if (size!=null) {
40             width(size.width);
41             height(size.height);
42         }
43     }
44
45     /* ------------------------------------------------------------ */
46     /** Construct from GIF file.
47      */

48     /*
49     public Image(String dirname, String src)
50     {
51         super("img");
52         attribute("src",src);
53         setSizeFromGif(dirname,src);
54     }
55     */

56     
57     /* ------------------------------------------------------------ */
58     /** Construct from GIF file.
59      */

60     public Image(File JavaDoc gif)
61     {
62         super("img");
63         attribute("src",gif.getName());
64         setSizeFromGif(gif);
65     }
66
67     /* ------------------------------------------------------------ */
68     /* public Image(String src,int width, int height, int border)
69     {
70         this(src);
71         width(width);
72         height(height);
73         border(border);
74     }
75     */

76
77     /* ------------------------------------------------------------ */
78     public Image border(int b)
79     {
80         attribute("border",b);
81         return this;
82     }
83     
84     /* ------------------------------------------------------------ */
85     public Image alt(String JavaDoc alt)
86     {
87         attribute("alt",alt);
88         return this;
89     }
90     
91     /* ------------------------------------------------------------ */
92     /** Set the image size from the header of a GIF file.
93      * @param dirname The directory name, expected to be in OS format
94      * @param pathname The image path name relative to the directory.
95      * Expected to be in WWW format (i.e. with slashes)
96      * and will be converted to OS format.
97      */

98     public Image setSizeFromGif(String JavaDoc dirname,
99                                 String JavaDoc pathname)
100     {
101         String JavaDoc filename =dirname + pathname.replace('/',File.separatorChar);
102         return setSizeFromGif(filename);
103     }
104     
105     /* ------------------------------------------------------------ */
106     /** Set the image size from the header of a GIF file.
107      */

108     public Image setSizeFromGif(String JavaDoc filename)
109     {
110         return setSizeFromGif(new File JavaDoc(filename));
111     }
112     
113     /* ------------------------------------------------------------ */
114     /** Set the image size from the header of a GIF file.
115      */

116     public Image setSizeFromGif(File JavaDoc gif)
117     {
118         if (gif.canRead())
119         {
120             try{
121                 byte [] buf = new byte[10];
122                 FileInputStream JavaDoc in = new FileInputStream JavaDoc(gif);
123                 if (in.read(buf,0,10)==10)
124                 {
125                     Code.debug("Image "+gif.getName()+
126                                " is " +
127                                ((0x00ff&buf[7])*256+(0x00ff&buf[6])) +
128                                " x " +
129                                (((0x00ff&buf[9])*256+(0x00ff&buf[8]))));
130                     width((0x00ff&buf[7])*256+(0x00ff&buf[6]));
131                     height(((0x00ff&buf[9])*256+(0x00ff&buf[8])));
132                 }
133             }
134             catch (IOException JavaDoc e){
135                 Code.ignore(e);
136             }
137         }
138         
139         return this;
140     }
141     
142 }
143
Popular Tags