1.  To run the Java2D demo :

2.  % java Java2Demo

3.  or -

4.  % appletviewer Java2Demo.html

5.  ------

6.  Introduction

7.  ------

8.  This Java2D demo consists of a set of demos housed in one GUI

9.  framework that uses a JTabbedPane. You can access different groups of

10.  demos by clicking the tabs at the top of the pane. There are demo

11.  groups for Arcs_Curves, Clipping, Colors, Composite, Fonts, Images,

12.  Lines, Mix, Paint, Paths and Transforms. On the right-hand side of the

13.  pane, the GUI framework features individual and global controls for

14.  changing graphics attributes. There's also a memory-usage monitor, and

15.  a monitor for tracking the performance, in frames per second, of

16.  animation demos.

17.  ------

18.  Tips on usage

19.  ------

20.  Click on one of the tabs at the top of the pane to select a demo group.

21.  When you select a group, a set of surfaces is displayed, each of which

22.  contains one of the group's demos. At the bottom of each surface is

23.  small toolbar for controlling the demo.

24.  If you click on a demo surface, that demo is laid out by itself. A

25.  new icon buttons will appear in the demo's toolbar one that enables you

26.  to create new instances of that demo's surface.

27.  To run the demo continuously without user interaction, select the

28.  Run Window item in the Options menu and press the run button in the

29.  new window that's displayed.

30.  Appletviewer and Hotjava throw security exceptions when attempting

31.  to print. To print run the demo as a stand alone application.

32.  ------

33.  NOTE about demo surfaces

34.  ------

35.  The demo groups are in separate packages with their class files stored

36.  in directories named according to the demo group name. This makes it

37.  possible for the demos to be loaded dynamically. All drawing demos extend

38.  the DemoSurface abstract class and implement the DemoSurface's drawDemo

39.  method. All animated demos implement the AnimatingContext interface.

40.  You can run the demos in stand-alone mode by issuing a command like

41.  this from the Java2D directory:

42.  java demos.Clipping.ClipAnim

43.  You can run the demos in groups by issuing a command like this from

44.  the Java2D directory:

45.  java DemoGroup Clipping

46.  To recompile a demo, issue this command from the Java2D directory:

47.  For win32 :

48.  javac demos\Clipping\ClipAnim.java -d .

49.  For solaris :

50.  javac demos/Clipping/ClipAnim.java -d .

51.  You can change a demo into a Canvas instead of a DemoSurface by making

52.  it extend Canvas rather than DemoSurface. You'll also need to change

53.  drawDemo(int w, int h, Graphics2D g2) to paint(Graphics g), and declare and

54.  intialize g2. For those demos that animate, the run(), start() and

55.  stop() methods will be needed to handle the thread, and you should

56.  create and draw into an off screen image for double buffering. As an

57.  example, the conversion for Curves.java (non-animated) would be:

58.  public class Curves extends DemoSurface {

59.  public void drawDemo(int w, int h, Graphics2D g2) {

60.  ...

61.  }

62.  }

63.  Becomes :

64.  public class Curves extends Canvas {

65.  public void paint(Graphics g) {

66.  Graphics2D g2 = (Graphics2D) g;

67.  Dimension d = getSize();

68.  ...

69.  }

70.  }

71.  ======

72.  For a Java2D API Overview & Tutorial :

73.  http://java.sun.com/products/jdk/1.2/docs/guide/2d/spec/j2d-bookTOC.doc.html

74.  For the latest version of the Java2D demo :

75.  http://java.sun.com/products/java-media/2D/index.html

76.  You may send comments via the alias,

77.  which is a one-way alias to Sun's Java 2D API developers, or via the

78.  alias, which is a public discussion list.

79.  /*

80.  * @(#)AnimatingContext.java 1.2 98/09/13

81.  *

82.  Copyright 1998 by Sun Microsystems, Inc.,

83.  901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

84.  All rights reserved.

85.  *

86.  This software is the confidential and proprietary information

87.  of Sun Microsystems, Inc. ("Confidential Information"). You

88.  shall not disclose such Confidential Information and shall use

89.  it only in accordance with the terms of the license agreement

90.  you entered into with Sun.

91.  */

92.  /**

93.  The interface for DemoSurface's that wish to animate.

94.  */

95.  public interface AnimatingContext {

96.  public void step(int w, int h);

97.  public void reset(int newwidth, int newheight);

98.  }

99.  /*

100.  * @(#)CloningFeature.java 1.12 98/09/04

101.  *

102.  Copyright 1998 by Sun Microsystems, Inc.,

103.  901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

104.  All rights reserved.

105.  *

106.  This software is the confidential and proprietary information

107.  of Sun Microsystems, Inc. ("Confidential Information"). You

108.  shall not disclose such Confidential Information and shall use

109.  it only in accordance with the terms of the license agreement

110.  you entered into with Sun.

111.  */

