Graphical table class library created with Java

zhaozj2021-02-17  47

Graphical table class library created with Java

Yang Hengxian (yanghx@70345.com)

Keywords: Java UML TOGETHER

This article is designed with TOGETHER modeling a graphical composition similar to the table element in the web page. Mainly used for dynamic generation of graphics table. This article uses UML class diagram relationships and implements the Java implementation, all classes inherit the custom HTMLELEMENT class and can be easily expanded.

1. Introduction

This article discusses how to build a complex class library with modeling tools and Java. We use Java and Together to implement a graphical table class library, and how to generate a colorful graphical interface using this class library and support JSP.

2. Requires function description

a) We demand that this class library implements the function of the table element on the web page, which is used to display the corresponding data. So each table is an object, so we define the form of HTMLTABLE. To provide additional features We just inherited from HTMLTable to extend the extension.

b) Convenient to customize the design to define the various properties of each column. We also define columns into a class HTMLColumn. The properties of the extended column are only extended from the HTMLColumn.

c) We can define the properties of each unit, so we define every unit is a class. HTMLTableCell. The properties of the extended cells are only available from HTMLTableCell.

d) Each Tanle has several unit blocks (such as A, B) of the above diagrams. We define this unit block as a class HTMLTABLESECTION. Used to manage blocks in the table.

e) Each unit block consists of a consolidated unit with a series of lines, we will merge the unit defined as HTMLTableSectionCell, and it is similar to HTMLTableCell, so we export a common base class HTMLBaseTableCell in these 2 classes.

f) The row mentioned above is also an object, so we define it as an object htmltableerow.

When you want to customize the properties of one of the classes, just inherit the virtual function override. The class features the table in the web page with the class starting with HTML. The following demonstrates that the MV starting is inheritance to implement the form of the form above.

3. Use UML tools to classify design

We selected the model of Borland to TOGETHER. This class diagram framework is as follows:

The picture above is the various types of relationships drawn in Together.

l In order to closely connect all kinds of relationships, we designed a public parent HTMLEMEMT to provide some common functions. Provides the property M_Nhalign, which is the following horizontal alignment property m_nhalign, indicating that the content of the element is vertically m_nvalign, indicating that the contents of the elements are vertically parental class M_ParentTable, indicating which element of this element. Provide related virtual functions. getWidth () acquisition the width of the object.

l Here we use a lot of ARRAYLIST in Java to manage the collection of objects. During the use of all from HTMLELEMENT inheritance, we use mandatory object transformation, convert from the parent class to the subclass (because Java1.4 does not support generics).

l We use the function that implements a specific function to place it in a virtual function so that you can make custom functions with the least code of the inheritance.

Specific view of the source code provided.

4. Expand

The following focuses on how we expand this class library and implement the features we need. We will explain how to expand in an instance.

The simplest is how to customize attributes such as a single unit.

Unit custom

We inherit units HTMLTableCell

Overload DrawBorder () draws the border of the Cell. Class myTablecell Extends HTMLTableCell {

Protected void drawborder (java.awt.graphics2d g, int tent, int top, int width,

INT height) {

Color prev = g.getcolor ();

g.setColor (Color.Red);

G. DrawRect (Left, Top, Width, Height);

G.SetColor (Prev);

}

}

The operation is as follows

When we want to customize higher levels, we need to rewrite

Public Void Draw (Graphics2D G, POINT PT), he provides all the features of the unit. This way you have to write.

Customization:

Split in solid lines:

Public class myrow extends HTMLTableRow {

Public myrow () {

}

Protected Void Drawline (Graphics2D G, INT LEFT, INT TOP, INT Right) {

g.drawline (Left, Top, Right, TOP);

}

}

running result

Of course, because each class has a function to implement Draw (), you will use the providing parameters when you want to complete yourself.

5. use

About the use of this class, follow

Table is made up of a series of TableSecions, while TableSection is composed of a series of TableRow with a TableSectioncell, each tablerow consists of some column TableCell.

When you want to customize element properties, the principle of compliance is:

TableCell Priority> TableROW Priority> TableSECTION Priority> Table Priority> TableColumn priority.

Let's demonstrate how to use these classes:

