Using draw2d and SWT drawings in Java

Source: Internet
Author: User
Tags range

Java drawings have always attracted the attention of developers. Traditionally, Java developers use the Java.awt.Graphics or Java 2D APIs for drawing. Some developers even use off-the-shelf open source toolkits, such as JSci, to draw. But most of the time, your choices are limited to AWT or Swing. To minimize reliance on third-party toolkits, or to simplify the drawing base, consider using draw2d, and write your own code for drawing or drawing.

draw2d Introduction

Draw2d is a lightweight widget system that resides on SWT Composite. A draw2d instance consists of an SWT composite, a lightweight system, and a graphic of its contents. A graphic is a draw2d building block. All the details about the draw2d API can be found in the Eclipse Help file in draw2d Developer ' s Guide. Because this article is not intended to be a tutorial on draw2d, for simplicity, it is sufficient to understand that the draw2d API can help you draw on SWT Canvas. You can use some standard graphics directly, such as Ellipse, Polyline, rectanglefigure, and triangle, or you can extend them to create your own graphics. In addition, some container graphics, such as a Panel, can act as a total container for all child graphics.

DRAW2D has two important packages: Org.eclipse.draw2d.geometry and Org.eclipse.draw2d.graph, and these two packages are used in this article. Org.eclipse.draw2d.geometry packages have some useful classes, such as Rectangle, point, and Pointlist, which are all self explanatory. Another package org.eclipse.draw2d.graph the developer may not be using too much. This package provides some important classes, such as Directedgraph, Node, Edge, NodeList, and Edgelist, which help create charts.

In this article, I'll explain how to write code using DRAW2D to help you visualize your data graphically. I'll start with a description of the technology that will scale to the equivalent data values (for example, from 0 to 100) in a range of data values, such as 0 to 2048, proportionally to another range. Then, I'll illustrate how to draw an X-y map of any series, each of which contains a set of data elements. After you've learned the concepts in this article, it's easy to draw other types of charts, such as Pie and bar charts.

The specific drawing process

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

Obviously, you want to graphically depict data from a data source. So, you need the data that you want to visualize in graphical form. For simplicity, I used the data generated by a simple function named DataGenerator, rather than reading from an XML file or some other data source, which used a for (;;) loop and returned the resulting value as an array list).

Listing 1. Generate 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;
}

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.