CGLIB

A Constraint-based Graphics Library for B-Prolog

(Version 6.5)

 

 

 

 

 

 

 

 

 

 

 

 

By Neng-Fa Zhou

Afany Software & CUNY

Copyright © Afany Software, 2000-2003

 

 


Preface

 

The widespread use of window systems has made a graphics package indispensable for any programming languages. Prolog is not an exception, although graphics is not part of the ISO standard. Some efforts have been made to introduce graphics programming into Prolog. One widely used approach is to adopt an external language interface with a language that supports graphics programming. For example, JIPL is an interface developed by KLS Inc. that bridges Prolog and Java bi-directionally. A Prolog system that adopts an interface like this will be able to access the graphics library of Java. This approach is not satisfactory for the following two reasons. First, you have to write code in two languages. This is especially daunting when there are interactions involved. Second, the graphics library in the adopted language, whether it is C, C++, Java, VB, or Tcl/Tk, is at such a low level that it does not match well with a high-level language like Prolog. Prolog, and CLP (Constraint Logic Programming) languages in general, should have a graphics library that provides high-level abstraction for specifying the states, layouts, and behaviors of graphical objects.

 

Motivated by the observations, we designed and implemented a high-level and constraint-based graphics library, called CGLIB, for B-Prolog. The library provides primitives on graphical objects and provides various kinds of constraints that facilitate the specification of layouts of graphical objects. The constraint solver of B-Prolog serves as a general-purpose layout manager that is significantly more flexible than the special-purpose layout managers used in Java and Tck/Tk. The library adopts a construct called action rules available in B-Prolog for programming interactions. An action rule consists of a pattern for agents, a pattern for events that can activate the agents, and an action the agents carry out when activated. Agents can communicate with each other through logical variables or global variables. Agents can behave concurrently and sequentially as well.

 

CGLIB enables you to use the Java graphics package without the need to write any code in Java. CGLIB, however, is not just another syntactical sugar for Java’s graphics package. It has a significantly higher abstraction level and is much easier to learn and use than Java’s graphics package. One of prominent features of CGLIB is the use of constraints in the specification of layouts.  In graphics languages such as PostScript and Tex, you have to specify every single detail about the graphical objects, including their sizes and positions. In languages such as Java and Tcl/Tk, the layout managers help determine the layouts of objects for certain applications, but the layout algorithms lack flexibility and are difficult to learn and use. The use of action rules to describe event handling is another feature of CGLIB. Action rules can be used to describe sophisticated and dynamic behaviors of graphical objects in a simple and straightforward manner.

 

CGLIB can be used in many areas such as drawing editors, interactive user interfaces, animation, information visualization, intelligent agents, and games. This manual describes the library and its usage. More example programs are available at www.probp.com.

 


Table of Contents

 

Preface. 0

Table of Contents.. 1

1. Getting Started.. 3

1.1 An illustrative example. 3

1.2 Installation.. 3

1.3 Start and quit B-Prolog with CGLIB.. 4

1.4 Open and close windows. 4

1.4 Draw graphics and clean windows. 5

2. Creating Objects.. 6

2.1 Attribute objects. 7

2.2 Windows. 7

2.3 Graphical objects. 9

2.4 Menu bars, menus and menu items. 19

2.5 File dialogs. 20

3. Constraints.. 22

3.1 Position constraints. 22

3.2 Same property constraints. 22

3.3 Grid and table constraints. 22

3.4 Tree constraint.. 24

3.5 Not overlap constraint.. 25

4. Operations on Objects.. 26

4.1 Packing and Showing Objects. 26

The primitive cgShow(O) shows O, and the primitive cgPack(O) packs O, where O can be one object or a list of objects. 26

4.2 Altering and Moving Objects. 26

5. Animation.. 28

6.    Generating Java Applets.. 29

7.    Event Handling.. 30

7.1 Action rules.