Xsplinefun displays colorful moving splines in a window : Line « 2D Graphics GUI « 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 » 2D Graphics GUI » LineScreenshots 
Xsplinefun displays colorful moving splines in a window


/**
 * Xsplinefun displays colorful moving splines in a window.
 
 * Taken from xsplinefun, Distribution of 02may92, by Jef Poskanzer,
 * jef@netcom.com, jef@well.sf.ca.us
 
 * @copyright (C) 1992 by Jef Poskanzer
 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation. This software is provided "as is" without express or implied
 * warranty.
 
 * First step in converting to Java was to junk all the X Windows stuff; in the
 * process we lost all the customization (need to re-add with getopt?).
 */
public class XSplineFun {
  public static final int POINTS = 20;
  public static final int DEFAULT_MAX_COLORS = 20;

  /* Spline-fun smarts. */

  static int x[] new int [POINTS]
    static int y[]new int [POINTS];
         static int  dx[] new int [POINTS];
                static int dy[]new int [POINTS];
  static int nred, ngreen, nblue, dred, dgreen, dblue;
  static int color;
  static Color xcolors[] new Color[DEFAULT_MAX_COLORS];

  public static void main(String[] av) {
    Frame f = new Frame("Spline Fun");
    XSplineFun xf = new XSplineFun();
    xf.init_splines();
    f.add(xf);
    while(true)
      try {
        xf.move_splines();
        Thread.sleep(150);    // msec
      catch (Exception e) {
        System.out.println(e);
      }
  }

  static void
  init_splines()
  {
    int i;

    /* Initialize points. */
    for i = 0; i < POINTS; ++i )
    {
    x[i= random() % width;
    y[i= random() % height;
    dx[i= random() MAX_DELTA * - MAX_DELTA;
    if dx[i<= --dx[i];
    dy[i= random() MAX_DELTA * - MAX_DELTA;
    if dy[i<= --dy[i];
    }

    /* Initalize colors. */
    for color = 0; color < ncolors; ++color )
    {
    xcolors[color].red = xcolors[color].green = xcolors[color].blue = 0;
    xcolors[color].pixel = pixels[color];
    xcolors[color].flags = DoRed|DoGreen|DoBlue;
    }
    color = 0;
    nred = ngreen = nblue = 0;
    dred = random() MAX_COLOR_DELTA * - MAX_COLOR_DELTA;
    if dred <= --dred;
    dgreen = random() MAX_COLOR_DELTA * - MAX_COLOR_DELTA;
    if dgreen <= --dgreen;
    dblue = random() MAX_COLOR_DELTA * - MAX_COLOR_DELTA;
    if dblue <= --dblue;
  }

  static void
  rotate_colormap()
  {
    int t, i;

    if forwards )
    {
    t = xcolors[0].pixel;
    for i = 0; i < ncolors - 1; ++i )
      xcolors[i].pixel = xcolors[i + 1].pixel;
    xcolors[ncolors - 1].pixel = t;
    XStoreColors(display, cmap, xcolors, ncolors );
    }
    else if backwards )
    {
    t = xcolors[ncolors - 1].pixel;
    for i = ncolors - 1; i > 0; --i )
      xcolors[i].pixel = xcolors[i - 1].pixel;
    xcolors[0].pixel = t;
    XStoreColors(display, cmap, xcolors, ncolors );
    }
  }

  static void
  new_color()
  {
    int t;

    for ; ; )
    {
    t = (intnred + dred;
    if t >= && t < 65536 break;
    dred = random() MAX_COLOR_DELTA * - MAX_COLOR_DELTA;
    if dred <= --dred;
    }
    xcolors[color].red = nred = t;
    for ; ; )
    {
    t = (intngreen + dgreen;
    if t >= && t < 65536 break;
    dgreen = random() MAX_COLOR_DELTA * - MAX_COLOR_DELTA;
    if dgreen <= --dgreen;
    }
    xcolors[color].green = ngreen = t;
    for ; ; )
    {
    t = (intnblue + dblue;
    if t >= && t < 65536 break;
    dblue = random() MAX_COLOR_DELTA * - MAX_COLOR_DELTA;
    if dblue <= --dblue;
    }
    xcolors[color].blue = nblue = t;
    XStoreColor(display, cmap, &(xcolors[color]) );
    XSetForegrounddisplay, gc, xcolors[color].pixel );
    if ++color >= ncolors color -= ncolors;
  }

  static void
  move_splines()
  {
    int i, t, px, py, zx, zy, nx, ny;

    /* Rotate colormap if necessary. */
    rotate_colormap();

    /* Choose new color. */
    new_color();

    /* Backwards rotation requires two new colors each loop. */
    if backwards )
    new_color();

    /* Move the points. */
    for i = 0; i < POINTS; i++ )
    {
    for ; ; )
      {
      t = x[i+ dx[i];
      if t >= && t < width break;
      dx[i= random() MAX_DELTA * - MAX_DELTA;
      if dx[i<= --dx[i];
      }
    x[i= t;
    for ; ; )
      {
      t = y[i+ dy[i];
      if t >= && t < height break;
      dy[i= random() MAX_DELTA * - MAX_DELTA;
      if dy[i<= --dy[i];
      }
    y[i= t;
    }

    /* Draw the figure. */
    px = zx = x[0+ x[POINTS-1] ) 2;
    py = zy = y[0+ y[POINTS-1] ) 2;
    for i = 0; i < POINTS-1; ++i )
    {
    nx = x[i+1+ x[i] ) 2;
    ny = y[i+1+ y[i] ) 2;
    XDrawSpline(g, px, py, x[i], y[i], nx, ny );
    px = nx;
    py = ny;
    }
    XDrawSpline(g, px, py, x[POINTS-1], y[POINTS-1], zx, zy );
  }


  /* X spline routine. */

  int abs(x) {
    return x < ? -x : x;
  }

  static void
  XDrawSpline(Graphics g, int x0, y0, x1, y1, x2, y2) {
    register int xa, ya, xb, yb, xc, yc, xp, yp;

    xa = x0 + x1 2;
    ya = y0 + y1 2;
    xc = x1 + x2 2;
    yc = y1 + y2 2;
    xb = xa + xc 2;
    yb = ya + yc 2;

    xp = x0 + xb 2;
    yp = y0 + yb 2;
    if absxa - xp + absya - yp > SPLINE_THRESH )
    XDrawSplinedisplay, d, gc, x0, y0, xa, ya, xb, yb );
    else
    XDrawLinedisplay, d, gc, x0, y0, xb, yb );

    xp = x2 + xb 2;
    yp = y2 + yb 2;
    if absxc - xp + absyc - yp > SPLINE_THRESH )
    XDrawSplinedisplay, d, gc, xb, yb, xc, yc, x2, y2 );
    else
    XDrawLinedisplay, d, gc, xb, yb, x2, y2 );
  }
}


           
       
Related examples in the same category
1. Line StylesLine Styles
2. Program to draw gridsProgram to draw grids
w___w__w.___jav_a___2__s__.co___m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.