Embedded AWT components, Custom selection interaction, Dynamic annotations : Geometry « 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 » GeometryScreenshots 
Embedded AWT components, Custom selection interaction, Dynamic annotations
Embedded AWT components, Custom selection interaction, Dynamic annotations

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

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



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li> An actual game application
 * <li> Embedded AWT components
 * <li> Custom selection interaction
 * <li> Dynamic annotations
 * <li> Geomtry generation
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo19 extends JFrame
{
  public Demo19()
  {
    super ("G Graphics Library - Demo 19");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create the graphic canvas
    GWindow window = new GWindow (new Color (170180190));
    getContentPane().add (window.getCanvas(), BorderLayout.CENTER);
    
    // Create scene with default viewport and world extent settings
    GScene scene = new GScene (window);

    // Create the graphics object and add to the scene
    Simon  simon = new Simon();
    GSimon gSimon = new GSimon (simon);
    scene.add (gSimon);

    pack();
    setSize (new Dimension (500500));
    setVisible (true);

    window.startInteraction (gSimon);
  }


  
  /**
   * Defines the geometry and presentation for the sample
   * graphic object.
   */   
  private class GSimon extends GObject
    implements ActionListener, GInteraction
  {
    private Simon       simon_;
    private GSegment    background_;
    private GSegment[]  sector_;
    private GSegment    title_;
    private GSegment    score_;
    private GSegment    gameOver_;
    
    
    GSimon (Simon simon)
    {
      simon_ = simon;
      
      background_ = new GSegment();
      GStyle backgroundStyle = new GStyle();
      backgroundStyle.setForegroundColor (new Color (000));
      backgroundStyle.setBackgroundColor (new Color (000));
      background_.setStyle (backgroundStyle);
      addSegment (background_);

      sector_ = new GSegment[4];
      
      sector_[0new GSegment();
      GStyle redStyle = new GStyle();
      redStyle.setForegroundColor (new Color (20000));
      redStyle.setBackgroundColor (new Color (20000));
      sector_[0].setStyle (redStyle);
      addSegment (sector_[0]);

      sector_[1new GSegment();
      GStyle greenStyle = new GStyle();
      greenStyle.setForegroundColor (new Color (02000));
      greenStyle.setBackgroundColor (new Color (02000));
      sector_[1].setStyle (greenStyle);
      addSegment (sector_[1]);

      sector_[2new GSegment();
      GStyle yellowStyle = new GStyle();
      yellowStyle.setForegroundColor (new Color (2002000));
      yellowStyle.setBackgroundColor (new Color (2002000));
      sector_[2].setStyle (yellowStyle);
      addSegment (sector_[2]);

      sector_[3new GSegment();
      GStyle blueStyle = new GStyle();
      blueStyle.setForegroundColor (new Color (00200));
      blueStyle.setBackgroundColor (new Color (00200));
      sector_[3].setStyle (blueStyle);
      addSegment (sector_[3]);

      title_ = new GSegment();
      GStyle titleStyle = new GStyle();
      titleStyle.setForegroundColor (new Color (255255255));
      titleStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);            
      titleStyle.setFont (new Font ("Dialog", Font.BOLD, 36));
      title_.setStyle (titleStyle);
      title_.setText (new GText ("SIMON", GPosition.NORTH));
      JButton startButton = new JButton ("Start");
      startButton.addActionListener (this);
      title_.setComponent (new GComponent (startButton, GPosition.SOUTH));
      addSegment (title_);
      
      score_ = new GSegment();
      GStyle scoreStyle = new GStyle();
      scoreStyle.setForegroundColor (new Color (255255255));
      scoreStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);      
      scoreStyle.setFont (new Font ("Dialog", Font.BOLD, 48));
      score_.setStyle (scoreStyle);
      score_.setText (new GText ("", GPosition.MIDDLE));
      addSegment (score_);

      gameOver_ = new GSegment();
      GStyle gameOverStyle = new GStyle();
      gameOverStyle.setForegroundColor (new Color (1.0f0.0f0.0f0.8f));
      gameOverStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);      
      gameOverStyle.setFont (new Font ("Dialog", Font.BOLD, 18));
      gameOver_.setStyle (gameOverStyle);
      gameOver_.setText (new GText ("", GPosition.MIDDLE));
      addSegment (gameOver_);
    }


    public void actionPerformed (ActionEvent event)
    {
      gameOver_.getText().setText ("");
      score_.getText().setText ("");
      refresh();

      simon_.newGame();
      play (simon_.getSequence());
      getWindow().startInteraction (this);
    }


    public void event (GScene scene, int event, int x, int y)
    {
      if (event == GWindow.BUTTON1_UP) {
        GSegment segment = findSegment (x, y);
        int guess = -1;
        for (int i = 0; i < 4; i++)
          if (segment == sector_[i]) guess = i;

        if (guess == -1return;

        boolean isCorrect = simon_.guess (guess);
        if (isCorrect) {
          highlight (guess, 500);
          if (simon_.isDone()) {
            GText scoreText = score_.getText();
            scoreText.setText (Integer.toString (simon_.getSequence().length - 1));
            refresh();

            // Pause
            try {Thread.currentThread().sleep (500);catch (Exception e) {}

            play (simon_.getSequence());
          }
        }
        else {
          gameOver_.getText().setText ("GAME OVER");
          refresh();
          getWindow().stopInteraction();
        }
      }
    }

    

    private void highlight (int sectorNo, int nMillis)
    {
      GSegment sector = sector_[sectorNo];
      GStyle style = sector.getStyle();
      Color color = style.getBackgroundColor();
      Color highlight = color.brighter();
      style.setForegroundColor (highlight);
      style.setBackgroundColor (highlight);
      refresh();

      // Pause
      try {Thread.currentThread().sleep (500);catch (Exception e) {}
      
      style.setForegroundColor (color);
      style.setBackgroundColor (color);
      refresh();
    }
    
    
    private void play (int[] code)
    {
      for (int i = 0; i < code.length; i++)
        highlight (code[i]500);
    }
    
    
    private int[] createZone (int x0, int y0, int r0, int r1,
                              double angle0, double angle1)
    {
      int[] inner = Geometry.createSector (x0, y0, r0, angle0, angle1);      
      int[] outer = Geometry.createSector (x0, y0, r1, angle0, angle1);

      int n = outer.length + inner.length - 6;

      int[] zone = new int[n];
      int i = 0;
      for (int j = 0; j < inner.length - 4; j++)
        zone[i++= inner[j];

      for (int j = outer.length - 6; j >= 0; j-=2) {
        zone[i++= outer[j+0];
        zone[i++= outer[j+1];
      }

      zone[i++= zone[0];
      zone[i++= zone[1];

      return zone;
    }
    
    
    public void draw()
    {
      background_.setGeometry (Geometry.createCircle (250250215));

      sector_[0].setGeometry (createZone (250250801900.0, Math.PI / 2.0));
      sector_[0].translate (10, -10);

      sector_[1].setGeometry (createZone (25025080190, Math.PI / 2.0, Math.PI));
      sector_[1].translate (-10, -10);

      sector_[3].setGeometry (createZone (25025080190, Math.PI, 3.0 * Math.PI / 2.0));
      sector_[3].translate (-1010);

      sector_[2].setGeometry (createZone (250250801903.0 * Math.PI / 2.0, Math.PI * 2.0));
      sector_[2].translate (1010);

      title_.setGeometry (250240);
      score_.setGeometry (250300);
      gameOver_.setGeometry (250300);
    }
  }


  private class Simon
  {
    private int[]    sequence_;
    private int      current_;
    private boolean  isDone_;

    
    int[] getSequence()
    {
      return sequence_;
    }
    
    
    void newGame()
    {
      sequence_ = new int[0];
      add();
      isDone_ = false;
    }


    private void add()
    {
      int[] newSequence = new int[sequence_.length + 1];
      System.arraycopy (sequence_, 0, newSequence, 0, sequence_.length);
      sequence_ = newSequence;
      sequence_[sequence_.length - 1(intMath.round (Math.random() 3);
      current_ = 0;
    }
    

    boolean guess (int guess)
    {
      boolean isCorrect = sequence_[current_++== guess;
      isDone_ = current_ == sequence_.length;
      if (isCorrect && isDone_add();
      return isCorrect;
    }


    boolean isDone()
    {
      return isDone_;
    }
  }
  


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

           
       
G-EmbededAWT.zip( 224 k)
Related examples in the same category
1. Update World Extent GeometryUpdate World Extent Geometry
2. Gemometry GenerationGemometry Generation
3. Advaced geometry generation
4. Draw String and StarDraw String and Star
5. Draw Radiation: Annotation layout mechanism, Visibility settings, Custom linestyleDraw Radiation: Annotation layout mechanism, Visibility settings, Custom linestyle
ww___w___.j___av_a_2s_.com_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.