Lines Intersection : Intersection « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Advanced Graphics » IntersectionScreenshots 
Lines Intersection
Lines Intersection

import java.util.Collection;
import java.util.Iterator;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li>Custom selection interaction
 * <li>Object detection features
 * <li>Style inheritance and manipulation
 * <li>Dynamic style setting
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo6 extends JFrame
  implements GInteraction, ActionListener
{
  private JButton     typeButton_;
  private GStyle      selectionStyle_;
  private GStyle      textStyle_;
  private GStyle      selectedTextStyle_;
  private GScene      scene_;
  private GObject     rubberBand_;
  private Collection  selection_;
  private int         x0_, y0_;
  
  
  /**
   * Class for creating the demo canvas and hande Swing events.
   */   
  public Demo6()
  {
    super ("G Graphics Library - Demo 6");
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    selectionStyle_ = new GStyle();
    selectionStyle_.setForegroundColor (new Color (255255150));
    selectionStyle_.setLineWidth (2);        

    selectedTextStyle_ = new GStyle();
    selectedTextStyle_.setForegroundColor (new Color (255255255));
    selectedTextStyle_.setFont (new Font ("Dialog", Font.BOLD, 14));

    selection_ = null;
      
    // Create the GUI
    JPanel topLevel = new JPanel();
    topLevel.setLayout (new BorderLayout());
    getContentPane().add (topLevel);        

    JPanel buttonPanel = new JPanel();
    buttonPanel.add (new JLabel ("Highlight lines "));

    typeButton_ = new JButton ("inside");
    typeButton_.addActionListener (this);
    buttonPanel.add (typeButton_);    

    buttonPanel.add (new JLabel (" rubberband"));
    topLevel.add (buttonPanel,   BorderLayout.NORTH);

    // Create the graphic canvas
    GWindow window = new GWindow();
    topLevel.add (window.getCanvas(), BorderLayout.CENTER);    

    // Create scene with default viewport and world extent settings
    scene_ = new GScene (window, "Scene");

    // Create som graphic objects
    GObject testObject = new TestObject (scene_, 40);
    scene_.add (testObject);

    rubberBand_ = new GObject ("Interaction");
    GStyle rubberBandStyle = new GStyle();
    rubberBandStyle.setBackgroundColor (new Color (1.0f0.0f0.0f0.2f));
    rubberBand_.setStyle (rubberBandStyle);
    scene_.add (rubberBand_);
    
    pack();
    setSize (new Dimension (500500));
    setVisible (true);

    window.startInteraction (this);
  }


  
  public void actionPerformed (ActionEvent event)
  {
    String text = typeButton_.getText();
    if (text.equals ("inside")) typeButton_.setText ("intersecting");
    else                        typeButton_.setText ("inside");
  }

  
  
  public void event (GScene scene, int event, int x, int y)
  {
    switch (event) {
      case GWindow.BUTTON1_DOWN :
        x0_ = x;
        y0_ = y;
        rubberBand_.addSegment (new GSegment());
        break;
        
      case GWindow.BUTTON1_UP :
        rubberBand_.removeSegments();
        
        // Undo visual selection of current selection
        if (selection_ != null) {
          for (Iterator i = selection_.iterator(); i.hasNext()) {
            GSegment line = (GSegmenti.next();
            GText text = line.getText();
            text.setStyle (textStyle_);
            line.setStyle (null);
          }
        }

        scene_.refresh();
        break;
        
      case GWindow.BUTTON1_DRAG :
        int[] xRubber = new int[] {x0_, x, x, x0_, x0_};
        int[] yRubber = new int[] {y0_, y0_, y, y, y0_};      

        GSegment rubberBand = rubberBand_.getSegment();
        rubberBand.setGeometry (xRubber, yRubber);

        // Undo visual selection of current selection
        if (selection_ != null) {
          for (Iterator i = selection_.iterator(); i.hasNext()) {
            GSegment line = (GSegmenti.next();
            GText text = line.getText();
            text.setStyle (textStyle_);
            line.setStyle (null);
          }
        }

        if (typeButton_.getText().equals ("inside"))
          selection_ = scene_.findSegmentsInside (Math.min (x0_, x),
                                                  Math.min (y0_, y),
                                                  Math.max (x0_, x),
                                                  Math.max (y0_, y));
        else
          selection_ = scene_.findSegments (Math.min (x0_, x),
                                            Math.min (y0_, y),
                                            Math.max (x0_, x),
                                            Math.max (y0_, y));

        // Remove rubber band from selection
        selection_.remove (rubberBand);
        
        // Set visual appaerance of new selection
        for (Iterator i = selection_.iterator(); i.hasNext()) {
          GSegment line = (GSegmenti.next();
          line.setStyle (selectionStyle_);
          GText text = line.getText();
          text.setStyle (selectedTextStyle_);
        }
          
        scene_.refresh();
        break;
    }
  }
  
  
  
  /**
   * Defines the geometry and presentation for a sample
   * graphic object.
   */   
  private class TestObject extends GObject
  {
    private GSegment[] lines_;
    
    TestObject (GScene scene, int nLines)
    {
      lines_ = new GSegment[nLines];

      // Add style to object itself so it is inherited by segments
      GStyle lineStyle = new GStyle();
      lineStyle.setForegroundColor (new Color (100100100));
      setStyle (lineStyle);

      // Text style is set on each text element
      textStyle_ = new GStyle();
      textStyle_.setForegroundColor (new Color (000));
      textStyle_.setFont (new Font ("Dialog", Font.BOLD, 14));
      
      for (int i = 0; i < nLines; i++) {
        lines_[inew GSegment();
        addSegment (lines_[i]);

        GText text = new GText (Integer.toString (i));
        text.setStyle (textStyle_);
        lines_[i].setText (text);
      }
    }
    

    
    public void draw()
    {
      // Viewport dimensions
      int width  = (intMath.round (getScene().getViewport().getWidth());
      int height = (intMath.round (getScene().getViewport().getHeight());

      for (int i = 0; i < lines_.length; i++) {
        int x0 = (intMath.round (width  * Math.random());
        int y0 = (intMath.round (height * Math.random());
        int x1 = (intMath.round (width  * Math.random());
        int y1 = (intMath.round (height * Math.random());                

        lines_[i].setGeometry (x0, y0, x1, y1);
      }
    }
  }
  


  public static void main (String[] args)
  {
    new Demo6();
  }
}

           
       
G-LinesIntersection.zip( 210 k)
Related examples in the same category
1. Dynamic Graphics HighlightingDynamic Graphics Highlighting
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.