112.  import java.awt.Component;

113.  import java.awt.Color;

114.  import java.awt.Font;

115.  import java.awt.BorderLayout;

116.  import java.awt.Dimension;

117.  import java.awt.event.MouseEvent;

118.  import javax.swing.JPanel;

119.  import javax.swing.JScrollPane;

120.  import javax.swing.JTextArea;

121.  import javax.swing.border.EmptyBorder;

122.  import javax.swing.border.SoftBevelBorder;

123.  import javax.swing.border.CompoundBorder;

124.  /**

125.  Illustration of how to use the clone feature of the demo.

126.  */

127.  public class CloningFeature extends JPanel implements Runnable {

128.  private Thread thread;

129.  private JTextArea ta;

130.  public CloningFeature() {

131.  setLayout(new BorderLayout());

132.  EmptyBorder eb = new EmptyBorder(5,5,5,5);

133.  SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.RAISED);

134.  setBorder(new CompoundBorder(eb, sbb));

135.  ta = new JTextArea("Cloning Demonstrated\n\nClicking once on a demo\n");

136.  ta.setMinimumSize(new Dimension(300,500));

137.  JScrollPane scroller = new JScrollPane();

138.  scroller.getViewport().add(ta);

139.  ta.setFont(new Font("Dialog", Font.PLAIN, 14));

140.  ta.setForeground(Color.black);

141.  ta.setBackground(Color.lightGray);

142.  ta.setEditable(false);

143.  add("Center", scroller);

144.  start();

145.  }

146.  public void start() {

147.  thread = new Thread(this);

148.  thread.setPriority(Thread.MAX_PRIORITY);

149.  thread.setName("CloningFeature");

150.  thread.start();

151.  }

152.  public void stop() {

153.  thread = null;

154.  }

155.  private void sleep(long millis) {

156.  try {

157.  Thread.sleep(millis);

158.  } catch (Exception e) {}

159.  }

160.  public void run() {

161.  sleep(2000);

162.  DemoGroup dg = Java2Demo.group[Java2Demo.tabbedPane.getSelectedIndex()];

163.  DemoPanel dp = (DemoPanel) dg.getPanel().getComponent(0);

164.  if (dp.surface == null) {

165.  ta.append("Sorry your zeroth component is not a DemoSurface.");

166.  return;

167.  }

168.  dg.mouseClicked(new MouseEvent(dp.surface, MouseEvent.MOUSE_CLICKED, 0, 0, 10, 10, 1, false));

169.  sleep(4000);

170.  ta.append("Clicking the ToolBar double document button\n");

171.  sleep(4000);

172.  dp = (DemoPanel) dg.clonePanels[0].getComponent(0);

173.  if (dp.toolbar != null) {

174.  for (int i = 0; i < 3; i++) {

175.  ta.append(" Cloning\n");

176.  dp.toolbar.cloneB.doClick();

177.  sleep(4000);

178.  }

179.  }

180.  ta.append("Changing attributes \n");

181.  sleep(4000);

182.  Component cmps[] = dg.clonePanels[0].getComponents();

183.  for (int i = 0; i < cmps.length; i++) {

184.  dp = (DemoPanel) cmps[i];

185.  if (dp.toolbar == null)

186.  continue;

187.  switch (i) {

188.  case 0 : ta.append(" Changing AntiAliasing\n");

189.  dp.toolbar.aliasB.doClick();

190.  break;

191.  case 1 : ta.append(" Changing Composite & Texture\n");

192.  dp.toolbar.compositeB.doClick();

193.  dp.toolbar.textureB.doClick();

194.  break;

195.  case 2 : ta.append(" Changing Screen\n");

196.  dp.toolbar.imgTypeCombo.setSelectedIndex(4);

197.  break;

198.  case 3 : ta.append(" Removing a clone\n");

199.  dp.toolbar.cloneB.doClick();

200.  }

201.  sleep(4000);

202.  }

203.  ta.append("\nAll Done!");

204.  }

205.  }

206.  /*

207.  * @(#)Controls.java 1.16 98/09/21

208.  *

209.  Copyright 1998 by Sun Microsystems, Inc.,

210.  901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

211.  All rights reserved.

212.  *

213.  This software is the confidential and proprietary information

214.  of Sun Microsystems, Inc. ("Confidential Information"). You

215.  shall not disclose such Confidential Information and shall use

216.  it only in accordance with the terms of the license agreement

217.  you entered into with Sun.

218.  */

219.  import java.awt.Component;

220.  import java.awt.Dimension;

221.  import java.awt.GridBagLayout;

222.  import java.awt.GridBagConstraints;

