阅读:3583回复:8
一个Applet实例的问题
<P> 刚接触GIS,迷茫滴很!</P>
<P>一个网上的Applet实例。</P> <P>环境变量中,我将<BR>C:\j2sdk1.4.2\lib\tools.jar;<BR>C:\Tomcat 5.0\common\lib\servlet-api.jar;<BR>C:\Tomcat 5.\common\lib\mxj.jar;<BR>E:\gis\miutil.jar;<BR>E:\gis\mxj.jar;<BR>C:\Tomcat 5.0\common\lib\commons-logging.jar;.<BR>都放置在了classpath中<BR>程序在E:\gis下放置。机器上未安装MapXtreme For Java,只将mapxtreme471和mapviewer471文件夹放置在C:\Tomcat 5.0\webapps中。</P> <P>程序如下:</P> <P>import java.net.*;<BR>import javax.swing.*;<BR>import java.awt.*;<BR>import java.awt.event.*;<BR>import java.io.InputStream;<BR>import java.io.ByteArrayOutputStream;<BR>import java.util.List;</P> <P>import com.mapinfo.graphics.Rendition;<BR>import com.mapinfo.graphics.*;<BR>import com.mapinfo.graphics.RenditionImpl;<BR>import com.mapinfo.theme.OverrideTheme;<BR>import com.mapinfo.theme.ThemeList;<BR>import com.mapinfo.xmlprot.MapImageResponse;<BR>import com.mapinfo.mapxtreme.client.*;<BR>import com.mapinfo.util.Overlay;<BR>import com.mapinfo.mapj.*;<BR>import com.mapinfo.mapj.MapJ;<BR>import com.mapinfo.xmlprot.mxtj.*;<BR>import org.jdom.*;//该包存在于jdom.jar</P> <P>/**This class is an example of how to use the EncodedImageRenderer and Overlay classes<BR>* to draw animated gifs on a Map in an application.*/<BR>public class AnimatedApp extends JFrame{<BR> /** Image to draw map on. */<BR> public Image m_mapImage = null;<BR> <BR> /** Image to draw symbols. */<BR> public Image m_symbolImage = null;<BR> <BR> /** Map state. */<BR> public MapJ m_mapJ= null;<BR> <BR> /** Zoom buttons to change map. */<BR> public JButton inButton = new JButton("Zoom In");<BR> public JButton outButton = new JButton("Zoom Out");<BR> <BR> /** URL path to MDF. mdf文件的路径 */<BR> public String m_fileToLoad = "<a href="http://localhost:8080/mapdemo/maps/CHINA.mdf" target="_blank" >http://localhost:8080/mapdemo/maps/CHINA.mdf</A>";<BR> <BR> /** URL path to MXTJ Servlet. MapXtremeJava的路径 */<BR> public String m_mxtjURL = "<a href="http://localhost:8080/mapxtreme471/servlet/mapxtreme" target="_blank" >http://localhost:8080/mapxtreme471/servlet/mapxtreme</A>";<BR> <BR> /** URL path to symbol to draw. 绘制图形的路径 */<BR> public String m_symbolUrl ="<a href="http://localhost:8080/samples45/ColorStar.gif" target="_blank" >http://localhost:8080/samples45/ColorStar.gif</A>";<BR> <BR> /** List of Overlay objects to draw on map. */<BR> public List m_overlays = null;</P> <P> public AnimatedApp(){}<BR> </P> <P> public static void main (String args[]){<BR> AnimatedApp app = new AnimatedApp();<BR> app.initApp();<BR> System.out.println("456");<BR> }</P> <P><BR>/**Initializes the GUI and map.*/<BR> public void initApp(){<BR> try{<BR> this.setSize(700,500);<BR> <BR> InputStream isRemoteFile = null; <BR> m_mapJ = new MapJ(); <BR> URL u = new URL(m_fileToLoad); </P> <P> URLConnection conn = u.openConnection(); </P> <P> isRemoteFile = conn.getInputStream();<BR> <BR> // Load the map<BR> m_mapJ.loadMapDefinition(isRemoteFile);<BR> m_mapJ.loadMapDefinition(m_fileToLoad);<BR> <BR> //inner member class to draw the Map<BR> MapComponent map = new MapComponent(); <BR> map.setBackground(Color.blue); <BR> <BR> //this button will zoom in <BR> inButton.addActionListener(new java.awt.event.ActionListener() {<BR> public void actionPerformed (java.awt.event.ActionEvent e){<BR> try{<BR> m_mapJ.setZoom(m_mapJ.getZoom()/2.0);<BR> renderMap();<BR> }catch (Exception ex) {<BR> System.out.println("Exception in button "); ex.printStackTrace();<BR> }<BR> }<BR> });<BR> //this button will zoom out <BR> outButton.addActionListener(new java.awt.event.ActionListener() {<BR> public void actionPerformed (java.awt.event.ActionEvent e){<BR> try{<BR> m_mapJ.setZoom(m_mapJ.getZoom()*2.0);<BR> renderMap();<BR> }catch (Exception ex) {<BR> System.out.println("Exception in button "); ex.printStackTrace();<BR> }<BR> }<BR> });<BR> <BR> //load image to draw on map <BR> getImage();<BR> <BR> //apply Override theme with Overlays <BR> applyOverrideTheme();<BR> <BR> //render the initial Map <BR> renderMap();<BR> <BR> //build the GUI <BR> BorderLayout bl = new BorderLayout();<BR> getContentPane().setLayout(bl);<BR> getContentPane().add(inButton, bl.NORTH);<BR> getContentPane().add(outButton, bl.SOUTH);<BR> getContentPane().add(map,bl.CENTER);</P> <P> this.show();<BR> } catch (Exception e){<BR> System.out.println("Exception in init ");<BR> e.printStackTrace();<BR> }// end catch<BR> } // end of Init</P> <P><BR>/**Set the symbol Image.For OverrideTheme, this is only done once.*/<BR> public void getImage(){<BR> ByteArrayOutputStream baos = new ByteArrayOutputStream();<BR> try{<BR> URL url = new URL(m_symbolUrl);<BR> InputStream is = url.openStream(); <BR> byte [] ba = new byte[1];<BR> <BR> while ((is.read(ba,0,1))!=(-1)){<BR> baos.write(ba[0]); <BR> }<BR> <BR> // Image m_symbolImage m_symbolImage的变量类型Image<BR> m_symbolImage = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());<BR> MediaTracker tracker = new MediaTracker(this); </P> <P> tracker.addImage(m_symbolImage,1); </P> <P> tracker.waitForAll();<BR> }catch (Exception ex) {<BR> ex.printStackTrace();<BR> }<BR> }</P> <P><BR>/**Create an OverrideTheme on the 0th layer.sets the Rendition as an Overlay.*/<BR> public void applyOverrideTheme(){<BR> <BR> Layer l = m_mapJ.getLayers().elementAt(0); </P> <P> ThemeList tl = l.getThemeList();<BR> tl.removeAll(); // Removes all themes from the list.<BR> <BR> Rendition rendition = new RenditionImpl();</P> <P> // Sets the value for the specified rendition property. <BR> rendition.setValue(Rendition.SYMBOL_MODE, Rendition.SymbolMode.OVERLAY_IMAGE);</P> <P> // Specify the location of the Overlay image (animated gif in this example) to use<BR> rendition.setValue(Rendition.SYMBOL_URL, m_symbolUrl);</P> <P> // Create an OverrideTheme on the "World Capitals" layer using the above rendition<BR> OverrideTheme ovrTheme = new OverrideTheme(rendition, "AnimatedImages");<BR> <BR> l.getThemeList().add(ovrTheme);<BR> }<BR> </P> <P>/**render the Map using the EncodedImageRenderer 使用EncodedImageRenderer来渲染地图 */<BR> public void renderMap(){<BR> try{<BR> // Create the ImageRequestComposer<BR> ImageRequestComposer imageRC = ImageRequestComposer.create(<BR> m_mapJ,ImageRequestComposer.MAX_COLORS_TRUECOLOR, Color.blue, "image/gif");<BR> <BR> //render using EncodedImageRenderer creating a gif image<BR> EncodedImageRenderer renderer = new EncodedImageRenderer(m_mxtjURL);<BR> String mimeType = renderer.createMimeType("image/gif");<BR> ImageRequestComposer irc = ImageRequestComposer.create(<BR> m_mapJ,ImageRequestComposer.MAX_COLORS_TRUECOLOR, Color.blue, mimeType);</P> <P> renderer.render(irc);</P> <P> //get the map image bytes out of the response</P> <P> MapImageResponse response = renderer.getResponse();<BR> byte[] m_imageBytes = response.getEncodedImage();</P> <P> //create Image object from bytes<BR> Toolkit kit = Toolkit.getDefaultToolkit();<BR> m_mapImage = kit.createImage(m_imageBytes);<BR> MediaTracker tracker = new MediaTracker(this);<BR> tracker.addImage(m_mapImage,1);<BR> tracker.waitForAll();</P> <P> //get the Overlays from response<BR> m_overlays = response.getOverlayList();</P> <P> repaint();<BR> }catch (Exception ex) {<BR> System.out.println("Exception in renderMap "); ex.printStackTrace();<BR> }<BR> }</P> <P><BR>/**Inner class that paints the map image to a JComponent. 内类用JComponent绘制地图图像*/<BR> public class MapComponent extends JComponent{<BR> /**Overridden paint method to draw the map,then draw the Overlay images on top.<BR> <a href="mailt*@param" target="_blank" >*@param</A> Graphics the graphics object to draw to.*/<BR> public void paint (Graphics g){<BR> if (m_mapImage!=null){<BR> g.drawImage(m_mapImage, 0, 0, this );<BR> try{<BR> //put the symbol in the correct location<BR> for (int loop = 0; loop < m_overlays.size(); loop++){<BR> // get the Overlay object from the List, then get it's Point<BR> Overlay overlayImage = (Overlay) m_overlays.get(loop); </P> <P> com.mapinfo.util.DoublePoint point = overlayImage.getPoint();<BR> g.drawImage(m_symbolImage, (int)point.x, (int)point.y, this);<BR> }<BR> }catch (Exception e) {<BR> e.printStackTrace();<BR> }<BR> }else{<BR> System.out.println("paint: Image null ");<BR> }<BR> } //end paint<BR> } //end inner class</P> <P><BR>/**Overridden so we can exit when window is closed */<BR> protected void processWindowEvent(WindowEvent e){<BR> super.processWindowEvent(e);<BR> if (e.getID() == WindowEvent.WINDOW_CLOSING){<BR> System.exit(0);<BR> }<BR> } //processWindowEvent函数结束<BR>} //AnimatedApp类结束</P> <P>在编译时,这时很多和MapXtreme For Java有关的类都未找到,我又将提示未找到的类放置在E:\gis\com\mapinfo下(即我解压缩了miutil.jar和mxj.jar)<BR>再编译顺利通过,但出现过时的类的提示:<BR>Note: AnimatedApp.java uses or overrides a deprecated API.<BR>Note: Recompile with -deprecation for details.<BR>未在意,继续运行,运行时不断提出错误,有时是找不到类,有时是文件地址不正确。</P> <P>在一台LINUX系统的机器上,运行成功,GUI组件完全显示,但在组件中未能显示地图。</P> <P>程序中的<BR>/** URL path to symbol to draw. 绘制图形的路径 */<BR>public String m_symbolUrl ="<a href="http://localhost:8080/samples45/ColorStar.gif" target="_blank" >http://localhost:8080/samples45/ColorStar.gif</A>";<BR>我不太理解它的作用,C:\Tomcat 5.0\webapps中我未创建samples45文件夹以及ColorStar.gif。这个是不是输入的文件?如果想将让该程序输出本地硬盘上的地图显示于这个程序中,该怎样修改?</P> |
|
1楼#
发布于:2007-08-22 22:42
不明白
|
|
2楼#
发布于:2007-04-13 18:23
奋斗顶一下
|
|
3楼#
发布于:2007-04-10 19:57
<img src="images/post/smile/dvbbs/em03.gif" /><img src="images/post/smile/dvbbs/em03.gif" /><img src="images/post/smile/dvbbs/em03.gif" />
|
|
4楼#
发布于:2007-02-03 22:32
trace <FONT color=#ff0000>(帝国出品,必属精品)</FONT>
|
|
5楼#
发布于:2007-02-03 10:09
真的不明白!!!!!!!!
|
|
6楼#
发布于:2007-01-19 12:53
我也不明白阿
|
|
7楼#
发布于:2006-10-26 18:11
<img src="images/post/smile/dvbbs/em07.gif" />
|
|
8楼#
发布于:2006-10-23 18:58
<img src="images/post/smile/dvbbs/em01.gif" />
|
|