Public class testclass {

Public static void main (string [] args) throws filenotfoundexception,

Imageformatexception, IOException {

Testframe frame = new testframe ();

Frame.setsize (700,600);

Frame.show ();

BufferedImage Image = (BufferedImage) frame.createImage (600, 400);

Graphics2D g = (graphics2d) image.getgraphics (); // g is going to paint on this

MVTable Table = new mvTable (); // Create a table object

Table.AddColumn (New HTMLTableColumn);

Table.AddColumn (New HTMLTableColumn);

Table.AddColumn (New HTMLTableColumn (150));

For (int i = 0; i <5; i ) {

Table.AddColumn (New HTMLTableColumn (60, i 2, 1, New Color (i * 25, i * 10 125, 255-i * 50)));

}

/ / The above is the attribute of the column of the form

HTMLTABLESECTION Sec = New HTMLTABLESECTION ();

// Create a TableSection

Htmlsectioncell Cell = New Htmlsectioncell (New Color (0x008080), "A"); Sec.CreateSecion (Cell);

HTMLTABLEROW ROW = New MvtableRow (3.0, color.cyan);

HTMLTableCell C = New HTMLTableCell ();

C.SETCAPTION ("1-1");

Row.Addcell (C);

C = New HTMLTableCell (Color.gray, "Custom Unit Background");

Row.Addcell (C);

For (int i = 0; i <5; i ) {

C = new htmltablecell ();

Row.Addcell (C);

}

Sec.com; // Add an HTMLTableRow object

Row = New MvtableRow (3.5, color.red);

C = New HTMLTableCell (New Color (0x00ccff), "1-2");

Row.Addcell (C);

FONT TEMP = New Font ("Song", Font.Bold, 14);

C = New HTMLTablecell (TEMP, NULL, "custom cell font");

Row.Addcell (C);

For (int i = 0; i <5; i ) {

C = new htmltablecell ();

Row.Addcell (C);

}

Sec.Addrow (row);

Table.AddSection (SEC);

// -----------------------

Sec = new htmltableesection ();

Cell = New HTMLSECTIONCELL (New Color (0x99ccff), "B");

Sec.createSecion (Cell);

Row = new mvtableRow (4.1, color.blue);

C = new htmltablecell ();

C.SETCAPTION ("2-1");

Row.Addcell (C);

C = new htmltablecell ();

C.SETCAPTION ("custom line high");

Row.Addcell (C);

For (int i = 0; i <5; i ) {

C = new htmltablecell ();

Row.Addcell (C);

}

Row.SetHeight (60);

Sec.Addrow (row);

Row = new mvtableRow (5.1, color.pink);

C = New HTMLTablecell (Color.Yellow, "2-2");

Row.Addcell (C);

C = new mytablecell ();

C.Setcaption ("Rewind Drawborder");

Row.Addcell (C);

For (int i = 0; i <5; i ) {

C = new htmltablecell ();

Row.Addcell (C);

}

Sec.Addrow (row);

Row = new myrow ();

C = New HTMLTablecell (Color.Yellow, "2-3");

Row.Addcell (C);

C = new htmltablecell ();

C.SETCAPTION ("Drawline");

Row.Addcell (c); for (int i = 0; i <5; i ) {

C = new htmltablecell ();

Row.Addcell (C);

}

Sec.Addrow (row);

Row = new htmltablerow ();

C = New HTMLTablecell (Color.Yellow, "2-3");

Row.Addcell (C);

C = new htmltablecell ();

C.SETCAPTION ("rewritten");

Row.Addcell (C);

For (int i = 0; i <5; i ) {

C = new htmltablecell ();

Row.Addcell (C);

}

Sec.Addrow (row);

Table.AddSection (SEC);

Table.AddInfo ("Inherited Cell", 3, 2);

Table.AddInfo ("Dow can also be customized", 5, 1);

Table.AddInfo ("Table can also", 6, 1);

Table.Addinfo ("Big Duel I don't invoke the base class function, completely rewritten", 7, 1);

Table.DRAW (g);

/ / =========================================================================================================================================================================================== =========

OutputStream out = new fileoutputStream ("c: //1.jpg");

JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT);

// encoder.setjpegencodeparam ();

Encoder.encode (image);

Out.close ();

}

}

6. summary

During this class library build process, we build relevant function class diagrams with Borland's Together and specify its relationship. We build in principles of key data and key function self-management, so that systems are more conducive to functionality with inheritance.

The younger brother is limited, if there is a deficiencies, please criticize, thank you.

Class library download address: Download source code

转载请注明原文地址:https://www.9cbs.com/read-30036.html

New Post(0)