diff options
Diffstat (limited to 'gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt')
4 files changed, 2859 insertions, 0 deletions
diff --git a/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/AicasGraphicsBenchmark.java b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/AicasGraphicsBenchmark.java new file mode 100644 index 000000000..5bb4ec48a --- /dev/null +++ b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/AicasGraphicsBenchmark.java @@ -0,0 +1,1018 @@ +/* AnimationApplet.java -- An example of an old-style AWT applet + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath examples. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. */ + +package gnu.classpath.examples.awt; + +import java.awt.BorderLayout; +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.Insets; +import java.awt.Label; +import java.awt.Panel; +import java.awt.Toolkit; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.net.URL; +import java.util.Iterator; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class AicasGraphicsBenchmark extends Panel +{ + /** + * Default number of test-iterations. + */ + private static final int DEFAULT_TEST_SIZE = 1000; + + /** + * Default screen size. + */ + private static final int DEFAULT_SCREEN_WIDTH = 320; + private static final int DEFAULT_SCREEN_HEIGHT = 240; + + /** + * AWT tests. + */ + private static final int AWTTEST_LINES = 1 << 0; + private static final int AWTTEST_RECT = 1 << 1; + private static final int AWTTEST_POLYLINE = 1 << 2; + private static final int AWTTEST_POLYGON = 1 << 3; + private static final int AWTTEST_ARC = 1 << 4; + private static final int AWTTEST_OVAL = 1 << 5; + private static final int AWTTEST_ROUNDRECT = 1 << 6; + private static final int AWTTEST_STRING = 1 << 7; + private static final int AWTTEST_TRANSPARENTIMAGE = 1 << 8; + private static final int AWTTEST_IMAGE = 1 << 9; + + private static final int AWTTEST_NONE = 0; + private static final int AWTTEST_ALL = AWTTEST_LINES + | AWTTEST_RECT + | AWTTEST_POLYLINE + | AWTTEST_POLYGON + | AWTTEST_ARC + | AWTTEST_OVAL + | AWTTEST_ROUNDRECT + | AWTTEST_STRING + | AWTTEST_TRANSPARENTIMAGE + | AWTTEST_IMAGE + ; + + int iterations = 1; + private int screenWidth = DEFAULT_SCREEN_WIDTH; + private int screenHeight = DEFAULT_SCREEN_HEIGHT; + boolean doubleBufferFlag = true; + private int awtTests = AWTTEST_ALL; + + private Label testLabel; + + private String testContext = ""; + + Logger logger = Logger.getLogger("AicasGraphicsBenchmark"); + + private Image pngTestImage; + private Image gifTestImage; + + private TestSet testSetMap = new TestSet(); + + public AicasGraphicsBenchmark() + { + pngTestImage = loadImage("../icons/aicas.png"); + gifTestImage = loadImage("../icons/palme.gif"); + + setLayout(new BorderLayout()); + testLabel = new Label(); + add(testLabel,BorderLayout.NORTH); + add(new GraphicsTest(),BorderLayout.CENTER); + } + + void setTestContext(String testName) + { + logger.logp(Level.INFO, "AicasGraphicsBenchmark", "recordTest", + "--- Starting new test context: " + testName); + testContext = testName; + testLabel.setText(testName); + } + + private void recordTest(String testName, long time) + { + logger.logp(Level.INFO, "AicasGraphicsBenchmark", "recordTest", + testContext + ": " + testName + " duration (ms): " + time); + TestRecorder recorder = testSetMap.getTest(testName); + if (recorder == null) + { + recorder = new TestRecorder(testName); + testSetMap.putTest(testName,recorder); + } + recorder.addRun(time); + } + + void printReport() + { + for (Iterator i = testSetMap.testIterator(); i.hasNext(); ) + { + TestRecorder recorder = testSetMap.getTest((String)i.next()); + System.out.println("TEST " + recorder.getTestName() + ": average " + + recorder.getAverage() + "ms [" + + recorder.getMinTime() + "-" + recorder.getMaxTime() + + "]"); + } + } + + public static void main(String[] args) + { + int awtTests; + int i; + boolean endOfOptionsFlag; + AicasGraphicsBenchmark speed= new AicasGraphicsBenchmark(); + + // Parse arguments. + i = 0; + endOfOptionsFlag = false; + awtTests = AWTTEST_NONE; + while (i < args.length) + { + if (!endOfOptionsFlag) + { + if (args[i].equals("--help") || args[i].equals("-help") + || args[i].equals("-h")) + { + System.out.println("Usage: AicasGraphicsBenchmark [<options>] [<test> ...]"); + System.out.println(""); + System.out.println("Options: -i|--iterations=<n|-1> - number of iterations (-1 is infinite)"); + System.out.println(" -w|--width=<n> - screen width; default "+DEFAULT_SCREEN_WIDTH); + System.out.println(" -h|--height=<n> - screen height; default "+DEFAULT_SCREEN_HEIGHT); + System.out.println(" -n|--noDoubleBuffer - disable double-buffering test"); + System.out.println(""); + System.out.println("Tests: line"); + System.out.println(" rect"); + System.out.println(" polyline"); + System.out.println(" polygon"); + System.out.println(" arc"); + System.out.println(" oval"); + System.out.println(" roundrect"); + System.out.println(" string"); + System.out.println(" transparentimage"); + System.out.println(" image"); + System.exit(1); + } + else if ((args[i].startsWith("-i=") + || args[i].startsWith("--iterations="))) + { + speed.iterations = + Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1)); + i += 1; + continue; + } + else if ((args[i].equals("-i") || args[i].equals("--iterations"))) + { + if ((i + 1) >= args.length) + { + System.err.println("ERROR: No argument given for option '" + + args[i] + "'!"); + System.exit(2); + } + speed.iterations = Integer.parseInt(args[i + 1]); + i += 2; + continue; + } + else if ((args[i].startsWith("-w=") + || args[i].startsWith("--width="))) + { + speed.screenWidth = + Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1)); + i += 1; + continue; + } + else if ((args[i].equals("-w") || args[i].equals("--width"))) + { + if ((i + 1) >= args.length) + { + System.err.println("ERROR: No argument given for option '" + + args[i] + "'!"); + System.exit(2); + } + speed.screenWidth = Integer.parseInt(args[i + 1]); + i += 2; + continue; + } + else if ((args[i].startsWith("-h=") + || args[i].startsWith("--height="))) + { + speed.screenHeight = + Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1)); + i+=1; + continue; + } + else if ((args[i].equals("-h") || args[i].equals("--height"))) + { + if ((i+1) >= args.length) + { + System.err.println("ERROR: No argument given for option '" + + args[i] + "'!"); + System.exit(2); + } + speed.screenHeight = Integer.parseInt(args[i + 1]); + i += 2; + continue; + } + else if ((args[i].equals("-n") + || args[i].equals("--noDoubleBuffer"))) + { + speed.doubleBufferFlag = false; + i += 1; + continue; + } + else if (args[i].equals("--")) + { + endOfOptionsFlag = true; + i += 1; + continue; + } + else if (args[i].startsWith("-")) + { + System.err.println("ERROR: Unknown option '" + args[i] + "'!"); + System.exit(2); + } + } + StringTokenizer tokenizer = new StringTokenizer(args[i], " +,"); + while (tokenizer.hasMoreTokens()) + { + String s = tokenizer.nextToken().toLowerCase(); + if (s.equals("line")) + awtTests |= AWTTEST_LINES; + else if (s.equals("rect")) + awtTests |= AWTTEST_RECT; + else if (s.equals("polyline")) + awtTests |= AWTTEST_POLYLINE; + else if (s.equals("polygon")) + awtTests |= AWTTEST_POLYGON; + else if (s.equals("arc")) + awtTests |= AWTTEST_ARC; + else if (s.equals("oval")) + awtTests |= AWTTEST_OVAL; + else if (s.equals("roundrect")) + awtTests |= AWTTEST_ROUNDRECT; + else if (s.equals("string")) + awtTests |= AWTTEST_STRING; + else if (s.equals("transparentimage")) + awtTests |= AWTTEST_TRANSPARENTIMAGE; + else if (s.equals("image")) + awtTests |= AWTTEST_IMAGE; + else + { + System.err.println("Unknown AWT test '" + s + "'!"); + System.exit(2); + } + } + i += 1; + } + if (awtTests != AWTTEST_NONE) + speed.awtTests = awtTests; + + // Create graphics. + final Frame frame = new Frame("AicasGraphicsBenchmark"); + + frame.addWindowListener(new WindowAdapter() + { + public void windowClosing(WindowEvent e) + { + frame.setVisible(false); + System.exit(0); + } + }); + + frame.add(speed,BorderLayout.CENTER); + frame.setSize(speed.screenWidth,speed.screenHeight); + frame.setVisible(true); + + // Insets are correctly set only after the native peer was created. + Insets insets = frame.getInsets(); + // The internal size of the frame should be 320x240. + frame.setSize(320 + insets.right + insets.left, + 240 + insets.top + insets.bottom); + } + + private Image loadImage(String imageName) + { + Image result = null; + logger.logp(Level.INFO, "AicasGraphicsBenchmark", "loadImage", + "Loading image: " + imageName); + URL url = getClass().getResource(imageName); + if (url != null) + { + result = Toolkit.getDefaultToolkit().getImage(url); + prepareImage(result, this); + } + else + { + logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "loadImage", + "Could not locate image resource in class path: " + + imageName); + } + return result; + } + + /** + * Executes the test methods. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + void runTestSet(Graphics g, Dimension size) + { + if ((awtTests & AWTTEST_LINES) != 0) + test_drawLine(g, size); + if ((awtTests & AWTTEST_RECT) != 0) + test_drawRect(g, size); + if ((awtTests & AWTTEST_RECT) != 0) + test_fillRect(g, size); + if ((awtTests & AWTTEST_POLYLINE) != 0) + test_drawPolyline(g, size); + if ((awtTests & AWTTEST_POLYGON) != 0) + test_drawPolygon(g, size); + if ((awtTests & AWTTEST_POLYGON) != 0) + test_fillPolygon(g,size); + if ((awtTests & AWTTEST_ARC) != 0) + test_drawArc(g,size); + if ((awtTests & AWTTEST_ARC) != 0) + test_fillArc(g,size); + if ((awtTests & AWTTEST_OVAL) != 0) + test_drawOval(g, size); + if ((awtTests & AWTTEST_OVAL) != 0) + test_fillOval(g, size); + if ((awtTests & AWTTEST_ROUNDRECT) != 0) + test_fillRoundRect(g, size); + if ((awtTests & AWTTEST_STRING) != 0) + test_drawString(g, size); + if ((awtTests & AWTTEST_TRANSPARENTIMAGE) != 0) + test_drawTransparentImage(g,size); + if ((awtTests & AWTTEST_IMAGE) != 0) + test_drawImage(g,size); + } + + /** + * Gets a new random Color. + * + * @returna new random Color + */ + private Color getNextColor() + { + return new Color((int) (Math.random() * 254) + 1, + (int) (Math.random() * 254) + 1, + (int) (Math.random() * 254) + 1); + } + + /** + * Draws random lines within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawLine(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i=0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width-minSize)); + int y1 = (int) (Math.random() * (size.height-minSize)); + int x2 = (int) (Math.random() * (size.width-minSize)); + int y2 = (int) (Math.random() * (size.height-minSize)); + g.drawLine(x1, y1, x2, y2); + } + long endTime = System.currentTimeMillis(); + recordTest("drawLine " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random rectangles within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawRect(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i=0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width-minSize)); + int y1 = (int) (Math.random() * (size.height-minSize)); + int x2 = (int) (Math.random() * (size.width-minSize)); + int y2 = (int) (Math.random() * (size.height-minSize)); + g.drawRect(x1, y1, x2, y2); + } + long endTime = System.currentTimeMillis(); + recordTest("drawRect " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random rectangles within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillRect(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize = 10; + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x1 = (int) (Math.random() * (size.width-minSize)); + int y1 = (int) (Math.random() * (size.height-minSize)); + int x2 = (int) (Math.random() * (size.width-minSize)); + int y2 = (int) (Math.random() * (size.height-minSize)); + g.fillRect(x1, y1, x2, y2); + } + long endTime = System.currentTimeMillis(); + recordTest("fillRect " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random polylines within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawPolyline(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + long startTime = System.currentTimeMillis(); + for (int i=0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int points = (int)(Math.random() * 6) + 3; + int[] x_coords = new int[points]; + int[] y_coords = new int[points]; + for (int j = 0; j < points; j+=1) + { + x_coords[j] = (int)(Math.random() * (size.width)); + y_coords[j] = (int)(Math.random() * (size.height)); + } + g.drawPolyline(x_coords,y_coords, points); + } + long endTime = System.currentTimeMillis(); + recordTest("drawPolyline " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random polygons within the given dimensions. + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawPolygon(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + long startTime = System.currentTimeMillis(); + for (int i=0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int points = (int) (Math.random() * 6) + 3; + int[] xcoords = new int[points]; + int[] ycoords = new int[points]; + for(int j = 0; j < points; j+=1) + { + xcoords[j] = (int) (Math.random() * (size.width)); + ycoords[j] = (int) (Math.random() * (size.height)); + } + g.drawPolygon(xcoords, ycoords, points); + } + long endTime = System.currentTimeMillis(); + recordTest("drawPolygon " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random filled polygons within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillPolygon(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + long startTime = System.currentTimeMillis(); + for (int i=0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int points = (int) (Math.random() * 6) + 3; + int[] xcoords = new int[points]; + int[] ycoords = new int[points]; + for (int j = 0; j < points; j+=1) + { + xcoords[j] = (int) (Math.random() * (size.width)); + ycoords[j] = (int) (Math.random() * (size.height)); + } + g.fillPolygon(xcoords, ycoords, points); + } + long endTime = System.currentTimeMillis(); + recordTest("fillPolygon " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random arcs within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawArc(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i=0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int) (Math.random() * (size.width - x - minSize) + minSize); + int height = (int) (Math.random() * (size.height - y - minSize) + minSize); + int startAngle = (int) (Math.random() * 360); + int arcAngle = (int) (Math.random() * 360 - startAngle); + g.drawArc(x, y, width, height, startAngle, arcAngle); + } + endTime = System.currentTimeMillis(); + recordTest("drawArc " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random filled arcs within the given dimensions. + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillArc(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int)(Math.random() * (size.width - x - minSize) + minSize); + int height = (int)(Math.random() * (size.height - y - minSize) + minSize); + int startAngle = (int)(Math.random() * 360); + int arcAngle = (int)(Math.random() * 360); + g.fillArc(x, y, width, height, startAngle, arcAngle); + + } + endTime = System.currentTimeMillis(); + recordTest("fillArc " + maxTests + " times", (endTime - startTime)); + } + + /** + * Draws random ovals within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawOval(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int)(Math.random() * (size.width - minSize + 1)); + int y = (int)(Math.random() * (size.height - minSize + 1)); + int width = (int)(Math.random() * (size.width - x - minSize) + minSize); + int height = (int)(Math.random() * (size.height - y - minSize) + minSize); + g.drawOval(x, y, Math.min(width, height), Math.min(width, height)); + } + endTime = System.currentTimeMillis(); + recordTest("drawOval " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random filled ovals within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillOval(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int) (Math.random() * (size.width - x - minSize) + minSize); + int height = (int) (Math.random() * (size.height - y - minSize) + minSize); + g.fillOval(x, y, width,height); + } + endTime = System.currentTimeMillis(); + recordTest("fillOval " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random filled rounded rectangles within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_fillRoundRect(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + int minSize; + long startTime; + long endTime; + minSize = 10; + startTime = System.currentTimeMillis(); + for (int i=0; i < maxTests; i+=1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - minSize + 1)); + int y = (int) (Math.random() * (size.height - minSize + 1)); + int width = (int) (Math.random() * (size.width - x - minSize) + minSize); + int height = (int) (Math.random() * (size.height - y - minSize) + minSize); + int arcWidth = (int) (Math.random() * (width - 1) + 1); + int arcHeight = (int) (Math.random() * (height - 1) + 5); + g.fillRoundRect(x, y, width, height, arcWidth, arcHeight); + } + endTime = System.currentTimeMillis(); + recordTest("fillRoundRect " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random images within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawImage(Graphics g, Dimension size) + { + if (gifTestImage == null) + { + logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "runTestSet", + "Skipping 'test_drawImage' due to missing resource."); + return; + } + + int maxTests = DEFAULT_TEST_SIZE / 2; + if(maxTests == 0) + maxTests = 1; + int imageWidth = gifTestImage.getWidth(this); + int imageHeight = gifTestImage.getHeight(this); + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - imageWidth + 1)); + int y = (int) (Math.random() * (size.height - imageHeight + 1)); + g.drawImage(gifTestImage, x, y, this); + } + long endTime = System.currentTimeMillis(); + recordTest("drawImage " + maxTests + " times", (endTime-startTime)); + } + + /** + * Draws random transparent images within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawTransparentImage(Graphics g, Dimension size) + { + if (pngTestImage == null) + { + logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "runTestSet", + "Skipping 'test_drawTransparentImage' due to missing resource."); + return; + } + + + int maxTests = DEFAULT_TEST_SIZE / 5; + if(maxTests == 0) + maxTests = 1; + int imageWidth = pngTestImage.getWidth(this); + int imageHeight = pngTestImage.getHeight(this); + long startTime = System.currentTimeMillis(); + for (int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + int x = (int) (Math.random() * (size.width - imageWidth + 1)); + int y = (int) (Math.random() * (size.height - imageHeight + 1)); + g.drawImage(pngTestImage, x, y, this); + } + long endTime = System.currentTimeMillis(); + recordTest("draw transparent image " + maxTests + " times", + (endTime-startTime)); + } + + /** + * Draws random strings within the given dimensions. + * + * @param g The Graphics object that is used to paint. + * @param size The size of the canvas. + */ + private void test_drawString(Graphics g, Dimension size) + { + int maxTests = DEFAULT_TEST_SIZE; + String testString = "HelloWorld"; + int stringWidth = g.getFontMetrics().stringWidth(testString); + int stringHeight = g.getFontMetrics().getHeight(); + + long startTime = System.currentTimeMillis(); + for(int i = 0; i < maxTests; i += 1) + { + g.setColor(getNextColor()); + g.drawString(testString, (int) (Math.random() * (size.width - stringWidth + 1)),(int)(Math.random() * (size.height - stringHeight + 1)) + stringHeight); + } + long endTime = System.currentTimeMillis(); + recordTest("drawString " + maxTests + " times", (endTime-startTime)); + } + + private class GraphicsTest extends Canvas implements Runnable + { + Thread paintThread; + boolean done = false; + boolean doPaint = false; + boolean withClipping = false; + + public GraphicsTest() + { + paintThread = new Thread(this); + paintThread.start(); + } + + public void run() + { + int runCount = 0; + while (!done) + { + runCount++; + + try + { + synchronized (this) + { + while (!doPaint) + { + try + { + wait(200); + } + catch (InterruptedException exception) + { + return; + } + } + } + + if (iterations != 0) + System.out.println("--- run...(" + runCount + "/" + iterations + + ") ------------------------------------------------------"); + + Graphics g = getGraphics(); + Dimension size = getSize(); + logger.logp(Level.INFO, "AicasGraphicsBenchmark.GraphicsTest", "run", + "Start testing non-double-buffered drawing"); + runSet_noClipping(g,size); + runSet_zeroClipping(g, size); + runSet_withClipping(g, size); + g.dispose(); + + if (doubleBufferFlag) + { + logger.logp(Level.INFO, "AicasGraphicsBenchmark.GraphicsTest", + "run", "Start testing double-buffered drawing"); + Graphics canvas = getGraphics(); + Image doublebuffer = createImage(size.width,size.height); + g = doublebuffer.getGraphics(); + runSet_noClipping(g,size); + g.dispose(); + canvas.drawImage(doublebuffer, 0, 0, this); + + g = doublebuffer.getGraphics(); + runSet_withClipping(g, size); + g.dispose(); + canvas.drawImage(doublebuffer, 0, 0, this); + + g = doublebuffer.getGraphics(); + runSet_zeroClipping(g, size); + g.dispose(); + canvas.drawImage(doublebuffer, 0, 0, this); + canvas.dispose(); + } + + printReport(); + + if (iterations != 0) + { + if (iterations != -1) + iterations--; + } + else + { + System.out.println("--- done --------------------------------------------------------"); + synchronized (this) + { + doPaint = false; + } + done = true; + } + } + catch (Error error) + { + System.err.println("Error: " + error); + System.exit(129); + } + } + System.exit(0); + } + + private void runSet_zeroClipping(Graphics g, Dimension size) + { + int clipped_width; + int clipped_height; + int clipped_x; + int clipped_y; + + clipped_width = 0; + clipped_height = 0; + clipped_x = (size.width) / 2; + clipped_y = (size.height) / 2; + g.setClip(0, 0, size.width, size.height); + g.setColor(Color.BLACK); + g.fillRect(0, 0, size.width, size.height); + g.setColor(Color.WHITE); + g.drawRect(0, 0, size.width - 1, size.height - 1); + g.fillRect(clipped_x - 1, clipped_y - 1, clipped_width + 2, clipped_height + 2); + + g.clipRect(clipped_x, clipped_y, clipped_width, clipped_height); + g.setColor(Color.BLACK); + g.fillRect(0, 0, size.width, size.height); + + setTestContext("clipping to zero"); + + runTestSet(g, size); + } + + private void runSet_withClipping(Graphics g, Dimension size) + { + int clipped_width = 2 * size.width / 3; + int clipped_height = 2 * size.height / 3; + int clipped_x = (size.width - clipped_width) / 2; + int clipped_y = (size.height - clipped_height) / 2; + + g.setClip(0,0,size.width,size.height); + + g.setColor(Color.BLACK); + g.fillRect(0, 0, size.width, size.height); + g.setColor(Color.GREEN); + g.drawRect(0, 0, size.width - 1, size.height - 1); + g.setColor(Color.WHITE); + g.fillRect(clipped_x - 1, clipped_y - 1, clipped_width + 2, clipped_height + 2); + + g.clipRect(clipped_x, clipped_y, clipped_width, clipped_height); + g.setColor(Color.BLACK); + g.fillRect(0, 0, size.width, size.height); + + setTestContext("with clipping"); + + runTestSet(g, size); + } + + public void runSet_noClipping(Graphics g, Dimension size) + { + g.setColor(Color.BLACK); + g.fillRect(0, 0, size.width, size.height); + + setTestContext("without clipping"); + + runTestSet(g, size); + } + + public void paint(Graphics g) + { + synchronized(this) + { + doPaint=true; + notify(); + } + } + } +} + +class TestContext +{ +} + +class TestSet +{ + private Map testsMap = new TreeMap(); + + public void putTest(String testName, TestRecorder recoder) + { + testsMap.put(testName,recoder); + } + + public TestRecorder getTest(String testName) + { + return (TestRecorder)testsMap.get(testName); + } + + public Iterator testIterator() + { + return testsMap.keySet().iterator(); + } +} + +class TestRecorder +{ + String test; + long totalTime = 0; + long minTime = Long.MAX_VALUE; + long maxTime = Long.MIN_VALUE; + int runCount = 0; + + /** + * @return Returns the maxTime. + */ + public final long getMaxTime() + { + return maxTime; + } + + /** + * @return Returns the minTime. + */ + public final long getMinTime() + { + return minTime; + } + + /** + * @return Returns the test name. + */ + public final String getTestName() + { + return test; + } + + public final double getAverage() + { + return ((double)totalTime) / ((double)runCount); + } + + public TestRecorder(String testName) + { + test = testName; + } + + public void addRun(long time) + { + totalTime += time; + if(minTime > time) + minTime = time; + if(maxTime < time) + maxTime = time; + runCount += 1; + } +} diff --git a/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/AnimationApplet.java b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/AnimationApplet.java new file mode 100644 index 000000000..aea8cd4f6 --- /dev/null +++ b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/AnimationApplet.java @@ -0,0 +1,232 @@ +/* AnimationApplet.java -- An example of an old-style AWT applet + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath examples. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. */ + +package gnu.classpath.examples.awt; + +import java.awt.*; +import java.applet.*; + + +/** + * AnimationApplet demonstrates the need for Xflush calls in + * GdkGraphics.c. To see how this demo can fail in their absence, + * remove the contents of schedule_flush in GdkGraphics.c. The + * animation will be so choppy that it is effectively stopped. + */ +public class AnimationApplet + extends Applet + implements Runnable +{ + boolean going = false; + Thread animThread = null; + int SPEED = 5; + int circleX = 0; + int circleY = 0; + int circleXold = 0; + int circleYold = 0; + int circleXdelta = 0; + int circleYdelta = 0; + int circleDiameter = 0; + int autoCircleX = 0; + int autoCircleY = 0; + int autoCircleXold = 0; + int autoCircleYold = 0; + int autoCircleXdelta = (int) (0.66 * SPEED); + int autoCircleYdelta = (int) (1.33 * SPEED); + int boardWidth = 0; + int boardHeight = 0; + int CIRCLE_SIZE = 5; + + private Graphics appletGraphics; + + // Update the circles' location values. + private void moveCircles() + { + circleX += circleXdelta; + if (circleX < 0) + circleX = 0; + if (circleX > boardWidth - circleDiameter) + circleX = boardWidth - circleDiameter; + + circleY += circleYdelta; + if (circleY < 0) + circleY = 0; + if (circleY > boardHeight - circleDiameter) + circleY = boardHeight - circleDiameter; + + autoCircleX += autoCircleXdelta; + if (autoCircleX < 0) + { + autoCircleX = 0; + autoCircleXdelta = -autoCircleXdelta; + } + if (autoCircleX > boardWidth - circleDiameter) + { + autoCircleX = boardWidth - circleDiameter; + autoCircleXdelta = -autoCircleXdelta; + } + + autoCircleY += autoCircleYdelta; + if (autoCircleY < 0) + { + autoCircleY = 0; + autoCircleYdelta = -autoCircleYdelta; + } + if (autoCircleY > boardHeight - circleDiameter) + { + autoCircleY = boardHeight - circleDiameter; + autoCircleYdelta = -autoCircleYdelta; + } + } + + // Clear the circle in the old location and paint a new circle + // in the new location. + private void paintCircles() + { + appletGraphics.setColor(Color.BLUE); + appletGraphics.fillOval(circleXold, circleYold, circleDiameter, + circleDiameter); + appletGraphics.setColor(Color.YELLOW); + appletGraphics.fillOval(circleX, circleY, circleDiameter, + circleDiameter); + + appletGraphics.setColor(Color.BLUE); + appletGraphics.fillOval(autoCircleXold, autoCircleYold, circleDiameter, + circleDiameter); + appletGraphics.setColor(Color.WHITE); + appletGraphics.fillOval(autoCircleX, autoCircleY, circleDiameter, + circleDiameter); + } + + // Override Applet.run. + public void run() + { + while (animThread != null) + { + circleXold = circleX; + circleYold = circleY; + autoCircleXold = autoCircleX; + autoCircleYold = autoCircleY; + + moveCircles(); + paintCircles(); + + if (animThread != null) + { + try + { + Thread.sleep(20); + } + catch (InterruptedException e) + { + } + } + } + } + + // Override Applet.paint. + public void paint(Graphics g) + { + boardWidth = this.getSize().width; + boardHeight = this.getSize().height; + g.setColor(Color.BLUE); + g.fillRect(0, 0, boardWidth, boardHeight); + if (!going) + { + FontMetrics fm = appletGraphics.getFontMetrics(); + appletGraphics.setColor(Color.WHITE); + String msg = "Click to Start"; + appletGraphics.drawString(msg, + (boardWidth >> 1) - (fm.stringWidth(msg) >> 1), + (boardHeight >> 1) - (fm.getHeight() >> 1)); + } + } + + // Override Applet.destroy. + public void destroy() + { + // animThread.stop(); + animThread = null; + } + + // Override Applet.init. + public void init() + { + boardWidth = this.getSize().width; + boardHeight = this.getSize().height; + going = false; + appletGraphics = getGraphics(); + appletGraphics.setFont(new Font(appletGraphics.getFont().getName(), + Font.BOLD, 15)); + } + + // Override Component.preferredSize for when we're run standalone. + public Dimension preferredSize () + { + return new Dimension (400, 400); + } + + // Override Applet.handleEvent, the old-style AWT-event handler. + public boolean handleEvent(Event event) + { + switch (event.id) + { + case Event.MOUSE_DOWN: + if (!going) + { + going = true; + circleDiameter = boardWidth / CIRCLE_SIZE; + circleX = (boardWidth - circleDiameter) >> 1; + circleY = (boardHeight - circleDiameter) >> 1; + circleXdelta = 0; + circleYdelta = 0; + repaint(); + animThread = new Thread(this); + animThread.start(); + } + break; + case Event.KEY_ACTION: + case Event.KEY_PRESS: + if (event.key == Event.LEFT) + circleXdelta = -SPEED; + else if (event.key == Event.RIGHT) + circleXdelta = SPEED; + else if (event.key == Event.UP) + circleYdelta = -SPEED; + else if (event.key == Event.DOWN) + circleYdelta = SPEED; + break; + case Event.KEY_ACTION_RELEASE: + case Event.KEY_RELEASE: + if (event.key == Event.LEFT && circleXdelta < 0) + circleXdelta = 0; + else if (event.key == Event.RIGHT && circleXdelta > 0) + circleXdelta = 0; + else if (event.key == Event.UP && circleYdelta < 0) + circleYdelta = 0; + else if (event.key == Event.DOWN && circleYdelta > 0) + circleYdelta = 0; + break; + default: + break; + } + return false; + } +} diff --git a/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/Demo.java b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/Demo.java new file mode 100644 index 000000000..bd5e755cb --- /dev/null +++ b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/Demo.java @@ -0,0 +1,1189 @@ +/* Demo.java -- Shows examples of AWT components + Copyright (C) 1998, 1999, 2002, 2004, 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath examples. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. */ + +package gnu.classpath.examples.awt; + +import java.awt.BorderLayout; +import java.awt.Button; +import java.awt.Canvas; +import java.awt.Checkbox; +import java.awt.CheckboxGroup; +import java.awt.CheckboxMenuItem; +import java.awt.Choice; +import java.awt.Color; +import java.awt.Cursor; +import java.awt.Dialog; +import java.awt.Dimension; +import java.awt.DisplayMode; +import java.awt.FileDialog; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.GridLayout; +import java.awt.Image; +import java.awt.Insets; +import java.awt.Label; +import java.awt.List; +import java.awt.Menu; +import java.awt.MenuBar; +import java.awt.MenuItem; +import java.awt.MenuShortcut; +import java.awt.Panel; +import java.awt.ScrollPane; +import java.awt.TextField; +import java.awt.Toolkit; +import java.awt.Window; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.dnd.DnDConstants; +import java.awt.dnd.DragGestureEvent; +import java.awt.dnd.DragGestureListener; +import java.awt.dnd.DragSource; +import java.awt.dnd.DragSourceContext; +import java.awt.dnd.DragSourceDragEvent; +import java.awt.dnd.DragSourceDropEvent; +import java.awt.dnd.DragSourceEvent; +import java.awt.dnd.DragSourceListener; +import java.awt.dnd.DropTarget; +import java.awt.dnd.DropTargetDragEvent; +import java.awt.dnd.DropTargetDropEvent; +import java.awt.dnd.DropTargetEvent; +import java.awt.dnd.DropTargetListener; +import java.awt.dnd.InvalidDnDOperationException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.net.URL; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; + +class Demo +{ + public static void main(String args[]) + { + MainWindow f = new MainWindow(); + f.show(); + } + + static interface SubWindow + { + public void init (); + } + + static class PrettyPanel extends Panel + { + Insets myInsets; + + public PrettyPanel () + { + myInsets = new Insets (10, 10, 10, 10); + } + public Insets getInsets () + { + return myInsets; + } + } + + static abstract class PrettyFrame extends Frame + { + public PrettyFrame () + { + ((BorderLayout) getLayout ()).setHgap (5); + ((BorderLayout) getLayout ()).setVgap (5); + } + + public Insets getInsets() + { + Insets oldInsets = super.getInsets (); + return new Insets (oldInsets.top+10, + oldInsets.left+10, + oldInsets.bottom+10, + oldInsets.right+10); + } + } + + static abstract class SubFrame extends PrettyFrame implements SubWindow + { + boolean initted = false; + + public void setVisible (boolean visible) + { + if (!initted && visible) + init(); + super.setVisible (visible); + } + } + + static class MainWindow extends PrettyFrame implements ActionListener + { + Button closeButton; + + Hashtable windows; + Vector buttons; + + void addSubWindow (String name, SubWindow w) + { + Button b = new Button (name); + b.addActionListener (this); + + buttons.addElement (b); + windows.put (b, w); + } + + MainWindow () + { + MenuBar mb = new MenuBar (); + Menu menu = new Menu ("File"); + Menu submenu = new Menu ("Testing", true); + submenu.add (new CheckboxMenuItem ("FooBar")); + submenu.add (new CheckboxMenuItem ("BarFoo")); + menu.add (submenu); + menu.add (new MenuItem("Orange")); + MenuItem quit = new MenuItem("Quit", new MenuShortcut('Q')); + quit.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + System.exit(0); + } + }); + menu.add(quit); + mb.add (menu); + + menu = new Menu("Edit", true); + menu.add(new MenuItem("Cut")); + menu.add(new MenuItem("Copy")); + menu.add(new MenuItem("Paste")); + mb.add (menu); + + Menu helpMenu = new Menu("Help"); + helpMenu.add(new MenuItem("About")); + mb.add(helpMenu); + mb.setHelpMenu(helpMenu); + + setMenuBar (mb); + + String version = System.getProperty("gnu.classpath.version"); + add (new Label ("GNU Classpath " + version), "North"); + + closeButton = new Button ("Close"); + closeButton.addActionListener (this); + closeButton.setFont (new Font ("Serif", Font.BOLD | Font.ITALIC, 18)); + add (closeButton, "South"); + + windows = new Hashtable (); + buttons = new Vector (); + + addSubWindow ("Buttons", new ButtonsWindow ()); + addSubWindow ("Cursors", new CursorsWindow ()); + addSubWindow ("Dialog", new DialogWindow (this)); + addSubWindow ("File", new FileWindow (this)); + addSubWindow ("Labels", new LabelWindow ()); + addSubWindow ("List", new ListWindow ()); + addSubWindow ("Radio Buttons", new RadioWindow ()); + addSubWindow ("TextField", new TextFieldWindow ()); + addSubWindow ("RandomTests", new TestWindow (this)); + addSubWindow ("RoundRect", new RoundRectWindow ()); + addSubWindow ("Animation", new AnimationWindow ()); + addSubWindow ("Resolution", new ResolutionWindow ()); + addSubWindow ("Fullscreen", new FullscreenWindow ()); + addSubWindow ("Drag n' Drop", new DragDropWindow ()); + + Panel sp = new Panel(); + PrettyPanel p = new PrettyPanel(); + p.setLayout (new GridLayout (windows.size(), 1)); + + for (Enumeration e = buttons.elements (); e.hasMoreElements (); ) + { + p.add ((Button) e.nextElement ()); + } + + sp.add (p); + add (sp, "Center"); + + setTitle ("AWT Demo"); + pack(); + } + + public void actionPerformed (ActionEvent evt) + { + Button source = (Button) evt.getSource (); + + if (source==closeButton) + { + dispose(); + System.exit (0); + } + + Window w = (Window) windows.get (source); + if (w.isVisible ()) + w.dispose (); + else + { + if (w instanceof Dialog) + { + w.show(); + } + else + { + w.setVisible (true); + } + } + } + } + + static class ButtonsWindow extends SubFrame implements ActionListener + { + Button b[] = new Button [9]; + + public void init () + { + initted = true; + Panel p = new Panel (); + p.setLayout (new GridLayout (0, 3, 5, 5)); + + for (int i=0; i<9; i++) + { + b[i]=new Button ("button" + (i+1)); + b[i].addActionListener (this); + } + + p.add (b[0]); + p.add (b[6]); + p.add (b[4]); + p.add (b[8]); + p.add (b[1]); + p.add (b[7]); + p.add (b[3]); + p.add (b[5]); + p.add (b[2]); + + add (p, "North"); + + Button cb = new Button ("close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + add (cb, "South"); + setTitle ("Buttons"); + pack(); + } + + public void actionPerformed (ActionEvent evt) + { + Button source = (Button) evt.getSource (); + + for (int i = 0; i < 9; i++) + { + if (source == b[i]) + { + int i2 = ((i + 1) == 9) ? 0 : (i + 1); + if (b[i2].isVisible()) + b[i2].setVisible(false); + else + b[i2].setVisible(true); + } + } + } + } + + + static class DialogWindow extends Dialog implements SubWindow + { + Label text; + Frame parent; + boolean initted = false; + + public DialogWindow (Frame f) + { + super (f, true); + + this.parent = f; + + addWindowListener (new WindowAdapter () + { + public void windowClosing (WindowEvent e) + { + text.setVisible (false); + hide (); + } + }); + } + + public void setVisible (boolean visible) + { + if (!initted && visible) + init(); + super.setVisible (visible); + } + + public void show () + { + if (!initted) + init(); + super.show (); + } + + public void init () + { + text = new Label ("Dialog Test"); + text.setAlignment (Label.CENTER); + + add (text, "North"); + text.setVisible (false); + + Panel p = new PrettyPanel(); + + Button cb = new Button ("OK"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) + { + text.setVisible (false); + hide(); + } + }); + + p.setLayout (new GridLayout (1, 3)); + ((GridLayout) p.getLayout ()).setHgap (5); + ((GridLayout) p.getLayout ()).setVgap (5); + p.add (cb); + + Button toggle = new Button ("Toggle"); + p.add (toggle); + + toggle.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) + { + if (text.isVisible ()) + text.setVisible (false); + else + text.setVisible (true); + doLayout(); + } + }); + + Button subdlg = new Button ("SubDialog"); + p.add (subdlg); + + subdlg.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) + { + DialogWindow sw = new DialogWindow (parent); + sw.show (); + } + }); + + add (p, "South"); + setTitle ("Dialog"); + pack(); + } + } + + static class CursorsWindow extends SubFrame implements ItemListener + { + Choice cursorChoice; + Canvas cursorCanvas; + + public void init () + { + cursorChoice = new Choice(); + cursorChoice.add ("Default"); + cursorChoice.add ("Crosshair"); + cursorChoice.add ("Text"); + cursorChoice.add ("Wait"); + cursorChoice.add ("Southwest Resize"); + cursorChoice.add ("Southeast Resize"); + cursorChoice.add ("Northwest Resize"); + cursorChoice.add ("Northeast Resize"); + cursorChoice.add ("North Resize"); + cursorChoice.add ("South Resize"); + cursorChoice.add ("West Resize"); + cursorChoice.add ("East Resize"); + cursorChoice.add ("Hand"); + cursorChoice.add ("Move"); + + cursorChoice.addItemListener(this); + + add (cursorChoice, "North"); + + cursorCanvas = new Canvas () + { + public void paint (Graphics g) + { + Dimension d = this.getSize(); + g.setColor(Color.white); + g.fillRect(0, 0, d.width, d.height/2); + g.setColor(Color.black); + g.fillRect(0, d.height/2, d.width, d.height/2); + g.setColor(this.getBackground()); + g.fillRect(d.width/3, d.height/3, d.width/3, + d.height/3); + } + }; + + cursorCanvas.setSize (80,80); + + add (cursorCanvas, "Center"); + + Button cb = new Button ("Close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + + add (cb, "South"); + setTitle ("Cursors"); + pack(); + } + + public void itemStateChanged (ItemEvent e) + { + int index = cursorChoice.getSelectedIndex(); + cursorCanvas.setCursor(Cursor.getPredefinedCursor(index)); + } + } + + static class TextFieldWindow extends SubFrame implements ItemListener + { + Checkbox editable, visible, sensitive; + TextField text; + + public void init () + { + initted = true; + text = new TextField ("hello world"); + add (text, "North"); + + Panel p = new Panel(); + p.setLayout (new GridLayout (3, 1)); + ((GridLayout) p.getLayout ()).setHgap (5); + ((GridLayout) p.getLayout ()).setVgap (5); + + editable = new Checkbox("Editable", true); + p.add (editable); + editable.addItemListener (this); + + visible = new Checkbox("Visible", true); + p.add (visible); + visible.addItemListener (this); + + sensitive = new Checkbox("Sensitive", true); + p.add (sensitive); + sensitive.addItemListener (this); + + add (p, "Center"); + + Button cb = new Button ("Close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + + add (cb, "South"); + setTitle ("TextField"); + pack(); + } + + public void itemStateChanged (ItemEvent e) + { + boolean on=true; + + if (e.getStateChange () == ItemEvent.DESELECTED) + on=false; + if (e.getSource() == editable) + text.setEditable (on); + if (e.getSource() == visible) + if (on) + text.setEchoChar ((char) 0); + else + text.setEchoChar ('*'); + if (e.getSource() == sensitive) + text.setEnabled (on); + + } + } + + static class FileWindow extends FileDialog implements SubWindow + { + boolean initted = false; + + public FileWindow (MainWindow mw) + { + super (mw); + } + + public void setVisible (boolean visible) + { + if (!initted && visible) + init(); + super.setVisible (visible); + } + + public void init() + { + initted = true; + } + } + + static class LabelWindow extends SubFrame + { + public void init () + { + initted = true; + + Panel p = new Panel(); + p.setLayout (new GridLayout (3, 1)); + ((GridLayout) p.getLayout ()).setHgap (5); + ((GridLayout) p.getLayout ()).setVgap (5); + + p.add (new Label ("left justified label", Label.LEFT)); + p.add (new Label ("center justified label", Label.CENTER)); + p.add (new Label ("right justified label", Label.RIGHT)); + + add (p, "Center"); + + Button cb = new Button ("Close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + + add (cb, "South"); + setTitle ("Labels"); + pack(); + } + } + + static class ListWindow extends SubFrame + { + public void init () + { + initted = true; + + Panel p = new Panel (); + p.setLayout (new GridLayout (3, 1)); + + List l = new List (5, true); + for (int i = 0; i < 10; i++) + l.add ("List item " + i); + + p.add (l); + + add (p, "Center"); + + Button cb = new Button ("Close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + + add (cb, "South"); + setTitle ("List"); + pack(); + } + } + + + static class RadioWindow extends SubFrame + { + public void init () + { + initted = true; + + Panel p = new Panel(); + p.setLayout (new GridLayout (3, 1)); + ((GridLayout) p.getLayout ()).setHgap (5); + ((GridLayout) p.getLayout ()).setVgap (5); + + final CheckboxGroup cg = new CheckboxGroup(); + final Checkbox[] boxes = new Checkbox[3]; + for (int i = 0; i < 3; ++i) + { + boxes[i] = new Checkbox("button" + i, cg, i == 0); + p.add(boxes[i]); + } + + add (p, "North"); + + p = new Panel(); + p.setLayout (new GridLayout (1, 3)); + ((GridLayout) p.getLayout ()).setHgap (5); + ((GridLayout) p.getLayout ()).setVgap (5); + + for (int i = 0; i < 3; ++i) + { + final int val = i; + Button tweak = new Button ("Set " + i); + tweak.addActionListener(new ActionListener () + { + public void actionPerformed (ActionEvent e) + { + cg.setSelectedCheckbox(boxes[val]); + } + }); + p.add(tweak); + } + + add (p, "Center"); + + Button cb = new Button ("Close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + + add (cb, "South"); + setTitle ("Radio Buttons"); + pack(); + } + } + + static class TestWindow extends SubFrame + { + static int xs = 5, ys = 5; + final Frame parent; + + public TestWindow(Frame f) + { + parent = f; + } + + public void init() + { + initted = true; + + addWindowListener (new WindowAdapter () + { + public void windowClosing (WindowEvent e) + { + hide (); + } + }); + + Panel pan = new Panel(); + + final Label l = new Label ("Pithy Message:"); + l.setCursor (Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR)); + pan.add (l); + + TextField tf = new TextField("Hello world!"); + pan.add(tf); + add(pan,"North"); + + final Image img; + URL imageurl; + imageurl = this.getClass() + .getResource("/gnu/classpath/examples/icons/big-warning.png"); + img = Toolkit.getDefaultToolkit().createImage(imageurl); + + final Canvas ch = new Canvas() + { + public void paint (Graphics g) + { + g.drawImage(img, xs + 25, ys + 25, this); + + Font font = new Font ("Serif", Font.PLAIN, 18); + g.setFont (font); + g.setXORMode (Color.red); + + g.drawString("Hi Red!", xs + 15, ys + 10); + g.setColor (Color.blue); + g.drawLine (xs, ys, xs + 100, ys + 100); + + } + }; + ch.setSize(150, 150); + add(ch, "Center"); + + final ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); + final Panel p = new Panel(); + p.add(new Button("Stop")); + p.add(new Button("evil")); + p.add(new Button("hoarders")); + p.add(new Button("use")); + p.add(new Button("GNU!")); + + sp.add(p); + add(sp, "South"); + + Panel east_panel = new Panel(); + east_panel.setLayout(new GridLayout (0,1)); + + CheckboxGroup group = new CheckboxGroup(); + Checkbox cb = new Checkbox("one", group, true); + east_panel.add(cb); + cb = new Checkbox("two", group, false); + east_panel.add(cb); + + add(east_panel,"East"); + + final Button wb = new Button(); + wb.setLabel("Hello World!"); + wb.addActionListener(new ActionListener() + { + public void actionPerformed (ActionEvent e) + { + l.setText ("Hello World!"); + + final Dialog d = new Dialog(parent); + d.setLayout(new FlowLayout()); + d.setModal(true); + Button b = new Button("foobar"); + b.addMouseListener(new MouseAdapter() + { + public void mousePressed (MouseEvent me) + { + d.hide (); + } + }); + d.add (b); + + List ch = new List(); + ch.add("Ding"); + ch.add("September"); + ch.add("Red"); + ch.add("Quassia"); + ch.add("Pterodactyl"); + d.add(ch); + + d.pack (); + d.show (); + } + }); + + wb.addMouseListener(new MouseAdapter() + { + public void mousePressed(MouseEvent e) { + xs++; + ys++; + ch.repaint (); + } + }); + + add(wb,"West"); + + pack(); + show(); + + sp.setScrollPosition (10,0); + + Toolkit t = Toolkit.getDefaultToolkit(); + t.beep(); + } + } + + static class ResolutionWindow extends SubFrame + { + GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + + public void init () + { + initted = true; + + setTitle("Change Screen Resolution"); + final List list = new List(); + DisplayMode[] modes = gd.getDisplayModes(); + + for (int i=0;i<modes.length;i++ ) + list.add(modes[i].getWidth() + "x" + + modes[i].getHeight() + + ((modes[i].getBitDepth() != DisplayMode.BIT_DEPTH_MULTI) + ? "x" + modes[i].getBitDepth() + "bpp" + : "") + + ((modes[i].getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN) + ? "@" + modes[i].getRefreshRate() + "Hz" + : "")); + + ActionListener al = new ActionListener() + { + public void actionPerformed(ActionEvent ae) + { + int i = list.getSelectedIndex(); + gd.setDisplayMode(gd.getDisplayModes()[i]); + } + }; + + Button b = new Button("Switch"); + Button c = new Button("Close"); + + list.addActionListener(al); + b.addActionListener(al); + + c.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + + setLayout(new GridLayout(3, 1, 5, 5)); + add(list); + add(b); + add(c); + + pack(); + } + } + + static class DragDropWindow + extends SubFrame + implements ActionListener, DropTargetListener + { + DragLabel source = new DragLabel("Drag and drop me to the following Button", + Label.CENTER); + + Button target = new Button(); + + public void init() + { + source.setForeground(Color.red); + add(source, BorderLayout.NORTH); + + target.addActionListener(this); + add(target, BorderLayout.SOUTH); + + new DropTarget(target, DnDConstants.ACTION_COPY_OR_MOVE, this); + + setSize(205, 100); + + pack(); + } + + public void actionPerformed(ActionEvent e) + { + Button b = (Button) e.getSource(); + b.setLabel(""); + } + + public void dragEnter(DropTargetDragEvent e) + { + } + + public void dragExit(DropTargetEvent e) + { + } + + public void dragOver(DropTargetDragEvent e) + { + } + + public void drop(DropTargetDropEvent e) + { + try + { + Transferable t = e.getTransferable(); + + if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) + { + e.acceptDrop(e.getDropAction()); + + String s; + s = (String) t.getTransferData(DataFlavor.stringFlavor); + + target.setLabel(s); + + e.dropComplete(true); + } + else + e.rejectDrop(); + } + catch (java.io.IOException e2) + { + } + catch (UnsupportedFlavorException e2) + { + } + } + + public void dropActionChanged(DropTargetDragEvent e) + { + } + + class DragLabel + extends Label + implements DragGestureListener, DragSourceListener + { + private DragSource ds = DragSource.getDefaultDragSource(); + + public DragLabel(String s, int alignment) + { + super(s, alignment); + int action = DnDConstants.ACTION_COPY_OR_MOVE; + ds.createDefaultDragGestureRecognizer(this, action, this); + } + + public void dragGestureRecognized(DragGestureEvent e) + { + try + { + Transferable t = new StringSelection(getText()); + e.startDrag(DragSource.DefaultCopyNoDrop, t, this); + } + catch (InvalidDnDOperationException e2) + { + System.out.println(e2); + } + } + + public void dragDropEnd(DragSourceDropEvent e) + { + if (e.getDropSuccess() == false) + return; + + int action = e.getDropAction(); + if ((action & DnDConstants.ACTION_MOVE) != 0) + setText(""); + } + + public void dragEnter(DragSourceDragEvent e) + { + DragSourceContext ctx = e.getDragSourceContext(); + + int action = e.getDropAction(); + if ((action & DnDConstants.ACTION_COPY) != 0) + ctx.setCursor(DragSource.DefaultCopyDrop); + else + ctx.setCursor(DragSource.DefaultCopyNoDrop); + } + + public void dragExit(DragSourceEvent e) + { + } + + public void dragOver(DragSourceDragEvent e) + { + } + + public void dropActionChanged(DragSourceDragEvent e) + { + } + } + } + + static class FullscreenWindow extends SubFrame + { + GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + + public void init () + { + initted = true; + + setTitle("Fullscreen Exclusive Mode"); + + ActionListener al = new ActionListener() + { + public void actionPerformed(ActionEvent ae) + { + if (gd.getFullScreenWindow() == FullscreenWindow.this) + gd.setFullScreenWindow(null); + else + gd.setFullScreenWindow(FullscreenWindow.this); + } + }; + + Button b = new Button("Toggle Fullscreen"); + Button c = new Button("Close"); + + b.addActionListener(al); + + c.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + gd.setFullScreenWindow(null); + dispose(); + } + }); + + setLayout(new GridLayout(3, 1, 5, 5)); + add(b); + add(c); + + pack(); + } + } + + static class RoundRectWindow extends SubFrame + { + public void init () + { + initted = true; + setTitle("RoundRect"); + setLayout(new BorderLayout()); + add(new DrawRoundRect(), "West"); + Button cb = new Button ("Close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) { + dispose(); + } + }); + add(cb, "Center"); + add(new FillRoundRect(), "East"); + pack(); + } + + static class DrawRoundRect extends Panel + { + + public Dimension getPreferredSize() + { + return new Dimension(500, 500); + } + + public void paint( Graphics g ) + { + // left side + + // rectangles should be identical + g.setColor(Color.red); + g.drawRect(50, 50, 300, 100); + g.setColor(Color.black); + g.drawRoundRect(50, 50, 300, 100, 0, 0); + + // small round corners + g.setColor(Color.red); + g.drawRect(50, 200, 300, 100); + g.setColor(Color.black); + g.drawRoundRect(50, 200, 300, 100, 25, 25); + + // round ends + g.setColor(Color.red); + g.drawRect(50, 350, 300, 100); + g.setColor(Color.black); + g.drawRoundRect(50, 350, 300, 100, 25, 100); + + // right side + + // circle only + g.setColor(Color.blue); + g.drawOval(375, 50, 100, 100); + + // round rectangle should exactly cover circle + g.setColor(Color.blue); + g.drawOval(375, 200, 100, 100); + g.setColor(Color.black); + g.drawRoundRect(375, 200, 100, 100, 100, 100); + + // round rectangle should look like a circle + g.setColor(Color.red); + g.drawRect(375, 350, 100, 100); + g.setColor(Color.black); + g.drawRoundRect(375, 350, 100, 100, 100, 100); + } + } + + static class FillRoundRect extends Panel + { + + public Dimension getPreferredSize() + { + return new Dimension(500, 500); + } + + public void paint( Graphics g ) + { + // left side + + // rectangles should be identical + g.setColor(Color.red); + g.fillRect(50, 50, 300, 100); + g.setColor(Color.black); + g.fillRoundRect(50, 50, 300, 100, 0, 0); + + // small round corners + g.setColor(Color.red); + g.fillRect(50, 200, 300, 100); + g.setColor(Color.black); + g.fillRoundRect(50, 200, 300, 100, 25, 25); + + // round ends + g.setColor(Color.red); + g.fillRect(50, 350, 300, 100); + g.setColor(Color.black); + g.fillRoundRect(50, 350, 300, 100, 25, 100); + + // right side + + // circle only + g.setColor(Color.blue); + g.fillOval(375, 50, 100, 100); + + // round rectangle should exactly cover circle + g.setColor(Color.blue); + g.fillOval(375, 200, 100, 100); + g.setColor(Color.black); + g.fillRoundRect(375, 200, 100, 100, 100, 100); + + // round rectangle should look like a circle + g.setColor(Color.red); + g.fillRect(375, 350, 100, 100); + g.setColor(Color.black); + g.fillRoundRect(375, 350, 100, 100, 100, 100); + } + } + } + + static class AnimationWindow extends SubFrame + { + AnimationApplet a; + public void init () + { + initted = true; + setTitle("Animation"); + Button cb = new Button ("Close"); + cb.addActionListener(new ActionListener () { + public void actionPerformed (ActionEvent e) + { + if (a != null) + { + a.destroy(); + dispose(); + } + } + }); + a = new AnimationApplet(); + add(a, "Center"); + add(cb, "South"); + pack(); + } + + public void show() + { + super.show(); + a.init(); + a.run(); + } + } +} diff --git a/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/HintingDemo.java b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/HintingDemo.java new file mode 100644 index 000000000..99c262b71 --- /dev/null +++ b/gcc-4.4.3/libjava/classpath/examples/gnu/classpath/examples/awt/HintingDemo.java @@ -0,0 +1,420 @@ +/* Demo.java -- Shows examples of AWT components + Copyright (C) 1998, 1999, 2002, 2004, 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath examples. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. */ + +package gnu.classpath.examples.awt; + +import gnu.java.awt.font.FontDelegate; +import gnu.java.awt.font.GNUGlyphVector; +import gnu.java.awt.font.opentype.OpenTypeFontFactory; + +import java.awt.BasicStroke; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.GridLayout; +import java.awt.Insets; +import java.awt.RenderingHints; +import java.awt.Shape; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.font.FontRenderContext; +import java.io.File; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.text.StringCharacterIterator; + +import javax.swing.BoxLayout; +import javax.swing.JCheckBox; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.JTextField; +import javax.swing.border.TitledBorder; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +public class HintingDemo extends JFrame { + + FontDelegate font; + GNUGlyphVector glyph; + GlyphPreview glyphPreview; + HintPanel hintPanel; + StringViewer stringViewer; + Chooser chooser; + char character; + Options options; + boolean antiAlias; + boolean showGrid; + boolean showOriginal; + boolean showHinted; + int flags; + + class StringViewer extends JPanel + implements ActionListener + { + JTextField input; + GNUGlyphVector gv; + Viewer viewer; + StringViewer() + { + setLayout(new GridLayout(0, 1)); + setBorder(new TitledBorder("Use this field to render complete strings")); + input = new JTextField(); + input.addActionListener(this); + add(input); + viewer = new Viewer(); + add(viewer); + } + + public void actionPerformed(ActionEvent event) + { + refresh(); + } + + void refresh() + { + gv = (GNUGlyphVector) + font.createGlyphVector(new Font("Dialog", 0, 12), + new FontRenderContext(null, false, false), + new StringCharacterIterator(input.getText())); + viewer.repaint(); + } + + class Viewer extends JPanel + { + protected void paintComponent(Graphics g) + { + if (gv != null && g instanceof Graphics2D) + { + Graphics2D g2d = (Graphics2D) g; + if (antiAlias) + { + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + } + else + { + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + } + g2d.clearRect(0, 0, getWidth(), getHeight()); + g2d.setColor(Color.BLACK); + Shape outline = gv.getOutline(0, 0, + flags | FontDelegate.FLAG_FITTED); + g2d.translate(20, Math.floor(outline.getBounds2D().getHeight()) + 2); + g2d.fill(outline); + } + } + } + } + + class HintPanel extends JPanel + { + + HintPanel() + { + setBorder(new TitledBorder("Detailed glyph view")); + } + protected void paintComponent(Graphics g) + { + if (glyph != null && g instanceof Graphics2D) + { + Graphics2D g2d = (Graphics2D) g.create(); + Insets i = getInsets(); + g2d.clearRect(i.left, i.top, getWidth() - i.left - i.right, + getHeight() - i.top - i.bottom); + if (showGrid) + { + g2d.setColor(Color.GRAY); + for (int x = 20; x < getWidth(); x += 20) + { + g2d.drawLine(x, i.top, x, getHeight() - i.top - i.bottom); + } + for (int y = 20; y < getHeight(); y += 20) + { + g2d.drawLine(i.left, y, getWidth() - i.left - i.right, y); + } + } +// g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, +// RenderingHints.VALUE_ANTIALIAS_ON); + g2d.translate(40, 300); + g2d.scale(20., 20.); + g2d.setStroke(new BasicStroke((float) (1/10.))); + if (showOriginal) + { + g2d.setColor(Color.RED); + g2d.draw(glyph.getOutline(0, 0, + flags & ~FontDelegate.FLAG_FITTED)); + } + if (showHinted) + { + g2d.setColor(Color.RED); + g2d.draw(glyph.getOutline(0, 0, + flags | FontDelegate.FLAG_FITTED)); + } + } + } + + } + + class GlyphPreview extends JPanel + { + protected void paintComponent(Graphics g) + { + if (glyph != null && g instanceof Graphics2D) + { + Graphics2D g2d = (Graphics2D) g; + if (antiAlias) + { + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + } + else + { + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + } + g2d.clearRect(0, 0, getWidth(), getHeight()); + g2d.setColor(Color.BLACK); + Shape outline = glyph.getOutline(0, 0, + flags | FontDelegate.FLAG_FITTED); + g2d.translate(20, outline.getBounds2D().getHeight() + 2); + g2d.fill(outline); + } + } + + } + + HintingDemo() + { + File file = new File("/usr/share/fonts/truetype/freefont/FreeSans.ttf"); + loadFont(file); + setLayout(new BorderLayout()); + chooser = new Chooser(); + add(chooser, BorderLayout.NORTH); + hintPanel = new HintPanel(); + character = 'A'; + add(hintPanel); + + options = new Options(); + add(options, BorderLayout.EAST); + + stringViewer = new StringViewer(); + add(stringViewer, BorderLayout.SOUTH); + refresh(); + + JMenuBar mb = new JMenuBar(); + setJMenuBar(mb); + JMenu fileMenu = new JMenu("File"); + mb.add(fileMenu); + JMenuItem loadFont = new JMenuItem("Load font"); + loadFont.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent ev) + { + JFileChooser fc = new JFileChooser() + { + public boolean accept(File f) + { + return f.isDirectory() || f.getName().endsWith(".ttf"); + } + }; + int status = fc.showOpenDialog(HintingDemo.this); + if (status == JFileChooser.APPROVE_OPTION) + { + File file = fc.getSelectedFile(); + loadFont(file); + } + } + }); + fileMenu.add(loadFont); + } + + void refresh() + { + if (chooser != null) + chooser.refresh(); + if (glyphPreview != null) + glyphPreview.repaint(); + if (hintPanel != null) + hintPanel.repaint(); + if (stringViewer != null) + stringViewer.refresh(); + } + + class Options extends JPanel + implements ActionListener + { + JCheckBox antiAliasOpt; + JCheckBox showGridOpt; + JCheckBox showOriginalOpt; + JCheckBox showHintedOpt; + JCheckBox hintHorizontalOpt; + JCheckBox hintVerticalOpt; + JCheckBox hintEdgeOpt; + JCheckBox hintStrongOpt; + JCheckBox hintWeakOpt; + Options() + { + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + setBorder(new TitledBorder("Hinting options")); + antiAliasOpt = new JCheckBox("Antialias"); + antiAliasOpt.setSelected(true); + antiAliasOpt.addActionListener(this); + add(antiAliasOpt); + showGridOpt = new JCheckBox("Show grid"); + showGridOpt.setSelected(true); + showGridOpt.addActionListener(this); + add(showGridOpt); + showOriginalOpt = new JCheckBox("Show original"); + showOriginalOpt.setSelected(true); + showOriginalOpt.addActionListener(this); + add(showOriginalOpt); + showHintedOpt = new JCheckBox("Show hinted"); + showHintedOpt.setSelected(true); + showHintedOpt.addActionListener(this); + add(showHintedOpt); + hintHorizontalOpt = new JCheckBox("Hint horizontal"); + hintHorizontalOpt.setSelected(true); + hintHorizontalOpt.addActionListener(this); + add(hintHorizontalOpt); + hintVerticalOpt = new JCheckBox("Hint vertical"); + hintVerticalOpt.setSelected(true); + hintVerticalOpt.addActionListener(this); + add(hintVerticalOpt); + hintEdgeOpt = new JCheckBox("Hint edge points"); + hintEdgeOpt.setSelected(true); + hintEdgeOpt.addActionListener(this); + add(hintEdgeOpt); + hintStrongOpt = new JCheckBox("Hint strong points"); + hintStrongOpt.setSelected(true); + hintStrongOpt.addActionListener(this); + add(hintStrongOpt); + hintWeakOpt = new JCheckBox("Hint weak points"); + hintWeakOpt.setSelected(true); + hintWeakOpt.addActionListener(this); + add(hintWeakOpt); + sync(); + } + + void sync() + { + antiAlias = antiAliasOpt.isSelected(); + showGrid = showGridOpt.isSelected(); + showOriginal = showOriginalOpt.isSelected(); + showHinted = showHintedOpt.isSelected(); + if (hintHorizontalOpt.isSelected()) + flags &= ~FontDelegate.FLAG_NO_HINT_HORIZONTAL; + else + flags |= FontDelegate.FLAG_NO_HINT_HORIZONTAL; + if (hintVerticalOpt.isSelected()) + flags &= ~FontDelegate.FLAG_NO_HINT_VERTICAL; + else + flags |= FontDelegate.FLAG_NO_HINT_VERTICAL; + if (hintEdgeOpt.isSelected()) + flags &= ~FontDelegate.FLAG_NO_HINT_EDGE_POINTS; + else + flags |= FontDelegate.FLAG_NO_HINT_EDGE_POINTS; + if (hintStrongOpt.isSelected()) + flags &= ~FontDelegate.FLAG_NO_HINT_STRONG_POINTS; + else + flags |= FontDelegate.FLAG_NO_HINT_STRONG_POINTS; + if (hintWeakOpt.isSelected()) + flags &= ~FontDelegate.FLAG_NO_HINT_WEAK_POINTS; + else + flags |= FontDelegate.FLAG_NO_HINT_WEAK_POINTS; + + refresh(); + } + + public void actionPerformed(ActionEvent event) + { + sync(); + } + } + + class Chooser extends JPanel + { + JSpinner spin; + Chooser() + { + setLayout(new GridLayout(1, 0)); + setBorder(new TitledBorder("Choose and preview the character to render")); + spin = new JSpinner(); + spin.addChangeListener(new ChangeListener() + { + + public void stateChanged(ChangeEvent event) + { + int val = ((Integer) spin.getValue()).intValue(); + setGlyph((char) val); + } + }); + add(spin); + glyphPreview = new GlyphPreview(); + add(glyphPreview); + } + void refresh() + { + spin.setValue(new Integer(character)); + repaint(); + } + } + + private void loadFont(File file) + { + try + { + RandomAccessFile raf = new RandomAccessFile(file, "r"); + FileChannel chan = raf.getChannel(); + ByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, raf.length()); + FontDelegate[] fonts = OpenTypeFontFactory.createFonts(buf); + font = fonts[0]; + setGlyph(character); + refresh(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + void setGlyph(char ch) + { + character = ch; + glyph = (GNUGlyphVector) + font.createGlyphVector(new Font("Dialog", 0, 12), + new FontRenderContext(null, false, false), + new StringCharacterIterator(new String(new char[]{ch}))); + refresh(); + } + + public static void main(String[] args) { + HintingDemo f = new HintingDemo(); + f.setSize(500, 500); + f.setVisible(true); + } +} |