KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Selection.java
3  *
4  * Created on 10 August 2004, 11:36
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 the objects on it selectable with
20  * the mouse.
21  *
22  * @author Brandon Franklin
23  * @version $Date: 2006/11/25 09:21:56 $
24  */

25 public class Selection 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>Selection</CODE>.
32      */

33     public Selection() {
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 Object Selection 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 textObj = new VisualTextObject("Click on any object to select it", null, null, brush);
71
72         // Set the location and size of the objects
73
AffineTransform transform = textObj.getTransform();
74         transform.scale(2.0, 2.0);
75         transform.translate(15, 120);
76         textObj.setTransform(transform);
77
78         // Display the objects on the VVPanel
79
vvDisplay.addObject(rectangle1);
80         vvDisplay.addObject(rectangle2);
81         vvDisplay.addObject(rectangle3);
82         vvDisplay.addObject(textObj);
83
84         // Add a mouse listener, which is required for selection to work
85
VVMouseListener listener = new VVMouseListener(vvDisplay);
86         vvDisplay.addVVMouseListener(listener);
87
88         // Add the selection listener
89
SelectionManager.getSharedInstance().addSelectionListener(new SelectionListener() {
90
91             final private Brush SELECTION_BRUSH = new Brush(Color.WHITE, null, null);
92
93             private Brush oldBrush = null;
94
95             public void selectionChanged(final SelectionEvent event) {
96                 VisualObject visObjSrc = (VisualObject) event.getSource();
97                 if (event.isSelection()) {
98                     oldBrush = visObjSrc.getFillBrush();
99                     visObjSrc.setFillBrush(SELECTION_BRUSH);
100                 }
101                 else {
102                     visObjSrc.setFillBrush(oldBrush);
103                 }
104                 vvDisplay.getViewPane().repaint();
105             }
106
107         });
108
109     }
110
111     /**
112      * Instantiates a <CODE>Selection</CODE> and executes it.
113      *
114      * @param args the command line arguments, ignored here
115      */

116     public static void main(final String JavaDoc[] args) {
117         Runnable JavaDoc example = new Selection();
118         example.run();
119     }
120
121 }
122
Popular Tags