home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Mix / Balls.java next >
Encoding:
Java Source  |  2002-09-06  |  11.5 KB  |  347 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Balls.java    1.22 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Mix;
  41.  
  42. import java.awt.*;
  43. import java.awt.image.*;
  44. import java.awt.event.*;
  45. import javax.swing.*;
  46. import java2d.AnimatingControlsSurface;
  47. import java2d.CustomControls;
  48.  
  49.  
  50.  
  51. /**
  52.  * Animated color bouncing balls with custom controls.
  53.  */
  54. public class Balls extends AnimatingControlsSurface {
  55.  
  56.     private static Color colors[] = 
  57.             { Color.red, Color.orange, Color.yellow, Color.green.darker(),
  58.               Color.blue, new Color(75, 00, 82), new Color(238,130,238) };
  59.     private long now, deltaT, lasttime;
  60.     private boolean active;
  61.     protected Ball balls[] = new Ball[colors.length];
  62.     protected boolean clearToggle;
  63.     protected JComboBox combo;
  64.  
  65.  
  66.     public Balls() {
  67.         setBackground(Color.white);
  68.         for (int i = 0; i < colors.length; i++) {
  69.             balls[i] = new Ball(colors[i], 30);
  70.         }
  71.         balls[0].isSelected = true;
  72.         balls[3].isSelected = true;
  73.         balls[4].isSelected = true;
  74.         balls[6].isSelected = true;
  75.         setControls(new Component[] { new DemoControls(this) });
  76.     }
  77.  
  78.  
  79.     public void reset(int w, int h) {
  80.         if (w > 400 && h > 100) {
  81.             combo.setSelectedIndex(5);
  82.         }
  83.     }
  84.  
  85.  
  86.     public void step(int w, int h) {
  87.         if (lasttime == 0) {
  88.             lasttime = System.currentTimeMillis();
  89.         }
  90.         now = System.currentTimeMillis();
  91.         deltaT = now - lasttime;
  92.         active = false;
  93.         for (int i = 0; i < balls.length; i++) {
  94.             if (balls[i] == null) {
  95.                 return;
  96.             }
  97.             balls[i].step(deltaT, w, h);
  98.             if (balls[i].Vy > .02 || -balls[i].Vy > .02 ||
  99.                     balls[i].y + balls[i].bsize < h) {
  100.                 active = true;
  101.             }
  102.         }
  103.         if (!active) {
  104.             for (int i = 0; i < balls.length; i++) {
  105.                 balls[i].Vx = (float)Math.random() / 4.0f - 0.125f;
  106.                 balls[i].Vy = -(float)Math.random() / 4.0f - 0.2f;
  107.             }
  108.             clearToggle = true;
  109.         }
  110.     }
  111.  
  112.  
  113.     public void render(int w, int h, Graphics2D g2) {
  114.         for (int i = 0; i < balls.length; i++) {
  115.             Ball b = balls[i];
  116.             if (b == null || b.imgs[b.index] == null || !b.isSelected) {
  117.                 continue;
  118.             }
  119.             g2.drawImage(b.imgs[b.index], (int) b.x, (int) b.y, this);
  120.         }
  121.         lasttime = now;
  122.     }
  123.  
  124.  
  125.     public static void main(String argv[]) {
  126.         createDemoFrame(new Balls());
  127.     }
  128.  
  129.  
  130.     static class Ball {
  131.     
  132.         public int bsize;
  133.         public float x, y;
  134.         public float Vx = 0.1f;
  135.         public float Vy = 0.05f;
  136.         public int nImgs = 5;
  137.         public BufferedImage imgs[];
  138.         public int index = (int) (Math.random() * (nImgs-1));
  139.     
  140.         private final float inelasticity = .96f;
  141.         private final float Ax = 0.0f;
  142.         private final float Ay = 0.0002f;
  143.         private final float Ar = 0.9f;
  144.         private final int UP = 0;
  145.         private final int DOWN = 1;
  146.         private int indexDirection = UP;
  147.         private boolean collision_x, collision_y;
  148.         private float jitter;
  149.         private Color color;
  150.         private boolean isSelected;
  151.     
  152.     
  153.         public Ball(Color color, int bsize) {
  154.             this.color = color;
  155.             makeImages(bsize);
  156.         }
  157.     
  158.     
  159.         public void makeImages(int bsize) {
  160.             this.bsize = bsize*2;
  161.             int R = bsize;
  162.             byte[] data = new byte[R * 2 * R * 2];
  163.             int maxr = 0;
  164.             for (int Y = 2 * R; --Y >= 0;) {
  165.                 int x0 = (int) (Math.sqrt(R * R - (Y - R) * (Y - R)) + 0.5);
  166.                 int p = Y * (R * 2) + R - x0;
  167.                 for (int X = -x0; X < x0; X++) {
  168.                     int x = X + 15;
  169.                     int y = Y - R + 15;
  170.                     int r = (int) (Math.sqrt(x * x + y * y) + 0.5);
  171.                     if (r > maxr) {
  172.                         maxr = r;
  173.                     }
  174.                     data[p++] = r <= 0 ? 1 : (byte) r;
  175.                 }
  176.             }
  177.     
  178.             imgs = new BufferedImage[nImgs];
  179.     
  180.             int bg = 255;
  181.             byte red[] = new byte[256];
  182.             red[0] = (byte) bg;
  183.             byte green[] = new byte[256];
  184.             green[0] = (byte) bg;
  185.             byte blue[] = new byte[256];
  186.             blue[0] = (byte) bg;
  187.     
  188.             for (int r = 0; r < imgs.length; r++) {
  189.                 float b = 0.5f + (float) ((r+1f)/imgs.length/2f);
  190.                 for (int i = maxr; i >= 1; --i) {
  191.                     float d = (float) i / maxr;
  192.                     red[i] = (byte) blend(blend(color.getRed(), 255, d), bg, b);
  193.                     green[i] = (byte) blend(blend(color.getGreen(), 255, d), bg, b);
  194.                     blue[i] = (byte) blend(blend(color.getBlue(), 255, d), bg, b);
  195.                 }
  196.                 IndexColorModel icm = new IndexColorModel(8, maxr + 1,
  197.                             red, green, blue, 0);
  198.                 DataBufferByte dbb = new DataBufferByte(data, data.length);
  199.                 int bandOffsets[] = {0};
  200.                 WritableRaster wr = Raster.createInterleavedRaster(dbb,
  201.                     R*2,R*2,R*2,1, bandOffsets,null);
  202.                 imgs[r] = new BufferedImage(icm, wr,icm.isAlphaPremultiplied(),null);
  203.             }
  204.         }
  205.     
  206.     
  207.         private final int blend(int fg, int bg, float fgfactor) {
  208.             return (int) (bg + (fg - bg) * fgfactor);
  209.         }
  210.     
  211.     
  212.         public void step(long deltaT, int w, int h) {
  213.             collision_x = false;
  214.             collision_y = false;
  215.     
  216.             jitter = (float) Math.random() * .01f - .005f;
  217.     
  218.             x += Vx * deltaT + (Ax / 2.0) * deltaT * deltaT;
  219.             y += Vy * deltaT + (Ay / 2.0) * deltaT * deltaT;
  220.             if (x <= 0.0f) {
  221.                 x = 0.0f;
  222.                 Vx = -Vx * inelasticity + jitter;
  223.                 collision_x = true;
  224.             }
  225.             if (x + bsize >= w) {
  226.                 x = w - bsize;
  227.                 Vx = -Vx * inelasticity + jitter;
  228.                 collision_x = true;
  229.             }
  230.             if (y <= 0) {
  231.                 y = 0;
  232.                 Vy = -Vy * inelasticity + jitter;
  233.                 collision_y = true;
  234.             }
  235.             if (y + bsize >= h) {
  236.                 y = h - bsize;
  237.                 Vx *= inelasticity;
  238.                 Vy = -Vy * inelasticity + jitter;
  239.                 collision_y = true;
  240.             }
  241.             Vy = Vy + Ay * deltaT;
  242.             Vx = Vx + Ax * deltaT;
  243.     
  244.             if (indexDirection == UP) {
  245.                 index++; 
  246.             }
  247.             if (indexDirection == DOWN) {
  248.                 --index; 
  249.             }
  250.             if (index+1 == nImgs) {
  251.                 indexDirection = DOWN;
  252.             }
  253.             if (index == 0) {
  254.                 indexDirection = UP;
  255.             }
  256.         }
  257.     }  // End class Ball
  258.  
  259.  
  260.  
  261.     class DemoControls extends CustomControls implements ActionListener {
  262.  
  263.         Balls demo;
  264.         JToolBar toolbar;
  265.  
  266.         public DemoControls(Balls demo) {
  267.             super(demo.name);
  268.             this.demo = demo;
  269.             setBackground(Color.gray);
  270.             add(toolbar = new JToolBar());
  271.             toolbar.setFloatable(false);
  272.             addTool("Clear", true);
  273.             addTool("R", demo.balls[0].isSelected);
  274.             addTool("O", demo.balls[1].isSelected);
  275.             addTool("Y", demo.balls[2].isSelected);
  276.             addTool("G", demo.balls[3].isSelected);
  277.             addTool("B", demo.balls[4].isSelected);
  278.             addTool("I", demo.balls[5].isSelected);
  279.             addTool("V", demo.balls[6].isSelected);
  280.             add(combo = new JComboBox());
  281.             combo.addItem("10");
  282.             combo.addItem("20");
  283.             combo.addItem("30");
  284.             combo.addItem("40");
  285.             combo.addItem("50");
  286.             combo.addItem("60");
  287.             combo.addItem("70");
  288.             combo.addItem("80");
  289.             combo.setSelectedIndex(2);
  290.             combo.addActionListener(this);
  291.         }
  292.  
  293.  
  294.         public void addTool(String str, boolean state) {
  295.             JButton b = (JButton) toolbar.add(new JButton(str));
  296.             b.setBackground(state ? Color.green : Color.lightGray);
  297.             b.setSelected(state);
  298.             b.addActionListener(this);
  299.         }
  300.  
  301.  
  302.         public void actionPerformed(ActionEvent e) {
  303.             if (e.getSource() instanceof JComboBox) {
  304.                 int size = Integer.parseInt((String) combo.getSelectedItem());
  305.                 for (int i = 0; i < demo.balls.length; i++) {
  306.                     demo.balls[i].makeImages(size);
  307.                 }
  308.                 return;
  309.             }
  310.             JButton b = (JButton) e.getSource();
  311.             b.setSelected(!b.isSelected());
  312.             b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  313.             if (b.getText().equals("Clear")) {
  314.                 demo.clearSurface = b.isSelected();
  315.             } else {
  316.                 int index = toolbar.getComponentIndex(b)-1;
  317.                 demo.balls[index].isSelected = b.isSelected();
  318.             }
  319.         }
  320.  
  321.  
  322.         public Dimension getPreferredSize() {
  323.             return new Dimension(200,37);
  324.         }
  325.  
  326.  
  327.         public void run() {
  328.             try { thread.sleep(999); } catch (Exception e) { return; }
  329.             Thread me = Thread.currentThread();
  330.             ((JButton) toolbar.getComponentAtIndex(2)).doClick();
  331.             while (thread == me) {
  332.                 try {
  333.                     thread.sleep(222);
  334.                 } catch (InterruptedException e) { return; }
  335.                 if (demo.clearToggle) {
  336.                     if (demo.clearSurface) {
  337.                         combo.setSelectedIndex((int) (Math.random()*5));
  338.                     }
  339.                     ((JButton) toolbar.getComponentAtIndex(0)).doClick();
  340.                     demo.clearToggle = false;
  341.                 }
  342.             }
  343.             thread = null;
  344.         }
  345.     } // End DemoControls
  346. } // End Balls
  347.