KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > example > fundamental > SavingAsXML


1 /*
2  * SavingAsXML.java
3  *
4  * Created on 23 May 2005, 11:39
5  */

6
7 package com.thoughtriver.open.vectorvisuals.example.fundamental;
8
9 import java.awt.*;
10 import java.awt.geom.*;
11 import java.io.*;
12
13 import javax.swing.*;
14
15 import com.thoughtriver.open.vectorvisuals.*;
16 import com.thoughtriver.open.vectorvisuals.persistence.*;
17
18 /**
19  * This example illustrates how to encode a vector world as XML.
20  *
21  * @author Brandon Franklin
22  * @version $Date: 2006/11/25 09:35:06 $
23  */

24 public class SavingAsXML implements Runnable JavaDoc {
25
26     /** The <CODE>VVDisplay</CODE> used to display the test */
27     private VVDisplay vvDisplay = null;
28
29     /**
30      * Creates a new instance of <CODE>SavingAsXML</CODE>.
31      */

32     public SavingAsXML() {
33         vvDisplay = new VVDisplay(new VVPanel());
34     }
35
36     /**
37      * Starts the example.
38      */

39     public void run() {
40         JFrame frame = new JFrame("Vector Visuals Saving as XML Example");
41         frame.setSize(640, 480);
42         frame.getContentPane().setLayout(new BorderLayout());
43         frame.getContentPane().add(vvDisplay.getViewPane(), BorderLayout.CENTER);
44         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45
46         frame.setVisible(true);
47
48         // Set up the panel itself
49
vvDisplay.getViewPane().setBackground(Color.DARK_GRAY);
50         vvDisplay.setWorldViewScale(1);
51
52         // Create a nice red rectangle
53
Brush rectLineBrush = new Brush(Color.WHITE, null, null);
54         Brush rectFillBrush = new Brush(Color.RED, null, null);
55         VisualObject rectangle = new VisualObject(new Rectangle2D.Double(0, 0, 115, 40), rectLineBrush, rectFillBrush);
56
57         // Create a text object reading "Hello, Embedding!"
58
Brush textBrush = new Brush(Color.YELLOW, null, null);
59         VisualTextObject textObj = new VisualTextObject("Encoded as XML!", null, null, textBrush);
60
61         // Embed the text object into the rectangle object, so they'll move and
62
// scale together
63
rectangle.add(textObj);
64
65         // Set the location of the text inside the rectangle
66
AffineTransform transform = textObj.getTransform();
67         transform.translate(10, 25);
68         textObj.setTransform(transform);
69
70         // Move and rescale the rectangle
71
transform = rectangle.getTransform();
72         transform.scale(3.0, 3.0);
73         transform.translate(50, 45);
74         rectangle.setTransform(transform);
75
76         // Display the objects on the VVPanel
77
vvDisplay.addObject(rectangle);
78
79         // Encode the world as XML, printed to the console
80
PersistenceManager.getInstance().encode(new BufferedOutputStream(System.out), vvDisplay // Try
81
// this
82
// with
83
// various
84
// VisualObjects,
85
// too
86
);
87     }
88
89     /**
90      * Instantiates a <CODE>SavingAsXML</CODE> and executes it.
91      *
92      * @param args the command line arguments, ignored here
93      */

94     public static void main(final String JavaDoc[] args) {
95         Runnable JavaDoc example = new SavingAsXML();
96         example.run();
97     }
98
99 }
100
Popular Tags