223.  import java.awt.Font;

224.  import javax.swing.JPanel;

225.  import javax.swing.JCheckBox;

226.  import javax.swing.JComboBox;

227.  import java.awt.event.ItemListener;

228.  import java.awt.event.ItemEvent;

229.  import javax.swing.border.TitledBorder;

230.  import javax.swing.border.EtchedBorder;

231.  /**

232.  Global Controls panel for changing graphic attributes of

233.  the demo surface.

234.  */

235.  public class Controls extends JPanel implements ItemListener {

236.  public TextureChooser texturechooser;

237.  public JCheckBox aliasCB, renderCB, toolBarCB;

238.  public JCheckBox compositeCB, textureCB;

239.  public JCheckBox verboseCB;

240.  public JComboBox imgTypeCombo;

241.  private Font font = new Font("serif", Font.PLAIN, 10);

242.  public Controls() {

243.  setLayout(new GridBagLayout());

244.  setBorder(new TitledBorder(new EtchedBorder(), "Global Controls"));

245.  aliasCB = createCheckBox("Anti-Aliasing", true, 0);

246.  renderCB = createCheckBox("Rendering Quality", false, 1);

247.  textureCB = createCheckBox("Texture", false, 2);

248.  compositeCB = createCheckBox("AlphaComposite", false, 3);

249.  imgTypeCombo = new JComboBox();

250.  imgTypeCombo.setPreferredSize(new Dimension(120, 18));

251.  imgTypeCombo.setFont(font);

252.  imgTypeCombo.addItem("Auto Screen");

253.  imgTypeCombo.addItem("On Screen");

254.  imgTypeCombo.addItem("Off Screen");

255.  imgTypeCombo.addItem("INT_RGB");

256.  imgTypeCombo.addItem("INT_ARGB");

257.  imgTypeCombo.addItem("INT_ARGB_PRE");

258.  imgTypeCombo.addItem("INT_BGR");

259.  imgTypeCombo.addItem("3BYTE_BGR");

260.  imgTypeCombo.addItem("4BYTE_ABGR");

261.  imgTypeCombo.addItem("4BYTE_ABGR_PRE");

262.  imgTypeCombo.addItem("USHORT_565_RGB");

263.  imgTypeCombo.addItem("USHORT_555_RGB");

264.  imgTypeCombo.addItem("BYTE_GRAY");

265.  imgTypeCombo.addItem("USHORT_GRAY");

266.  imgTypeCombo.addItem("BYTE_BINARY");

267.  imgTypeCombo.setSelectedIndex(0);

268.  imgTypeCombo.addItemListener(this);

269.  Java2Demo.addToGridBag(this, imgTypeCombo, 0, 4, 1, 1, 0.0, 0.0);

270.  toolBarCB = createCheckBox("ToolBar", false, 5);

271.  verboseCB = createCheckBox("Verbose", false, 6);

272.  texturechooser = new TextureChooser(0);

273.  Java2Demo.addToGridBag(this, texturechooser, 0, 7, 1, 1, 1.0, 1.0);

274.  }

275.  private JCheckBox createCheckBox(String s, boolean b, int y) {

276.  JCheckBox cb = new JCheckBox(s, b);

277.  cb.setFont(font);

278.  cb.setHorizontalAlignment(JCheckBox.LEFT);

279.  cb.addItemListener(this);

280.  Java2Demo.addToGridBag(this, cb, 0, y, 1, 1, 1.0, 1.0);

281.  return cb;

282.  }

283.  public void itemStateChanged(ItemEvent e) {

284.  Java2Demo.group[Java2Demo.tabbedPane.getSelectedIndex()].setup(true);

285.  }

286.  public Dimension getMinimumSize() {

287.  return getPreferredSize();

288.  }

289.  public Dimension getMaximumSize() {

290.  return getPreferredSize();

291.  }

292.  public Dimension getPreferredSize() {

293.  return new Dimension(140,240);

294.  }

295.  }

296.  /*

297.  * @(#)CustomControls.java 1.2 98/09/13

298.  *

299.  Copyright 1998 by Sun Microsystems, Inc.,

300.  901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

301.  All rights reserved.

302.  *

303.  This software is the confidential and proprietary information

304.  of Sun Microsystems, Inc. ("Confidential Information"). You

305.  shall not disclose such Confidential Information and shall use

306.  it only in accordance with the terms of the license agreement

307.  you entered into with Sun.

308.  */

309.  /**

310.  The interface for DemoSurface's that wish to add custom

311.  controls to the DemoPanel.

312.  */

313.  public interface CustomControls {

314.  public String getCustomControlsConstraint();

315.  public java.awt.Component getCustomControls();

316.  }

