# HG changeset patch # User mrkam # Date 1301072134 -3600 # Node ID 9e16ce7c2da2665ee4dd16f338d10d81087c3dbc # Parent 64b719bac41114da0bd156685c7a71ebb5e516c4 7027683: /applets/GraphicsTest demo needs to be improved Reviewed-by: alexp diff -r 64b719bac411 -r 9e16ce7c2da2 jdk/src/share/demo/applets/GraphicsTest/AppletFrame.java --- a/jdk/src/share/demo/applets/GraphicsTest/AppletFrame.java Fri Mar 25 17:52:51 2011 +0100 +++ b/jdk/src/share/demo/applets/GraphicsTest/AppletFrame.java Fri Mar 25 17:55:34 2011 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,8 +29,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - */ import java.awt.Frame; import java.awt.Event; @@ -38,66 +36,67 @@ import java.applet.Applet; import java.awt.AWTEvent; -// Applet to Application Frame window -class AppletFrame extends Frame -{ + +/** + * Applet to Application Frame window + */ +@SuppressWarnings("serial") +class AppletFrame extends Frame { public static void startApplet(String className, - String title, - String args[]) - { - // local variables - Applet a; - Dimension appletSize; + String title, + String args[]) { + // local variables + Applet a; + Dimension appletSize; - try - { - // create an instance of your applet class - a = (Applet) Class.forName(className).newInstance(); - } - catch (ClassNotFoundException e) { return; } - catch (InstantiationException e) { return; } - catch (IllegalAccessException e) { return; } + try { + // create an instance of your applet class + a = (Applet) Class.forName(className).newInstance(); + } catch (ClassNotFoundException e) { + return; + } catch (InstantiationException e) { + return; + } catch (IllegalAccessException e) { + return; + } - // initialize the applet - a.init(); - a.start(); + // initialize the applet + a.init(); + a.start(); - // create new application frame window - AppletFrame f = new AppletFrame(title); + // create new application frame window + AppletFrame f = new AppletFrame(title); - // add applet to frame window - f.add("Center", a); + // add applet to frame window + f.add("Center", a); - // resize frame window to fit applet - // assumes that the applet sets its own size - // otherwise, you should set a specific size here. - appletSize = a.getSize(); - f.pack(); - f.setSize(appletSize); + // resize frame window to fit applet + // assumes that the applet sets its own size + // otherwise, you should set a specific size here. + appletSize = a.getSize(); + f.pack(); + f.setSize(appletSize); - // show the window - f.show(); + // show the window + f.setVisible(true); } // end startApplet() - // constructor needed to pass window title to class Frame - public AppletFrame(String name) - { - // call java.awt.Frame(String) constructor - super(name); + public AppletFrame(String name) { + // call java.awt.Frame(String) constructor + super(name); } // needed to allow window close - public void processEvent(AWTEvent e) - { - // Window Destroy event - if (e.getID() == Event.WINDOW_DESTROY) - { - // exit the program - System.exit(0); - } - } // end handleEvent() + @Override + public void processEvent(AWTEvent e) { + // Window Destroy event + if (e.getID() == Event.WINDOW_DESTROY) { + // exit the program + System.exit(0); + } + } // end handleEvent() +} // end class AppletFrame -} // end class AppletFrame diff -r 64b719bac411 -r 9e16ce7c2da2 jdk/src/share/demo/applets/GraphicsTest/GraphicsTest.java --- a/jdk/src/share/demo/applets/GraphicsTest/GraphicsTest.java Fri Mar 25 17:52:51 2011 +0100 +++ b/jdk/src/share/demo/applets/GraphicsTest/GraphicsTest.java Fri Mar 25 17:55:34 2011 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,22 +29,23 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - */ import java.awt.*; import java.util.*; import java.awt.event.*; import java.applet.Applet; + +@SuppressWarnings("serial") class GraphicsPanel extends Panel { + ActionListener al; ItemListener il; public GraphicsCards cards; - GraphicsPanel(EventListener listener) { - al = (ActionListener)listener; - il = (ItemListener)listener; + GraphicsPanel(EventListener listener) { + al = (ActionListener) listener; + il = (ItemListener) listener; setLayout(new BorderLayout()); @@ -78,62 +79,75 @@ setSize(400, 400); } + @Override public Dimension getPreferredSize() { return new Dimension(200, 100); } } + +@SuppressWarnings("serial") public class GraphicsTest extends Applet -implements ActionListener, ItemListener { + implements ActionListener, ItemListener { + GraphicsPanel mainPanel; + @Override public void init() { setLayout(new BorderLayout()); add("Center", mainPanel = new GraphicsPanel(this)); } + @Override public void destroy() { remove(mainPanel); } + @Override public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand(); if ("next".equals(arg)) { - ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards); - } - else if ("previous".equals(arg)) { - ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards); + ((CardLayout) mainPanel.cards.getLayout()).next(mainPanel.cards); + } else if ("previous".equals(arg)) { + ((CardLayout) mainPanel.cards.getLayout()).previous(mainPanel.cards); } } + @Override public void itemStateChanged(ItemEvent e) { - ((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String)e.getItem()); + ((CardLayout) mainPanel.cards.getLayout()).show(mainPanel.cards, + (String) e.getItem()); } public static void main(String args[]) { AppletFrame.startApplet("GraphicsTest", "Graphics Test", args); } + @Override public String getAppletInfo() { return "An interactive demonstration of some graphics."; } } // end class GraphicsTest +@SuppressWarnings("serial") class GraphicsCards extends Panel { + public GraphicsCards() { setLayout(new CardLayout()); add("Arc", new ArcCard()); - add("Oval", new ShapeTest( new OvalShape() ) ); - add("Polygon", new ShapeTest( new PolygonShape() ) ); - add("Rect", new ShapeTest( new RectShape() ) ); - add("RoundRect", new ShapeTest( new RoundRectShape() ) ); + add("Oval", new ShapeTest(new OvalShape())); + add("Polygon", new ShapeTest(new PolygonShape())); + add("Rect", new ShapeTest(new RectShape())); + add("RoundRect", new ShapeTest(new RoundRectShape())); } } // end class GraphicsCards +@SuppressWarnings("serial") class ArcCard extends Panel { + public ArcCard() { setLayout(new GridLayout(0, 2)); add(new ArcPanel(true)); @@ -144,7 +158,9 @@ } // end class ArcCard +@SuppressWarnings("serial") class ArcDegreePanel extends Panel { + boolean filled; public ArcDegreePanel(boolean filled) { @@ -152,290 +168,262 @@ } void arcSteps(Graphics g, - int step, - int x, - int y, - int w, - int h, - Color c1, - Color c2) { - int a1 = 0; - int a2 = step; - int progress = 0; - g.setColor(c1); - for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1) { - if (g.getColor() == c1) { - g.setColor(c2); - } - else { - g.setColor(c1); - } + int step, + int x, + int y, + int w, + int h, + Color c1, + Color c2) { + int a1 = 0; + int a2 = step; + int progress = 0; + g.setColor(c1); + for (; (a1 + a2) <= 360; a1 = a1 + a2, a2 += 1) { + if (g.getColor() == c1) { + g.setColor(c2); + } else { + g.setColor(c1); + } - if (filled) { - g.fillArc(x, y, w, h, a1, a2); - } - else { - g.drawArc(x, y, w, h, a1, a2); - } + if (filled) { + g.fillArc(x, y, w, h, a1, a2); + } else { + g.drawArc(x, y, w, h, a1, a2); + } - progress = a1+a2; - } // end for + progress = a1 + a2; + } // end for - if (progress != 360) { - if (filled) { - g.fillArc(x, y, w, h, a1, 360 - progress); - } - else { - g.drawArc(x, y, w, h, a1, 360 - progress); - } - } // end if - } // end arcSteps() + if (progress != 360) { + if (filled) { + g.fillArc(x, y, w, h, a1, 360 - progress); + } else { + g.drawArc(x, y, w, h, a1, 360 - progress); + } + } // end if + } // end arcSteps() + @Override public void paint(Graphics g) { Rectangle r = getBounds(); arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue); arcSteps(g, - 2, - r.width / 4, - r.height / 4, - r.width / 2, - r.height / 2, - Color.yellow, - Color.green); + 2, + r.width / 4, + r.height / 4, + r.width / 2, + r.height / 2, + Color.yellow, + Color.green); arcSteps(g, - 1, - (r.width * 3) / 8, - (r.height * 3) / 8, - r.width / 4, - r.height / 4, - Color.magenta, - Color.white); + 1, + (r.width * 3) / 8, + (r.height * 3) / 8, + r.width / 4, + r.height / 4, + Color.magenta, + Color.white); - } // end paint() + } // end paint() } // end class ArcDegreePanel +@SuppressWarnings("serial") class ArcPanel extends Panel { + boolean filled; public ArcPanel(boolean filled) { - this.filled = filled; - } + this.filled = filled; + } - public void paint(Graphics g) - { - Rectangle r = getBounds(); + @Override + public void paint(Graphics g) { + Rectangle r = getBounds(); - g.setColor(Color.yellow); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, 0, 45); - } - else - { - g.drawArc(0, 0, r.width, r.height, 0, 45); - } + g.setColor(Color.yellow); + if (filled) { + g.fillArc(0, 0, r.width, r.height, 0, 45); + } else { + g.drawArc(0, 0, r.width, r.height, 0, 45); + } - g.setColor(Color.green); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, 90, -45); - } - else - { - g.drawArc(0, 0, r.width, r.height, 90, -45); - } + g.setColor(Color.green); + if (filled) { + g.fillArc(0, 0, r.width, r.height, 90, -45); + } else { + g.drawArc(0, 0, r.width, r.height, 90, -45); + } - g.setColor(Color.orange); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, 135, -45); - } - else - { - g.drawArc(0, 0, r.width, r.height, 135, -45); - } + g.setColor(Color.orange); + if (filled) { + g.fillArc(0, 0, r.width, r.height, 135, -45); + } else { + g.drawArc(0, 0, r.width, r.height, 135, -45); + } - g.setColor(Color.magenta); + g.setColor(Color.magenta); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, -225, 45); - } - else - { - g.drawArc(0, 0, r.width, r.height, -225, 45); - } + if (filled) { + g.fillArc(0, 0, r.width, r.height, -225, 45); + } else { + g.drawArc(0, 0, r.width, r.height, -225, 45); + } - g.setColor(Color.yellow); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, 225, -45); - } - else - { - g.drawArc(0, 0, r.width, r.height, 225, -45); - } + g.setColor(Color.yellow); + if (filled) { + g.fillArc(0, 0, r.width, r.height, 225, -45); + } else { + g.drawArc(0, 0, r.width, r.height, 225, -45); + } - g.setColor(Color.green); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, -135, 45); - } - else - { - g.drawArc(0, 0, r.width, r.height, -135, 45); - } + g.setColor(Color.green); + if (filled) { + g.fillArc(0, 0, r.width, r.height, -135, 45); + } else { + g.drawArc(0, 0, r.width, r.height, -135, 45); + } - g.setColor(Color.orange); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, -45, -45); - } - else - { - g.drawArc(0, 0, r.width, r.height, -45, -45); - } + g.setColor(Color.orange); + if (filled) { + g.fillArc(0, 0, r.width, r.height, -45, -45); + } else { + g.drawArc(0, 0, r.width, r.height, -45, -45); + } - g.setColor(Color.magenta); - if (filled) - { - g.fillArc(0, 0, r.width, r.height, 315, 45); - } - else - { - g.drawArc(0, 0, r.width, r.height, 315, 45); - } + g.setColor(Color.magenta); + if (filled) { + g.fillArc(0, 0, r.width, r.height, 315, 45); + } else { + g.drawArc(0, 0, r.width, r.height, 315, 45); + } - } // end paint() - + } // end paint() } // end class ArcPanel -abstract class Shape -{ - abstract void draw(Graphics g, int x, int y, int w, int h); - abstract void fill(Graphics g, int x, int y, int w, int h); +abstract class Shape { + + abstract void draw(Graphics g, int x, int y, int w, int h); + + abstract void fill(Graphics g, int x, int y, int w, int h); +} + + +class RectShape extends Shape { + + @Override + void draw(Graphics g, int x, int y, int w, int h) { + g.drawRect(x, y, w, h); + } + + @Override + void fill(Graphics g, int x, int y, int w, int h) { + g.fillRect(x, y, w, h); + } } -class RectShape extends Shape -{ - void draw(Graphics g, int x, int y, int w, int h) - { - g.drawRect(x, y, w, h); - } +class OvalShape extends Shape { - void fill(Graphics g, int x, int y, int w, int h) - { - g.fillRect(x, y, w, h); - } + @Override + void draw(Graphics g, int x, int y, int w, int h) { + g.drawOval(x, y, w, h); + } + + @Override + void fill(Graphics g, int x, int y, int w, int h) { + g.fillOval(x, y, w, h); + } } -class OvalShape extends Shape -{ - void draw(Graphics g, int x, int y, int w, int h) - { - g.drawOval(x, y, w, h); - } +class RoundRectShape extends Shape { - void fill(Graphics g, int x, int y, int w, int h) - { - g.fillOval(x, y, w, h); - } + @Override + void draw(Graphics g, int x, int y, int w, int h) { + g.drawRoundRect(x, y, w, h, 10, 10); + } + + @Override + void fill(Graphics g, int x, int y, int w, int h) { + g.fillRoundRect(x, y, w, h, 10, 10); + } } -class RoundRectShape extends Shape -{ - void draw(Graphics g, int x, int y, int w, int h) - { - g.drawRoundRect(x, y, w, h, 10, 10); - } +class PolygonShape extends Shape { + // class variables - void fill(Graphics g, int x, int y, int w, int h) - { - g.fillRoundRect(x, y, w, h, 10, 10); - } -} + Polygon p; + Polygon pBase; -class PolygonShape extends Shape -{ - // class variables - Polygon p; - Polygon pBase; + public PolygonShape() { + pBase = new Polygon(); + pBase.addPoint(0, 0); + pBase.addPoint(10, 0); + pBase.addPoint(5, 15); + pBase.addPoint(10, 20); + pBase.addPoint(5, 20); + pBase.addPoint(0, 10); + pBase.addPoint(0, 0); + } - public PolygonShape() - { - pBase = new Polygon(); - pBase.addPoint(0, 0); - pBase.addPoint(10, 0); - pBase.addPoint(5, 15); - pBase.addPoint(10, 20); - pBase.addPoint(5, 20); - pBase.addPoint(0, 10); - pBase.addPoint(0, 0); - } + void scalePolygon(float w, float h) { + p = new Polygon(); + for (int i = 0; i < pBase.npoints; ++i) { + p.addPoint((int) (pBase.xpoints[i] * w), + (int) (pBase.ypoints[i] * h)); + } - void scalePolygon(float w, float h) - { - p = new Polygon(); - for (int i = 0; i < pBase.npoints; ++i) - { - p.addPoint( (int) (pBase.xpoints[i] * w), - (int) (pBase.ypoints[i] * h) ); - } - - } + } - void draw(Graphics g, int x, int y, int w, int h) - { - Graphics ng = g.create(); - try { - ng.translate(x, y); - scalePolygon( (float) ( (float) w / (float) 10 ), - (float) ( (float) h / (float) 20 ) ); - ng.drawPolygon(p); - } finally { - ng.dispose(); + @Override + void draw(Graphics g, int x, int y, int w, int h) { + Graphics ng = g.create(); + try { + ng.translate(x, y); + scalePolygon(((float) w / 10f), ((float) h / 20f)); + ng.drawPolygon(p); + } finally { + ng.dispose(); + } } - } - void fill(Graphics g, int x, int y, int w, int h) - { - Graphics ng = g.create(); - try { - ng.translate(x, y); - scalePolygon( (float) ( (float) w / (float) 10 ), - (float) ( (float) h / (float) 20 ) ); - ng.fillPolygon(p); - } finally { - ng.dispose(); + @Override + void fill(Graphics g, int x, int y, int w, int h) { + Graphics ng = g.create(); + try { + ng.translate(x, y); + scalePolygon(((float) w / 10f), ((float) h / 20f)); + ng.fillPolygon(p); + } finally { + ng.dispose(); + } } - } } -class ShapeTest extends Panel -{ - Shape shape; - int step; +@SuppressWarnings("serial") +class ShapeTest extends Panel { + + Shape shape; + int step; - public ShapeTest(Shape shape, int step) - { - this.shape = shape; - this.step = step; - } + public ShapeTest(Shape shape, int step) { + this.shape = shape; + this.step = step; + } - public ShapeTest(Shape shape) - { - this(shape, 10); - } + public ShapeTest(Shape shape) { + this(shape, 10); + } + @Override public void paint(Graphics g) { Rectangle bounds = getBounds(); @@ -443,35 +431,22 @@ Color color; - for (color=Color.red, - cx=bounds.x, - cy=bounds.y, - cw=bounds.width / 2, - ch=bounds.height; - cw > 0 && ch > 0; - - cx+=step, - cy += step, - cw -= (step * 2), - ch -= (step * 2), - color=ColorUtils.darker(color, 0.9) ) { + for (color = Color.red, cx = bounds.x, cy = bounds.y, + cw = bounds.width / 2, ch = bounds.height; + cw > 0 && ch > 0; + cx += step, cy += step, cw -= (step * 2), ch -= (step * 2), + color = ColorUtils.darker(color, 0.9)) { g.setColor(color); shape.draw(g, cx, cy, cw, ch); } - for (cx=bounds.x + bounds.width / 2, - cy=bounds.y, - cw=bounds.width / 2, ch=bounds.height; - cw > 0 && ch > 0; - - cx+=step, - cy += step, - cw -= (step * 2), - ch -= (step * 2) ) { + for (cx = bounds.x + bounds.width / 2, cy = bounds.y, + cw = bounds.width / 2, ch = bounds.height; + cw > 0 && ch > 0; + cx += step, cy += step, cw -= (step * 2), ch -= (step * 2)) { if (g.getColor() == Color.red) { g.setColor(Color.blue); - } - else { + } else { g.setColor(Color.red); } @@ -480,16 +455,18 @@ } // end paint() } // end class ShapeTest + class ColorUtils { + static Color brighter(Color c, double factor) { - return new Color( Math.min((int)(c.getRed() *(1/factor)), 255), - Math.min((int)(c.getGreen()*(1/factor)), 255), - Math.min((int)(c.getBlue() *(1/factor)), 255) ); + return new Color(Math.min((int) (c.getRed() * (1 / factor)), 255), + Math.min((int) (c.getGreen() * (1 / factor)), 255), + Math.min((int) (c.getBlue() * (1 / factor)), 255)); } static Color darker(Color c, double factor) { - return new Color( Math.max((int)(c.getRed() *factor), 0), - Math.max((int)(c.getGreen()*factor), 0), - Math.max((int)(c.getBlue() *factor), 0) ); + return new Color(Math.max((int) (c.getRed() * factor), 0), + Math.max((int) (c.getGreen() * factor), 0), + Math.max((int) (c.getBlue() * factor), 0)); } }