Custom Interaction : Interaction « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Advanced Graphics » InteractionScreenshots 
Custom Interaction
Custom Interaction

import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.awt.*;

import javax.swing.*;

import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li>A sample interactive game application
 * <li>Custom interaction
 * <li>Dynamic graphics highlighting
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo13 extends JFrame
  implements GInteraction
{
  private Reversi  reversi_;
  
  
  public Demo13 (int boardSize)
  {
    super ("G Graphics Library - Demo 13");
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create the GUI
    JPanel topLevel = new JPanel();
    topLevel.setLayout (new BorderLayout());
    getContentPane().add (topLevel);        

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

    // Create scene
    GScene scene = new GScene (window);
    double w0[] {0.0,              0.0,             0.0};
    double w1[] {boardSize + 2.0,  0.0,             0.0};
    double w2[] {0.0,              boardSize + 2.00.0};
    scene.setWorldExtent (w0, w1, w2);

    // Create the Reversi game and graphics representation
    reversi_ = new Reversi (boardSize);
    GObject reversiBoard = new ReversiBoard();
    scene.add (reversiBoard);
    
    pack();
    setSize (new Dimension (500500));
    setVisible (true);

    // Make sure plot can be scrolled
    window.startInteraction (this);
  }


  
  public void event (GScene scene, int event, int x, int y)
  {
    if (scene == nullreturn;

    GObject interaction = scene.find ("interaction");
    if (interaction == null) {
      interaction = new GObject ("interaction");
      scene.add (interaction);
    }

    interaction.removeSegments();

    double[] w = scene.getTransformer().deviceToWorld (x, y);

    int i = (intw[11;
    int j = (intw[01;

    if (i < || i >= reversi_.getSize()|| j < || j >= reversi_.getSize()) 
      return;

    switch (event) {
      case GWindow.MOTION     :
        if (reversi_.isLegalMove (i, j)) {
          GSegment highlight = new GSegment();
          GStyle highlightStyle  = new GStyle();
          highlightStyle.setBackgroundColor (new Color (1.0f1.0f1.0f0.7f));
          highlight.setStyle (highlightStyle);
          interaction.addSegment (highlight);

          highlight.setGeometryXy (new double[] {j + 1.0, i + 1.0,
                                                 j + 2.0, i + 1.0,
                                                 j + 2.0, i + 2.0,
                                                 j + 1.0, i + 2.0,
                                                 j + 1.0, i + 1.0});
        }
        break;
        
      case GWindow.BUTTON1_UP :
        if (reversi_.isLegalMove (i, j)) {
          reversi_.move (i, j);
          interaction.removeSegments();
          scene.redraw();
        }
    }
    
    scene.refresh();    
  }


  class ReversiBoard extends GObject
  {
    private GSegment    board_;
    private GSegment[]  grid_;
    private List        pieces_; // og GSegment
    private GStyle[]    pieceStyle_;
    
    

    public ReversiBoard()
    {
      board_ = new GSegment();
      GStyle boardStyle = new GStyle();
      boardStyle.setBackgroundColor (new Color (02000));
      board_.setStyle (boardStyle);
      addSegment (board_);

      GStyle gridStyle = new GStyle();
      gridStyle.setForegroundColor (new Color (000));
      gridStyle.setLineWidth (2);
      grid_ = new GSegment[(reversi_.getSize() 12];
      
      for (int i = 0; i < grid_.length; i++) {
        grid_[inew GSegment();
        grid_[i].setStyle (gridStyle);
        addSegment (grid_[i]);
      }

      pieceStyle_ = new GStyle[2];
      pieceStyle_[0new GStyle();
      pieceStyle_[0].setForegroundColor (new Color (255255255));
      pieceStyle_[0].setBackgroundColor (new Color (255255255));

      pieceStyle_[1new GStyle();
      pieceStyle_[1].setForegroundColor (new Color (000));
      pieceStyle_[1].setBackgroundColor (new Color (000));

      pieces_ = new ArrayList();
    }

    
    
    public void draw()
    {
      int size = reversi_.getSize();
      
      // Board
      board_.setGeometryXy (new double[] {1.01.0,
                                          size + 1.01.0,
                                          size + 1.0, size + 1.0,
                                          1.0, size + 1.0,
                                          1.01.0});

      // Grid lines
      for (int i = 0; i <= size; i++) {
        grid_[i*0].setGeometry (1.0, i + 1.0, size + 1.0, i + 1.0);
        grid_[i*1].setGeometry (i + 1.01.0, i + 1.0, size + 1.0);
      }

      // Pieces
      int[] state = reversi_.getState();
      int j = 0;
      for (int i = 0; i < state.length; i++) {
        if (state[i!= 0) {
          double y = i / size + 1.5;
          double x = i % size + 1.5;

          int[] xy = getTransformer().worldToDevice (x, y);

          GSegment piece;
          if (j < pieces_.size())
            piece = (GSegmentpieces_.get (j);
          else {
            piece = new GSegment();
            pieces_.add (piece);
            addSegment (piece);
          }

          j++;
          
          piece.setStyle (pieceStyle_[state[i1]);
          piece.setGeometry (Geometry.createCircle (xy[0], xy[1]15));
        }
      }
    }
  }
  


  class Reversi
  {
    private int    size_;
    private int[]  state_;
    private int    player_;

    
    public Reversi (int size)
    {
      size_ = size;
      
      state_ = new int[size_ * size_];
      for (int i = 0; i < state_.length; i++)
        state_[i0;

      state_[(size_ / 1* size_ + size_ / 11;
      state_[(size_ / 1* size_ + size_ / 2]     2;
      state_[(size_ / 2)     * size_ + size_ / 12;
      state_[(size_ / 2)     * size_ + size_ / 2]     1;

      player_ = 1;
    }


    public int getSize()
    {
      return size_;
    }
    

    public int[] getState()
    {
      return state_;
    }
    
    
    public boolean isLegalMove (int i, int j)
    {
      return state_[i*size_+j== && (win (i, j, player_)).length > 0;
    }

    
    public void move (int i, int j)
    {
      int[] win = win (i, j, player_);
      for (int s = 0; s < win.length; s++)
        state_[win[s]] = player_;
      state_[i*size_ + j= player_;

      player_ = player_ == 1;
    }

        
    private int[] win (int i, int j, int color)
    {
      List win = new ArrayList();
      win.addAll (win (i, j, color, +1,  0));
      win.addAll (win (i, j, color, +1, +1));
      win.addAll (win (i, j, color,  0, +1));
      win.addAll (win (i, j, color, -1, +1));
      win.addAll (win (i, j, color, -1,  0));
      win.addAll (win (i, j, color, -1, -1));
      win.addAll (win (i, j, color,  0, -1));
      win.addAll (win (i, j, color, +1, -1));

      int[] a = new int[win.size()];
      int s = 0;
      for (Iterator t = win.iterator(); t.hasNext(); s++)
        a[s((Integert.next()).intValue();

      return a;
    }
    

    private List win (int i, int j, int color, int dx, int dy)
    {
      List win = new ArrayList();
      
      while (true) {
        i += dx;
        j += dy;
        
        if (i < || i == size_ || j < || j == size_ ||
            state_[i*size_ + j== 0)
          return new ArrayList();

        else if (state_[i*size_ + j== color)
          return win;
        else if (state_[i*size_ + j!= color)
          win.add (new Integer (i*size_ + j));          
      }
    }
  }
  

  
  public static void main (String[] args)
  {
    int boardSize = 8;
    new Demo13 (boardSize);
  }
}

           
       
G-CustomInteraction.zip( 198 k)
Related examples in the same category
1. Custom Move InteractionCustom Move Interaction
2. Annotation, Interaction and Transparent Color ScaleAnnotation, Interaction and Transparent Color Scale
w___w__w___.___ja_v___a___2__s__.___c_o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.