Embedded AWT components, Custom selection interaction, Dynamic annotations : Geometry « Advanced Graphics « Java

Home
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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Advanced Graphics » Geometry 




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:[email protected]">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.Geometry GenerationGeometry Generation
3.Advanced 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.