Use Draw2D and SWT plotting in Java

Source: Internet
Author: User

Drawing with Java has always attracted the attention of developers. Traditionally, Java developers use java. awt. Graphics or Java 2D APIs for plotting. Some developers even use ready-made open-source toolboxes (such as JSci) for plotting. However, in many cases, your choice is limited to AWT or Swing. To minimize dependencies on third-party toolboxes, or to simplify the drawing basics, consider using Draw2D and writing your own code for plotting or plotting.

Lucky time to win Mobile Phone registration
2005 Samsung yepp summer digital Tourism


   Introduction to Draw2D

Draw2D is a lightweight Window widget system residing on SWT Composite. A Draw2D InstanceConsists of a SWT Composite, a lightweight system, and a graph of its content. GraphicsIs the building block of Draw2D. For details about the Draw2D API, refer to the Eclipse help file of the Draw2D Developer's Guide. Because this article is not intended to be a Draw2D tutorial, it is sufficient to understand the Draw2D API to help you draw on the SWT Canvas for simplicity. You can directly use standard images, such as Ellipse, Polyline, RectangleFigure, and Triangle, or expand them to create your own images. In addition, some container graphics, such as Panel, can act as the total container of all sub-graphics.

Draw2D has two important packages: org. eclipse. draw2d. geometry and org. eclipse. draw2d. graph. These two packages are used in this article. The org. eclipse. draw2d. geometry package has some useful classes, such as Rectangle, Point, and PointList, which are self-explanatory. Another package, org. eclipse. draw2d. graph, may not be used too much. This package provides important classes, such as DirectedGraph, Node, Edge, NodeList, and EdgeList, which help you create charts.

In this article, I will explain how to use Draw2D to write code and help you visualize your data graphically. I will start with a description of a technology that will be in a certain range of data values (for example, from 0 to 2048) scales proportionally to the equivalent data value in another range (for example, from 0 to 100 ). Then I will illustrate how to plot the X-Y Coordinate Map for any level, each level contains a set of data elements. After learning the concepts in this article, you can easily draw other types of charts, such as pie charts and bar charts.

   Specific Drawing Process


Step 1: What kind of image do you want to draw?

Obviously, you want to graphically depict data from the data source. Therefore, you need data that you want to visualize in graphs. For convenience, I used a simple function named dataGenerator to generate data instead of reading data from an XML file or some other data sources. This function uses a for (;) loop, and return the generated value in the form of an array list.
Listing 1. generating some data
  private ArrayList dataGenerator() {   double series1[] = new double[5];        for(int i=0; i<series1.length; i++)        series1[i] = (i*10) + 10; // a linear         series containing 10,20,30,40,50              double series2[] = new double[9];        series2[0] = 20; series2[1] = 150; series2[2] = 5;       series2[3] = 90; series2[4] = 35;  series2[5] = 20;                             series2[6] = 150; series2[7] = 5; series2[8] = 45;              double series3[] = new double[7];        for(int i=0; i<series3.length; i++)        series3[i] = (i*20) + 15;             seriesData.add(series1);       seriesData.add(series2);       seriesData.add(series3);                   return seriesData;  } 

Step 2: zoom technology-generate X and Y coordinates from the given data



Some new terms

FigureCanvas
FigureCanvas in Draw2D is an extension of SWT Canvas. FigureCanvas can contain Draw2D images.
Panel
Panel is a universal container image in Draw2D. It can contain subgraphs. You can add Multiple widgets to a Panel image and then provide the Panel image to FigureCanvas.
DirectedGraph
DirectedGraph is a 2-D graph with a limited number of nodes. Each Node is located in some points, and adjacent nodes are connected to each other through Edges.

To draw a point on a 2-D plane, you must find the X and Y coordinates of each point. The magic of plotting is that a given data value can be scaled proportionally from one range to another, that is, if a group of values such as {10, 20, 30} are given }, then you should be able to determine which points (X and Y coordinates) on the 2-D plane represent the 10, 20, and 30 data values.

Plotting is always performed according to a limited scaling ratio. In other words, you can draw any number of points in the same restricted area. Because the area is fixed, you can always find the span (length) of the X axis and the span (height) of the Y axis ). The spans of the X and Y axes are only part of the equation. The other part is to find out the range of data values, and calculate the coordinates of these values based on the equivalent values of each data value in the new range.

Returns X and Y coordinates.

X coordinate: X coordinate is the horizontal distance between a point and the origin. Calculate the number of elements and divide the span of the X axis NCIDR blocks, where, NIs the number of elements in a given set. In this way, you can calculate the horizontal coordinates of all vertices in a set. You can use this method to obtain the length of each segment. The first vertex in the set is within the first distance equal to the segment length. Each subsequent vertex is located within the distance of the segment length plus the distance from the origin point to the previous vertex.

For example, if a set {10, 20, 30, 40} is provided, you can immediately draw four vertices because the set contains four elements. Therefore, the span of the X axis should be divided into four equal segments, with the length of each segment = SPAN/4. Therefore, if the span of the X axis is 800, the segment length will be 800/4, that is, 200. The X coordinate of the first element (10) will be 200, and the X coordinate of the second element (20) will be 400, and so on.
List 2. Calculate the X coordinate
 private int[] getXCoordinates(ArrayList seriesData){  int xSpan = (int)GraFixConstants.xSpan;            int longestSeries = Utilities.getLongestSeries(seriesData);            int numSegments =             ((double[])seriesData.get(longestSeries)).length;                    int sectionWidth =             (int)xSpan / numSegments; //want to divide span of xAxis                     int xPositions[] =             new int[numSegments]; // will contain X-coordinate of all dots.  for(int i=0; i<numSegments; i++){   xPositions[i]=    (

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.