317.  /*

318.  * @(#)DemoGroup.java 1.22 98/09/21

319.  *

320.  Copyright 1998 by Sun Microsystems, Inc.,

321.  901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

322.  All rights reserved.

323.  *

324.  This software is the confidential and proprietary information

325.  of Sun Microsystems, Inc. ("Confidential Information"). You

326.  shall not disclose such Confidential Information and shall use

327.  it only in accordance with the terms of the license agreement

328.  you entered into with Sun.

329.  */

330.  import java.awt.*;

331.  import java.awt.event.*;

332.  import javax.swing.*;

333.  import javax.swing.border.*;

334.  import javax.swing.event.ChangeEvent;

335.  import javax.swing.event.ChangeListener;

336.  import java.util.Vector;

337.  import java.net.URL;

338.  import java.io.File;

339.  /**

340.  DemoGroup handles multiple demos inside of a panel. Demos are loaded

341.  dynamically this is achieved by a directory listing of all the class

342.  files in a demo group directory.

343.  Demo groups can be loaded individually, for example :

344.  java DemoGroup Fonts

345.  Loads all the demos found in the demos/Fonts directory.

346.  */

347.  public class DemoGroup extends JPanel implements MouseListener, ChangeListener, ActionListener {

348.  private static Font font = new Font("serif", Font.PLAIN, 10);

349.  private static EmptyBorder emptyB = new EmptyBorder(5,5,5,5);

350.  private static BevelBorder bevelB = new BevelBorder(BevelBorder.LOWERED);

351.  private String groupName;

352.  public JPanel clonePanels[];

353.  public JTabbedPane tabbedPane;

354.  public DemoGroup(String name) {

355.  groupName = name;

356.  setLayout(new BorderLayout());

357.  JPanel p = new JPanel(new GridLayout(0,2));

358.  p.setBorder(new CompoundBorder(emptyB, bevelB));

359.  // Retrieve class names in a specified demos/* directory

360.  URL url = DemoGroup.class.getResource("demos/" + name);

361.  File dir = new File(url.getFile());

362.  if (dir != null & dir.isDirectory()) {

363.  String list[] = dir.list();

364.  Java2Demo.sort(list);

365.  Vector vector = new Vector();

366.  for (int i = 0; i < list.length; i++) {

367.  if (list[i].indexOf('$') == -1 & list[i].endsWith(".class")) {

368.  String s1 = list[i].substring(0,list[i].indexOf('.'));

369.  vector.add("demos." + name + "." + s1);

370.  }

371.  }

372.  if (vector.size()%2 == 1) {

373.  p.setLayout(new GridBagLayout());

374.  }

375.  for (int i = 0; i < vector.size(); i++) {

376.  DemoPanel dp = new DemoPanel((String) vector.elementAt(i));

377.  dp.setDemoBorder(p);

378.  if (dp.surface != null) {

379.  dp.surface.addMouseListener(this);

380.  dp.surface.setMonitor(Java2Demo.performancemonitor != null);

381.  }

382.  if (p.getLayout() instanceof GridBagLayout) {

383.  int x = p.getComponentCount() % 2;

384.  int y = p.getComponentCount() / 2;

385.  int w = i == vector.size()-1 ? 2 : 1;

386.  Java2Demo.addToGridBag(p,dp,x,y,w,1,1,1);

387.  } else {

388.  p.add(dp);

389.  }

390.  }

391.  } else {

392.  System.out.println("Couldn't find " + name);

393.  }

394.  add(p);

395.  }

396.  public void mouseClicked(MouseEvent e) {

397.  if (tabbedPane == null) {

398.  shutDown(getPanel());

399.  JPanel p = new JPanel(new BorderLayout());

400.  p.setBorder(new CompoundBorder(emptyB, bevelB));

401.  tabbedPane = new JTabbedPane();

402.  tabbedPane.setFont(font);

403.  JPanel tmpP = (JPanel) getComponent(0);

404.  tabbedPane.addTab(groupName, tmpP);

405.  clonePanels = new JPanel[tmpP.getComponentCount()];

406.  for (int i = 0; i < clonePanels.length; i++) {

407.  clonePanels[i] = new JPanel(new BorderLayout());

408.  DemoPanel dp = (DemoPanel) tmpP.getComponent(i);

409.  DemoPanel c = new DemoPanel(dp.className);

410.  c.setDemoBorder(clonePanels[i]);

411.  if (c.surface != null) {

412.  c.surface.setMonitor(Java2Demo.performancemonitor != null);

413.  c.toolbar.cloneB =

414.  c.toolbar.addTool("clone.gif","Clone the Surface",this);

415.  }

416.  clonePanels[i].add(c);

417.  String s = dp.className.substring(dp.className.indexOf('.')+1);

418.  tabbedPane.addTab(s, clonePanels[i]);

419.  }