KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > example > mouse > ViewportDragging


1 /*
2  * ViewportDragging.java
3  *
4  * Created on 10 August 2004, 00:48
5  */

6
7 package com.thoughtriver.open.vectorvisuals.example.mouse;
8
9 import java.awt.*;
10 import java.awt.geom.*;
11
12 import javax.swing.*;
13
14 import com.thoughtriver.open.vectorvisuals.*;
15 import com.thoughtriver.open.vectorvisuals.ui.*;
16
17 /**
18  * This small application illustrates how to create a Vector Visuals drawing
19  * area (a <CODE>VVDisplay</CODE>) and make it draggable. In other words, the
20  * entire viewing area can be shifted around by clicking and dragging the mouse.
21  *
22  * @author Brandon Franklin
23  * @version $Date: 2006/11/25 09:21:56 $
24  */

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

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

40     public void run() {
41         JFrame frame = new JFrame("Vector Visuals Viewport Dragging Example");
42         frame.setSize(640, 480);
43         frame.getContentPane().setLayout(new BorderLayout());
44         frame.getContentPane().add(vvDisplay.getViewPane(), BorderLayout.CENTER);
45         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
46
47         frame.setVisible(true);
48
49         // Set up the panel itself
50
vvDisplay.getViewPane().setBackground(Color.DARK_GRAY);
51         vvDisplay.setWorldViewScale(1);
52
53         // Create a nice red rectangle
54
Brush rectLineBrush1 = new Brush(Color.WHITE, null, null);
55         Brush rectFillBrush1 = new Brush(Color.RED, null, null);
56         VisualObject rectangle1 = new VisualObject(new Rectangle2D.Double(120, 150, 115, 40), rectLineBrush1, rectFillBrush1);
57
58         // Create a nice yellow rectangle
59
Brush rectLineBrush2 = new Brush(Color.BLACK, null, null);
60         Brush rectFillBrush2 = new Brush(Color.YELLOW, null, null);
61         VisualObject rectangle2 = new VisualObject(new Rectangle2D.Double(240, 75, 40, 90), rectLineBrush2, rectFillBrush2);
62
63         // Create a nice blue rectangle
64
Brush rectLineBrush3 = new Brush(Color.YELLOW, null, null);
65         Brush rectFillBrush3 = new Brush(Color.BLUE, null, null);
66         VisualObject rectangle3 = new VisualObject(new Rectangle2D.Double(380, 250, 60, 60), rectLineBrush3, rectFillBrush3);
67
68         // Create some descriptive text
69
Brush brush = new Brush(Color.YELLOW, null, null);
70         VisualTextObject textObj1 = new VisualTextObject("Click and Drag anywhere to move the view", null, null, brush);
71         VisualTextObject textObj2 = new VisualTextObject("Rotate your mouse wheel to zoom", null, null, brush);
72
73         // Set the location and size of the objects
74
AffineTransform transform = textObj1.getTransform();
75         transform.scale(2.0, 2.0);
76         transform.translate(15, 120);
77         textObj1.setTransform(transform);
78
79         transform = textObj2.getTransform();
80         transform.scale(2.0, 2.0);
81         transform.translate(100, 180);
82         textObj2.setTransform(transform);
83
84         // Display the objects on the VVPanel
85
vvDisplay.addObject(rectangle1);
86         vvDisplay.addObject(rectangle2);
87         vvDisplay.addObject(rectangle3);
88         vvDisplay.addObject(textObj1);
89         vvDisplay.addObject(textObj2);
90
91         // Add a specialized mouse listener
92
VVMouseDragListener listener = new VVMouseDragListener(vvDisplay);
93         vvDisplay.addVVMouseListener(listener);
94
95         // Ensure that the viewport itself is draggable and zoomable
96
listener.setViewportDraggable(true);
97         listener.setViewportZoomable(true);
98
99     }
100
101     /**
102      * Instantiates a <CODE>ViewportDragging</CODE> and executes it.
103      *
104      * @param args the command line arguments, ignored here
105      */

106     public static void main(final String JavaDoc[] args) {
107         Runnable JavaDoc example = new ViewportDragging();
108         example.run();
109     }
110
111 }
112
Popular Tags