Table of Contents
The following listing contains the complete example the code snippets of section Composites in chapter An example arrangement where taken from.
Example G.1. Complete Layout Example
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.tensegrity.composite.Composite;
import com.tensegrity.composite.CompositeFactory;
import com.tensegrity.composite.CompositeView;
import com.tensegrity.composite.coordinate.CoordinateSystem;
import com.tensegrity.composite.coordinate.DefaultMutableScaleLinear;
import com.tensegrity.composite.coordinate.MutableCoordinateSystem;
import com.tensegrity.graphics.PaintFormat;
import com.tensegrity.graphics.device.AWTDevice;
import com.tensegrity.graphics.device.Device;
import com.tensegrity.graphics.primitive2D.Rect2D;
import com.tensegrity.graphics.render.DefaultRenderer;
import com.tensegrity.graphics.render.Renderer;
public class LayoutExample extends JFrame
{
public LayoutExample()
{
// Get a CompositeFactory instance
CompositeFactory factory = CompositeFactory.newInstance();
// Create a view with the given CoordinateSystem
CompositeView compositeView =
factory.newCompositeView();
// Setting up the first Composite
CoordinateSystem coordSystem1 =
new MutableCoordinateSystem(
new DefaultMutableScaleLinear(0, 2000),
new DefaultMutableScaleLinear(0, 2000));
Composite composite1 = factory.newComposite(coordSystem1);
// Add a simple Primitve to the Composite
Rect2D rect = new Rect2D(0, 0, 2000, 2000);
rect.setPaint(PaintFormat.DEFAULT_PAINT_FORMAT_BLACK);
composite1.addPrimitive(rect);
composite1.setBoundingBox(1000, 4000, 1000, 3500);
// Setting up the second Composite
CoordinateSystem coordSystem2 =
new MutableCoordinateSystem(
new DefaultMutableScaleLinear(0, 2000),
new DefaultMutableScaleLinear(0, 2000));
Composite composite2 = factory.newComposite(coordSystem2);
composite2.addPrimitive(new Rect2D(rect));
composite2.setBoundingBox(5000, 6500, 5000, 6500);
// Add the Composites to the View
compositeView.addComposite(composite1);
compositeView.addComposite(composite2);
// Add the CompositeViewPanel
CompositeViewPanel viewPanel =
new CompositeViewPanel(compositeView);
getContentPane().add("Center", viewPanel);
setSize(480, 480);
setVisible(true);
}
public static void main(String[] args)
{
new LayoutExample();
}
// ********************************************
// inner class to display a CompositeView
private class CompositeViewPanel extends JPanel
{
final static int OFF_X = 20, OFF_Y = 20;
private Renderer renderer = new DefaultRenderer();
private CompositeView compositeView;
CompositeViewPanel(CompositeView compositeView)
{
this.compositeView = compositeView;
compositeView.enableClipping(true);
doLayout();
}
public void doLayout()
{
super.doLayout();
Dimension dim = getSize();
compositeView.setBoundingBox(
OFF_X,
dim.width - 2 * OFF_X,
OFF_Y,
dim.height - 2 * OFF_Y);
}
public void paintComponent(Graphics gfx)
{
// draw a background
Dimension dim = getSize();
gfx.setColor(Color.lightGray);
gfx.fillRect(0, 0, dim.width, dim.height);
gfx.setColor(Color.white);
gfx.fillRect(
OFF_X, OFF_Y,
dim.width - 2 * OFF_X,
dim.height - 2 * OFF_Y);
gfx.setColor(Color.black);
gfx.drawRect(
OFF_X, OFF_Y,
dim.width - 2 * OFF_X,
dim.height - 2 * OFF_Y);
// draw inlying Composites
Device gfxDevice = AWTDevice.getDevice(gfx);
gfxDevice.initiate();
compositeView.draw(renderer, gfxDevice, null);
}
}
}
© 2004, 2005 Tensegrity Software GmbH