提交 eae7f2e0 编写于 作者: M mrkam

7027683: /applets/GraphicsTest demo needs to be improved

Reviewed-by: alexp
上级 6123d99f
/* /*
* 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,8 +29,6 @@ ...@@ -29,8 +29,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
import java.awt.Frame; import java.awt.Frame;
import java.awt.Event; import java.awt.Event;
...@@ -38,66 +36,67 @@ import java.awt.Dimension; ...@@ -38,66 +36,67 @@ import java.awt.Dimension;
import java.applet.Applet; import java.applet.Applet;
import java.awt.AWTEvent; 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, public static void startApplet(String className,
String title, String title,
String args[]) String args[]) {
{ // local variables
// local variables Applet a;
Applet a; Dimension appletSize;
Dimension appletSize;
try try {
{ // create an instance of your applet class
// create an instance of your applet class a = (Applet) Class.forName(className).newInstance();
a = (Applet) Class.forName(className).newInstance(); } catch (ClassNotFoundException e) {
} return;
catch (ClassNotFoundException e) { return; } } catch (InstantiationException e) {
catch (InstantiationException e) { return; } return;
catch (IllegalAccessException e) { return; } } catch (IllegalAccessException e) {
return;
}
// initialize the applet // initialize the applet
a.init(); a.init();
a.start(); a.start();
// create new application frame window // create new application frame window
AppletFrame f = new AppletFrame(title); AppletFrame f = new AppletFrame(title);
// add applet to frame window // add applet to frame window
f.add("Center", a); f.add("Center", a);
// resize frame window to fit applet // resize frame window to fit applet
// assumes that the applet sets its own size // assumes that the applet sets its own size
// otherwise, you should set a specific size here. // otherwise, you should set a specific size here.
appletSize = a.getSize(); appletSize = a.getSize();
f.pack(); f.pack();
f.setSize(appletSize); f.setSize(appletSize);
// show the window // show the window
f.show(); f.setVisible(true);
} // end startApplet() } // end startApplet()
// constructor needed to pass window title to class Frame // constructor needed to pass window title to class Frame
public AppletFrame(String name) public AppletFrame(String name) {
{ // call java.awt.Frame(String) constructor
// call java.awt.Frame(String) constructor super(name);
super(name);
} }
// needed to allow window close // needed to allow window close
public void processEvent(AWTEvent e) @Override
{ public void processEvent(AWTEvent e) {
// Window Destroy event // Window Destroy event
if (e.getID() == Event.WINDOW_DESTROY) if (e.getID() == Event.WINDOW_DESTROY) {
{ // exit the program
// exit the program System.exit(0);
System.exit(0); }
} } // end handleEvent()
} // end handleEvent()
} // end class AppletFrame } // end class AppletFrame
/* /*
* 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -29,22 +29,23 @@ ...@@ -29,22 +29,23 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
*/
import java.awt.*; import java.awt.*;
import java.util.*; import java.util.*;
import java.awt.event.*; import java.awt.event.*;
import java.applet.Applet; import java.applet.Applet;
@SuppressWarnings("serial")
class GraphicsPanel extends Panel { class GraphicsPanel extends Panel {
ActionListener al; ActionListener al;
ItemListener il; ItemListener il;
public GraphicsCards cards; public GraphicsCards cards;
GraphicsPanel(EventListener listener) { GraphicsPanel(EventListener listener) {
al = (ActionListener)listener; al = (ActionListener) listener;
il = (ItemListener)listener; il = (ItemListener) listener;
setLayout(new BorderLayout()); setLayout(new BorderLayout());
...@@ -78,62 +79,75 @@ class GraphicsPanel extends Panel { ...@@ -78,62 +79,75 @@ class GraphicsPanel extends Panel {
setSize(400, 400); setSize(400, 400);
} }
@Override
public Dimension getPreferredSize() { public Dimension getPreferredSize() {
return new Dimension(200, 100); return new Dimension(200, 100);
} }
} }
@SuppressWarnings("serial")
public class GraphicsTest extends Applet public class GraphicsTest extends Applet
implements ActionListener, ItemListener { implements ActionListener, ItemListener {
GraphicsPanel mainPanel; GraphicsPanel mainPanel;
@Override
public void init() { public void init() {
setLayout(new BorderLayout()); setLayout(new BorderLayout());
add("Center", mainPanel = new GraphicsPanel(this)); add("Center", mainPanel = new GraphicsPanel(this));
} }
@Override
public void destroy() { public void destroy() {
remove(mainPanel); remove(mainPanel);
} }
@Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand(); String arg = e.getActionCommand();
if ("next".equals(arg)) { if ("next".equals(arg)) {
((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards); ((CardLayout) mainPanel.cards.getLayout()).next(mainPanel.cards);
} } else if ("previous".equals(arg)) {
else if ("previous".equals(arg)) { ((CardLayout) mainPanel.cards.getLayout()).previous(mainPanel.cards);
((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
} }
} }
@Override
public void itemStateChanged(ItemEvent e) { 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[]) { public static void main(String args[]) {
AppletFrame.startApplet("GraphicsTest", "Graphics Test", args); AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
} }
@Override
public String getAppletInfo() { public String getAppletInfo() {
return "An interactive demonstration of some graphics."; return "An interactive demonstration of some graphics.";
} }
} // end class GraphicsTest } // end class GraphicsTest
@SuppressWarnings("serial")
class GraphicsCards extends Panel { class GraphicsCards extends Panel {
public GraphicsCards() { public GraphicsCards() {
setLayout(new CardLayout()); setLayout(new CardLayout());
add("Arc", new ArcCard()); add("Arc", new ArcCard());
add("Oval", new ShapeTest( new OvalShape() ) ); add("Oval", new ShapeTest(new OvalShape()));
add("Polygon", new ShapeTest( new PolygonShape() ) ); add("Polygon", new ShapeTest(new PolygonShape()));
add("Rect", new ShapeTest( new RectShape() ) ); add("Rect", new ShapeTest(new RectShape()));
add("RoundRect", new ShapeTest( new RoundRectShape() ) ); add("RoundRect", new ShapeTest(new RoundRectShape()));
} }
} // end class GraphicsCards } // end class GraphicsCards
@SuppressWarnings("serial")
class ArcCard extends Panel { class ArcCard extends Panel {
public ArcCard() { public ArcCard() {
setLayout(new GridLayout(0, 2)); setLayout(new GridLayout(0, 2));
add(new ArcPanel(true)); add(new ArcPanel(true));
...@@ -144,7 +158,9 @@ class ArcCard extends Panel { ...@@ -144,7 +158,9 @@ class ArcCard extends Panel {
} // end class ArcCard } // end class ArcCard
@SuppressWarnings("serial")
class ArcDegreePanel extends Panel { class ArcDegreePanel extends Panel {
boolean filled; boolean filled;
public ArcDegreePanel(boolean filled) { public ArcDegreePanel(boolean filled) {
...@@ -152,290 +168,262 @@ class ArcDegreePanel extends Panel { ...@@ -152,290 +168,262 @@ class ArcDegreePanel extends Panel {
} }
void arcSteps(Graphics g, void arcSteps(Graphics g,
int step, int step,
int x, int x,
int y, int y,
int w, int w,
int h, int h,
Color c1, Color c1,
Color c2) { Color c2) {
int a1 = 0; int a1 = 0;
int a2 = step; int a2 = step;
int progress = 0; int progress = 0;
g.setColor(c1); g.setColor(c1);
for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1) { for (; (a1 + a2) <= 360; a1 = a1 + a2, a2 += 1) {
if (g.getColor() == c1) { if (g.getColor() == c1) {
g.setColor(c2); g.setColor(c2);
} } else {
else { g.setColor(c1);
g.setColor(c1); }
}
if (filled) { if (filled) {
g.fillArc(x, y, w, h, a1, a2); g.fillArc(x, y, w, h, a1, a2);
} } else {
else { g.drawArc(x, y, w, h, a1, a2);
g.drawArc(x, y, w, h, a1, a2); }
}
progress = a1+a2; progress = a1 + a2;
} // end for } // end for
if (progress != 360) { if (progress != 360) {
if (filled) { if (filled) {
g.fillArc(x, y, w, h, a1, 360 - progress); g.fillArc(x, y, w, h, a1, 360 - progress);
} } else {
else { g.drawArc(x, y, w, h, a1, 360 - progress);
g.drawArc(x, y, w, h, a1, 360 - progress); }
} } // end if
} // end if } // end arcSteps()
} // end arcSteps()
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
Rectangle r = getBounds(); Rectangle r = getBounds();
arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue); arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
arcSteps(g, arcSteps(g,
2, 2,
r.width / 4, r.width / 4,
r.height / 4, r.height / 4,
r.width / 2, r.width / 2,
r.height / 2, r.height / 2,
Color.yellow, Color.yellow,
Color.green); Color.green);
arcSteps(g, arcSteps(g,
1, 1,
(r.width * 3) / 8, (r.width * 3) / 8,
(r.height * 3) / 8, (r.height * 3) / 8,
r.width / 4, r.width / 4,
r.height / 4, r.height / 4,
Color.magenta, Color.magenta,
Color.white); Color.white);
} // end paint() } // end paint()
} // end class ArcDegreePanel } // end class ArcDegreePanel
@SuppressWarnings("serial")
class ArcPanel extends Panel { class ArcPanel extends Panel {
boolean filled; boolean filled;
public ArcPanel(boolean filled) { public ArcPanel(boolean filled) {
this.filled = filled; this.filled = filled;
} }
public void paint(Graphics g) @Override
{ public void paint(Graphics g) {
Rectangle r = getBounds(); Rectangle r = getBounds();
g.setColor(Color.yellow); g.setColor(Color.yellow);
if (filled) if (filled) {
{ g.fillArc(0, 0, r.width, r.height, 0, 45);
g.fillArc(0, 0, r.width, r.height, 0, 45); } else {
} g.drawArc(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);
g.setColor(Color.green); } else {
if (filled) g.drawArc(0, 0, r.width, r.height, 90, -45);
{ }
g.fillArc(0, 0, r.width, r.height, 90, -45);
} g.setColor(Color.orange);
else if (filled) {
{ g.fillArc(0, 0, r.width, r.height, 135, -45);
g.drawArc(0, 0, r.width, r.height, 90, -45); } else {
} g.drawArc(0, 0, r.width, r.height, 135, -45);
}
g.setColor(Color.orange);
if (filled) g.setColor(Color.magenta);
{
g.fillArc(0, 0, r.width, r.height, 135, -45); if (filled) {
} g.fillArc(0, 0, r.width, r.height, -225, 45);
else } else {
{ g.drawArc(0, 0, r.width, r.height, -225, 45);
g.drawArc(0, 0, r.width, r.height, 135, -45); }
}
g.setColor(Color.yellow);
g.setColor(Color.magenta); if (filled) {
g.fillArc(0, 0, r.width, r.height, 225, -45);
if (filled) } else {
{ g.drawArc(0, 0, r.width, r.height, 225, -45);
g.fillArc(0, 0, r.width, r.height, -225, 45); }
}
else g.setColor(Color.green);
{ if (filled) {
g.drawArc(0, 0, r.width, r.height, -225, 45); g.fillArc(0, 0, r.width, r.height, -135, 45);
} } else {
g.drawArc(0, 0, r.width, r.height, -135, 45);
g.setColor(Color.yellow); }
if (filled)
{ g.setColor(Color.orange);
g.fillArc(0, 0, r.width, r.height, 225, -45); if (filled) {
} g.fillArc(0, 0, r.width, r.height, -45, -45);
else } else {
{ g.drawArc(0, 0, r.width, r.height, -45, -45);
g.drawArc(0, 0, r.width, r.height, 225, -45); }
}
g.setColor(Color.magenta);
g.setColor(Color.green); if (filled) {
if (filled) g.fillArc(0, 0, r.width, r.height, 315, 45);
{ } else {
g.fillArc(0, 0, r.width, r.height, -135, 45); g.drawArc(0, 0, r.width, r.height, 315, 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.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 } // end class ArcPanel
abstract class Shape abstract class Shape {
{
abstract void draw(Graphics g, int x, int y, int w, int h); 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 void fill(Graphics g, int x, int y, int w, int h);
} }
class RectShape extends Shape class RectShape extends Shape {
{
void draw(Graphics g, int x, int y, int w, int h) @Override
{ void draw(Graphics g, int x, int y, int w, int h) {
g.drawRect(x, y, w, h); g.drawRect(x, y, w, h);
} }
void fill(Graphics g, int x, int y, int w, int h) @Override
{ void fill(Graphics g, int x, int y, int w, int h) {
g.fillRect(x, y, w, h); g.fillRect(x, y, w, h);
} }
} }
class OvalShape extends Shape class OvalShape extends Shape {
{
void draw(Graphics g, int x, int y, int w, int h) @Override
{ void draw(Graphics g, int x, int y, int w, int h) {
g.drawOval(x, y, w, h); g.drawOval(x, y, w, h);
} }
void fill(Graphics g, int x, int y, int w, int h) @Override
{ void fill(Graphics g, int x, int y, int w, int h) {
g.fillOval(x, y, w, h); g.fillOval(x, y, w, h);
} }
} }
class RoundRectShape extends Shape class RoundRectShape extends Shape {
{
void draw(Graphics g, int x, int y, int w, int h) @Override
{ void draw(Graphics g, int x, int y, int w, int h) {
g.drawRoundRect(x, y, w, h, 10, 10); g.drawRoundRect(x, y, w, h, 10, 10);
} }
void fill(Graphics g, int x, int y, int w, int h) @Override
{ void fill(Graphics g, int x, int y, int w, int h) {
g.fillRoundRect(x, y, w, h, 10, 10); g.fillRoundRect(x, y, w, h, 10, 10);
} }
} }
class PolygonShape extends Shape
{ class PolygonShape extends Shape {
// class variables // class variables
Polygon p;
Polygon pBase; Polygon p;
Polygon pBase;
public PolygonShape()
{ public PolygonShape() {
pBase = new Polygon(); pBase = new Polygon();
pBase.addPoint(0, 0); pBase.addPoint(0, 0);
pBase.addPoint(10, 0); pBase.addPoint(10, 0);
pBase.addPoint(5, 15); pBase.addPoint(5, 15);
pBase.addPoint(10, 20); pBase.addPoint(10, 20);
pBase.addPoint(5, 20); pBase.addPoint(5, 20);
pBase.addPoint(0, 10); pBase.addPoint(0, 10);
pBase.addPoint(0, 0); 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 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();
} }
}
void scalePolygon(float w, float h) {
void fill(Graphics g, int x, int y, int w, int h) p = new Polygon();
{ for (int i = 0; i < pBase.npoints; ++i) {
Graphics ng = g.create(); p.addPoint((int) (pBase.xpoints[i] * w),
try { (int) (pBase.ypoints[i] * h));
ng.translate(x, y); }
scalePolygon( (float) ( (float) w / (float) 10 ),
(float) ( (float) h / (float) 20 ) ); }
ng.fillPolygon(p);
} finally { @Override
ng.dispose(); 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();
}
}
@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 @SuppressWarnings("serial")
{ class ShapeTest extends Panel {
Shape shape;
int step;
public ShapeTest(Shape shape, int step) 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) public ShapeTest(Shape shape) {
{ this(shape, 10);
this(shape, 10); }
}
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
Rectangle bounds = getBounds(); Rectangle bounds = getBounds();
...@@ -443,35 +431,22 @@ class ShapeTest extends Panel ...@@ -443,35 +431,22 @@ class ShapeTest extends Panel
Color color; Color color;
for (color=Color.red, for (color = Color.red, cx = bounds.x, cy = bounds.y,
cx=bounds.x, cw = bounds.width / 2, ch = bounds.height;
cy=bounds.y, cw > 0 && ch > 0;
cw=bounds.width / 2, cx += step, cy += step, cw -= (step * 2), ch -= (step * 2),
ch=bounds.height; color = ColorUtils.darker(color, 0.9)) {
cw > 0 && ch > 0;
cx+=step,
cy += step,
cw -= (step * 2),
ch -= (step * 2),
color=ColorUtils.darker(color, 0.9) ) {
g.setColor(color); g.setColor(color);
shape.draw(g, cx, cy, cw, ch); shape.draw(g, cx, cy, cw, ch);
} }
for (cx=bounds.x + bounds.width / 2, for (cx = bounds.x + bounds.width / 2, cy = bounds.y,
cy=bounds.y, cw = bounds.width / 2, ch = bounds.height;
cw=bounds.width / 2, ch=bounds.height; cw > 0 && ch > 0;
cw > 0 && ch > 0; cx += step, cy += step, cw -= (step * 2), ch -= (step * 2)) {
cx+=step,
cy += step,
cw -= (step * 2),
ch -= (step * 2) ) {
if (g.getColor() == Color.red) { if (g.getColor() == Color.red) {
g.setColor(Color.blue); g.setColor(Color.blue);
} } else {
else {
g.setColor(Color.red); g.setColor(Color.red);
} }
...@@ -480,16 +455,18 @@ class ShapeTest extends Panel ...@@ -480,16 +455,18 @@ class ShapeTest extends Panel
} // end paint() } // end paint()
} // end class ShapeTest } // end class ShapeTest
class ColorUtils { class ColorUtils {
static Color brighter(Color c, double factor) { static Color brighter(Color c, double factor) {
return new Color( Math.min((int)(c.getRed() *(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.getGreen() * (1 / factor)), 255),
Math.min((int)(c.getBlue() *(1/factor)), 255) ); Math.min((int) (c.getBlue() * (1 / factor)), 255));
} }
static Color darker(Color c, double factor) { static Color darker(Color c, double factor) {
return new Color( Math.max((int)(c.getRed() *factor), 0), return new Color(Math.max((int) (c.getRed() * factor), 0),
Math.max((int)(c.getGreen()*factor), 0), Math.max((int) (c.getGreen() * factor), 0),
Math.max((int)(c.getBlue() *factor), 0) ); Math.max((int) (c.getBlue() * factor), 0));
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册