< prev index next >

src/demo/share/jfc/J2Ddemo/java2d/demos/Mix/Balls.java

Print this page




  54 import java2d.AnimatingControlsSurface;
  55 import java2d.CustomControls;
  56 import javax.swing.AbstractButton;
  57 import javax.swing.JComboBox;
  58 import javax.swing.JToggleButton;
  59 import javax.swing.JToolBar;
  60 
  61 
  62 /**
  63  * Animated color bouncing balls with custom controls.
  64  */
  65 @SuppressWarnings("serial")
  66 public class Balls extends AnimatingControlsSurface {
  67 
  68     private static Color[] colors = { RED, ORANGE, YELLOW, GREEN.darker(), BLUE,
  69         new Color(75, 00, 82), new Color(238, 130, 238) };
  70     private long now, deltaT, lasttime;
  71     private boolean active;
  72     protected Ball[] balls = new Ball[colors.length];
  73     protected boolean clearToggle;
  74     protected JComboBox combo;
  75 
  76     public Balls() {
  77         setBackground(WHITE);
  78         for (int i = 0; i < colors.length; i++) {
  79             balls[i] = new Ball(colors[i], 30);
  80         }
  81         balls[0].isSelected = true;
  82         balls[3].isSelected = true;
  83         balls[4].isSelected = true;
  84         balls[6].isSelected = true;
  85         setControls(new Component[] { new DemoControls(this) });
  86     }
  87 
  88     @Override
  89     public void reset(int w, int h) {
  90         if (w > 400 && h > 100) {
  91             combo.setSelectedIndex(5);
  92         }
  93     }
  94 


 262 
 263     final class DemoControls extends CustomControls implements ActionListener {
 264 
 265         Balls demo;
 266         JToolBar toolbar;
 267 
 268         @SuppressWarnings("LeakingThisInConstructor")
 269         public DemoControls(Balls demo) {
 270             super(demo.name);
 271             this.demo = demo;
 272             add(toolbar = new JToolBar());
 273             toolbar.setFloatable(false);
 274             addTool("Clear", true);
 275             addTool("R", demo.balls[0].isSelected);
 276             addTool("O", demo.balls[1].isSelected);
 277             addTool("Y", demo.balls[2].isSelected);
 278             addTool("G", demo.balls[3].isSelected);
 279             addTool("B", demo.balls[4].isSelected);
 280             addTool("I", demo.balls[5].isSelected);
 281             addTool("V", demo.balls[6].isSelected);
 282             add(combo = new JComboBox());
 283             combo.addItem("10");
 284             combo.addItem("20");
 285             combo.addItem("30");
 286             combo.addItem("40");
 287             combo.addItem("50");
 288             combo.addItem("60");
 289             combo.addItem("70");
 290             combo.addItem("80");
 291             combo.setSelectedIndex(2);
 292             combo.addActionListener(this);
 293         }
 294 
 295         public void addTool(String str, boolean state) {
 296             JToggleButton b =
 297                     (JToggleButton) toolbar.add(new JToggleButton(str));
 298             b.setFocusPainted(false);
 299             b.setSelected(state);
 300             b.addActionListener(this);
 301             int width = b.getPreferredSize().width;
 302             Dimension prefSize = new Dimension(width, 21);




  54 import java2d.AnimatingControlsSurface;
  55 import java2d.CustomControls;
  56 import javax.swing.AbstractButton;
  57 import javax.swing.JComboBox;
  58 import javax.swing.JToggleButton;
  59 import javax.swing.JToolBar;
  60 
  61 
  62 /**
  63  * Animated color bouncing balls with custom controls.
  64  */
  65 @SuppressWarnings("serial")
  66 public class Balls extends AnimatingControlsSurface {
  67 
  68     private static Color[] colors = { RED, ORANGE, YELLOW, GREEN.darker(), BLUE,
  69         new Color(75, 00, 82), new Color(238, 130, 238) };
  70     private long now, deltaT, lasttime;
  71     private boolean active;
  72     protected Ball[] balls = new Ball[colors.length];
  73     protected boolean clearToggle;
  74     protected JComboBox<String> combo;
  75 
  76     public Balls() {
  77         setBackground(WHITE);
  78         for (int i = 0; i < colors.length; i++) {
  79             balls[i] = new Ball(colors[i], 30);
  80         }
  81         balls[0].isSelected = true;
  82         balls[3].isSelected = true;
  83         balls[4].isSelected = true;
  84         balls[6].isSelected = true;
  85         setControls(new Component[] { new DemoControls(this) });
  86     }
  87 
  88     @Override
  89     public void reset(int w, int h) {
  90         if (w > 400 && h > 100) {
  91             combo.setSelectedIndex(5);
  92         }
  93     }
  94 


 262 
 263     final class DemoControls extends CustomControls implements ActionListener {
 264 
 265         Balls demo;
 266         JToolBar toolbar;
 267 
 268         @SuppressWarnings("LeakingThisInConstructor")
 269         public DemoControls(Balls demo) {
 270             super(demo.name);
 271             this.demo = demo;
 272             add(toolbar = new JToolBar());
 273             toolbar.setFloatable(false);
 274             addTool("Clear", true);
 275             addTool("R", demo.balls[0].isSelected);
 276             addTool("O", demo.balls[1].isSelected);
 277             addTool("Y", demo.balls[2].isSelected);
 278             addTool("G", demo.balls[3].isSelected);
 279             addTool("B", demo.balls[4].isSelected);
 280             addTool("I", demo.balls[5].isSelected);
 281             addTool("V", demo.balls[6].isSelected);
 282             add(combo = new JComboBox<>());
 283             combo.addItem("10");
 284             combo.addItem("20");
 285             combo.addItem("30");
 286             combo.addItem("40");
 287             combo.addItem("50");
 288             combo.addItem("60");
 289             combo.addItem("70");
 290             combo.addItem("80");
 291             combo.setSelectedIndex(2);
 292             combo.addActionListener(this);
 293         }
 294 
 295         public void addTool(String str, boolean state) {
 296             JToggleButton b =
 297                     (JToggleButton) toolbar.add(new JToggleButton(str));
 298             b.setFocusPainted(false);
 299             b.setSelected(state);
 300             b.addActionListener(this);
 301             int width = b.getPreferredSize().width;
 302             Dimension prefSize = new Dimension(width, 21);


< prev index next >