Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Topology Diagram based on Java Applet

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to draw a topology diagram based on Java Applet, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Use Java Applet technology to develop small applications, and finally deploy them to the web page, so that it can read node and link data from the database, and draw the topology diagram according to the data. The process of drawing image is realized by jgraph open source software.

Technical points:

1. Deploy the Java Applet application on the page and call the jgraph.jar package

Applet >

Put the web page file and AppletDemo.jar in the same directory here. Since you need to refer to a third-party jgraph.jar package, write jgraph.jar in the archive attribute.

2. Java Applet application accesses database

Due to the policy restrictions of the Java Applet application itself, by default the application can only access the host running the Java Applet application using the http protocol; to modify the policy, you need to add a digital signature, and the browser prompts you for security issues. Therefore, in this development, the popular method of using Java Applet to access Servlet is used to solve the problem of reading database.

Java Applet applications interact with Servlet, and you can pass serialization-enabled objects (Serializable) directly between them. On the servlet side, you only need to respond to the doGet event and return the processing result in the form of ObjectStream:

DefaultGraphCell [] alCells = org.nlsde.draw.data.CellHandler.getAllCells (); ObjectOutputStream out = new ObjectOutputStream (response. GetOutputStream ()); out.writeObject (alCells)

On the Java Applet side, the results of initiating data request and accepting data are as follows:

URL url = new URL (getCodeBase () + "/ servlet/GetAllCells"); URLConnection urlConn = url.openConnection (); InputStream in = urlConn.getInputStream (); ObjectInputStream result = new ObjectInputStream (in); Object obj = result.readObject (); DefaultGraphCell [] alCell = (DefaultGraphCell []) obj

The part that interacts with the database is encapsulated in another class CellHandler. )

3. The process of drawing topology diagram with jgraph.

In jgraph, there are concepts of vertice, edge, and port, and their relationship is as follows:

Vertices, connections, and ports are all considered GraphCell, but connections and ports have their own unique properties.

The process of drawing with jgraph is as follows:

Create a collection of vertices and specify the properties of each vertex

DefaultGraphCell c = new DefaultGraphCell (rs.getString ("MAIN_IP")); / / create a vertex object and specify the Label display text GraphConstants.setAutoSize (c.getAttributes (), true); / / set the vertex size to automatically scale DefaultPort p = new DefaultPort (); c.add (p); / / add a port for the vertex to connect the line

If you need to add user-defined attributes to vertices, you can use the AttributeMap provided by jgraph to create attribute key-value pairs, and * attach to vertices:

AttributeMap m = new AttributeMap (); m.applyValue ("MAIN IP", rs.getString ("MAIN_IP")); c.getAttributes () .applyMap (m)

Then create the connection and specify the start point and end point of the connection according to the rules:

DefaultEdge e = new DefaultEdge (); AttributeMap m = new AttributeMap (); m.applyValue ("OBJECT_INSTANCE", rs.getString ("OBJECT_INSTANCE")); m.applyValue ("SOURCE1_INSTANCE", rs.getString ("SOURCE1_INSTANCE")); m.applyValue ("SOURCE2_INSTANCE", rs.getString ("SOURCE2_INSTANCE")); m.applyValue ("TYPE", "LINK"); e.getAttributes () .applyMap (m)

When specifying the start and end of a connection, the method is used to compare whether SOURCE1_INSTANCE and SOURCE2_INSTANCE match the OBJECT_INSTANCE in the vertex. If so, the compared vertex is the start or end of the current connection:

For (int I = 0; I < alLink.length; iTunes +) {for (int j = 0; j < alCell.length) Get ("OBJECT_INSTANCE") .toString (). EqualsIgnoreCase (alLink.getAttributes (). Get ("SOURCE1_INSTANCE") .toString ()) {if [I] .setSource (alCell.getChildAt (0)) } if (alCell.getAttributes (). Get ("OBJECT_INSTANCE") .toString (). EqualsIgnoreCase (alLink[ I] .getAttributes (). Get ("SOURCE2_INSTANCE"). ToString ()) {alLink.setTarget (alCell.getChildAt (0)) }}}

Setting the start or end point requires a call to the setSource () or setTarget () method. Note that the parameter of this method is the port of the vertex, not the vertex itself, so the parameter form is GraphCell.getChildAt (0). Since only one port has been created for vertices in this development, only getChildAt (0) is valid.

4. Specify an icon for the vertex

To specify an icon for a vertex, you must first successfully load the icon file in the Java Applet application. Java Applet only supports image files in gif/png/jpg format. All other formats set the error of ImageIcon object Image Load Status=4, indicating that there is an error in Load, but it will not prompt a specific error.

Call other files in Java Applet, which should be encapsulated in the Java Applet application package and follow the Java specification to create the name of the standard package. In this development, the package name of the Java Applet application itself is org.nlsde.draw.applet, and the package name of the picture file that needs to be called is org.nlsde.draw.applet.ico. When calling the picture file, you only need to use the getCodeBase () method to get the path of the Java Applet application itself, plus ico to find the picture file:

URL u = AppletDemo.class.getResource ("ico/" + alCell.getAttributes (). Get ("OBJECT_ICON") .toString () + ".jpg"); ImageIcon icon = new ImageIcon (u); GraphConstants.setIcon (alCell.getAttributes (), icon)

After getting the ImageIcon object of the picture, you can easily set icons for vertices using the built-in methods of jgraph.

5. Define the database connection

The database connection is encapsulated in the DBHandler class in this development. In addition, SystemConfig.xml files are used to store information about connecting to the database. When the DBHandler class accesses the SystemConfig.xml file, you specify the URL path instead of the local path for the SystemConfig.xml file:

Private static final String _ configFile = "http://192.168.15.230:8080/draw/SystemConfig.xml";

This is because when Java Web App runs, the default starting path is $TOMCAT_HOME/bin, not the root directory of Web App itself, which is troublesome to call.

The database connection information stored in the SystemConfig.xml file includes the Java-Oracle connection string:

Jdbc:oracle:thin:@ (DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=192.168.101.32) (PORT=1521)) (ADDRESS= (PROTOCOL=TCP) (HOST=192.168.101.34) (PORT=1521)) (LOAD_BALANCE=yes) (CONNECT_DATA= (SERVER=DEDICATED) (SERVICE_NAME=nmstest) (FAILOVER_MODE= (TYPE=SELECT) (METHOD=BASIC) (RETRIES=180) (DELAY=5)

This is because load balancing is configured on the Oracle side, and the server where the website is located does not have a client with Oracle 10g installed, and the database connection string cannot be resolved using the service name, so write the connection string with the service name nmstest directly here.

The above is what it is like to draw a topology diagram based on Java Applet. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report