Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/GraphicsTest/GraphicsTest.java
38829 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.awt.*;42import java.util.*;43import java.awt.event.*;44import java.applet.Applet;454647@SuppressWarnings("serial")48class GraphicsPanel extends Panel {4950ActionListener al;51ItemListener il;52public GraphicsCards cards;5354GraphicsPanel(EventListener listener) {55al = (ActionListener) listener;56il = (ItemListener) listener;5758setLayout(new BorderLayout());5960add("Center", cards = new GraphicsCards());6162Panel p = new Panel();63//p.setLayout(new BorderLayout());6465Button b = new Button("next");66b.addActionListener(al);67p.add(b);6869b = new Button("previous");70b.addActionListener(al);71p.add(b);7273p.add(new Label("go to:", Label.RIGHT));7475Choice c = new Choice();76c.addItemListener(il);77p.add(c);7879c.addItem("Arc");80c.addItem("Oval");81c.addItem("Polygon");82c.addItem("Rect");83c.addItem("RoundRect");8485add("North", p);8687setSize(400, 400);88}8990@Override91public Dimension getPreferredSize() {92return new Dimension(200, 100);93}94}959697@SuppressWarnings("serial")98public class GraphicsTest extends Applet99implements ActionListener, ItemListener {100101GraphicsPanel mainPanel;102103@Override104public void init() {105setLayout(new BorderLayout());106add("Center", mainPanel = new GraphicsPanel(this));107}108109@Override110public void destroy() {111remove(mainPanel);112}113114@Override115public void actionPerformed(ActionEvent e) {116String arg = e.getActionCommand();117118if ("next".equals(arg)) {119((CardLayout) mainPanel.cards.getLayout()).next(mainPanel.cards);120} else if ("previous".equals(arg)) {121((CardLayout) mainPanel.cards.getLayout()).previous(mainPanel.cards);122}123}124125@Override126public void itemStateChanged(ItemEvent e) {127((CardLayout) mainPanel.cards.getLayout()).show(mainPanel.cards,128(String) e.getItem());129}130131public static void main(String args[]) {132AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);133}134135@Override136public String getAppletInfo() {137return "An interactive demonstration of some graphics.";138}139} // end class GraphicsTest140141142@SuppressWarnings("serial")143class GraphicsCards extends Panel {144145public GraphicsCards() {146setLayout(new CardLayout());147add("Arc", new ArcCard());148add("Oval", new ShapeTest(new OvalShape()));149add("Polygon", new ShapeTest(new PolygonShape()));150add("Rect", new ShapeTest(new RectShape()));151add("RoundRect", new ShapeTest(new RoundRectShape()));152}153} // end class GraphicsCards154155156@SuppressWarnings("serial")157class ArcCard extends Panel {158159public ArcCard() {160setLayout(new GridLayout(0, 2));161add(new ArcPanel(true));162add(new ArcPanel(false));163add(new ArcDegreePanel(true));164add(new ArcDegreePanel(false));165}166} // end class ArcCard167168169@SuppressWarnings("serial")170class ArcDegreePanel extends Panel {171172boolean filled;173174public ArcDegreePanel(boolean filled) {175this.filled = filled;176}177178void arcSteps(Graphics g,179int step,180int x,181int y,182int w,183int h,184Color c1,185Color c2) {186int a1 = 0;187int a2 = step;188int progress = 0;189g.setColor(c1);190for (; (a1 + a2) <= 360; a1 = a1 + a2, a2 += 1) {191if (g.getColor() == c1) {192g.setColor(c2);193} else {194g.setColor(c1);195}196197if (filled) {198g.fillArc(x, y, w, h, a1, a2);199} else {200g.drawArc(x, y, w, h, a1, a2);201}202203progress = a1 + a2;204} // end for205206if (progress != 360) {207if (filled) {208g.fillArc(x, y, w, h, a1, 360 - progress);209} else {210g.drawArc(x, y, w, h, a1, 360 - progress);211}212} // end if213} // end arcSteps()214215@Override216public void paint(Graphics g) {217Rectangle r = getBounds();218219arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);220221arcSteps(g,2222,223r.width / 4,224r.height / 4,225r.width / 2,226r.height / 2,227Color.yellow,228Color.green);229230arcSteps(g,2311,232(r.width * 3) / 8,233(r.height * 3) / 8,234r.width / 4,235r.height / 4,236Color.magenta,237Color.white);238239} // end paint()240} // end class ArcDegreePanel241242243@SuppressWarnings("serial")244class ArcPanel extends Panel {245246boolean filled;247248public ArcPanel(boolean filled) {249this.filled = filled;250}251252@Override253public void paint(Graphics g) {254Rectangle r = getBounds();255256g.setColor(Color.yellow);257if (filled) {258g.fillArc(0, 0, r.width, r.height, 0, 45);259} else {260g.drawArc(0, 0, r.width, r.height, 0, 45);261}262263g.setColor(Color.green);264if (filled) {265g.fillArc(0, 0, r.width, r.height, 90, -45);266} else {267g.drawArc(0, 0, r.width, r.height, 90, -45);268}269270g.setColor(Color.orange);271if (filled) {272g.fillArc(0, 0, r.width, r.height, 135, -45);273} else {274g.drawArc(0, 0, r.width, r.height, 135, -45);275}276277g.setColor(Color.magenta);278279if (filled) {280g.fillArc(0, 0, r.width, r.height, -225, 45);281} else {282g.drawArc(0, 0, r.width, r.height, -225, 45);283}284285g.setColor(Color.yellow);286if (filled) {287g.fillArc(0, 0, r.width, r.height, 225, -45);288} else {289g.drawArc(0, 0, r.width, r.height, 225, -45);290}291292g.setColor(Color.green);293if (filled) {294g.fillArc(0, 0, r.width, r.height, -135, 45);295} else {296g.drawArc(0, 0, r.width, r.height, -135, 45);297}298299g.setColor(Color.orange);300if (filled) {301g.fillArc(0, 0, r.width, r.height, -45, -45);302} else {303g.drawArc(0, 0, r.width, r.height, -45, -45);304}305306g.setColor(Color.magenta);307if (filled) {308g.fillArc(0, 0, r.width, r.height, 315, 45);309} else {310g.drawArc(0, 0, r.width, r.height, 315, 45);311}312313} // end paint()314} // end class ArcPanel315316317abstract class Shape {318319abstract void draw(Graphics g, int x, int y, int w, int h);320321abstract void fill(Graphics g, int x, int y, int w, int h);322}323324325class RectShape extends Shape {326327@Override328void draw(Graphics g, int x, int y, int w, int h) {329g.drawRect(x, y, w, h);330}331332@Override333void fill(Graphics g, int x, int y, int w, int h) {334g.fillRect(x, y, w, h);335}336}337338339class OvalShape extends Shape {340341@Override342void draw(Graphics g, int x, int y, int w, int h) {343g.drawOval(x, y, w, h);344}345346@Override347void fill(Graphics g, int x, int y, int w, int h) {348g.fillOval(x, y, w, h);349}350}351352353class RoundRectShape extends Shape {354355@Override356void draw(Graphics g, int x, int y, int w, int h) {357g.drawRoundRect(x, y, w, h, 10, 10);358}359360@Override361void fill(Graphics g, int x, int y, int w, int h) {362g.fillRoundRect(x, y, w, h, 10, 10);363}364}365366367class PolygonShape extends Shape {368// class variables369370Polygon p;371Polygon pBase;372373public PolygonShape() {374pBase = new Polygon();375pBase.addPoint(0, 0);376pBase.addPoint(10, 0);377pBase.addPoint(5, 15);378pBase.addPoint(10, 20);379pBase.addPoint(5, 20);380pBase.addPoint(0, 10);381pBase.addPoint(0, 0);382}383384void scalePolygon(float w, float h) {385p = new Polygon();386for (int i = 0; i < pBase.npoints; ++i) {387p.addPoint((int) (pBase.xpoints[i] * w),388(int) (pBase.ypoints[i] * h));389}390391}392393@Override394void draw(Graphics g, int x, int y, int w, int h) {395Graphics ng = g.create();396try {397ng.translate(x, y);398scalePolygon(((float) w / 10f), ((float) h / 20f));399ng.drawPolygon(p);400} finally {401ng.dispose();402}403}404405@Override406void fill(Graphics g, int x, int y, int w, int h) {407Graphics ng = g.create();408try {409ng.translate(x, y);410scalePolygon(((float) w / 10f), ((float) h / 20f));411ng.fillPolygon(p);412} finally {413ng.dispose();414}415}416}417418419@SuppressWarnings("serial")420class ShapeTest extends Panel {421422Shape shape;423int step;424425public ShapeTest(Shape shape, int step) {426this.shape = shape;427this.step = step;428}429430public ShapeTest(Shape shape) {431this(shape, 10);432}433434@Override435public void paint(Graphics g) {436Rectangle bounds = getBounds();437438int cx, cy, cw, ch;439440Color color;441442for (color = Color.red, cx = bounds.x, cy = bounds.y,443cw = bounds.width / 2, ch = bounds.height;444cw > 0 && ch > 0;445cx += step, cy += step, cw -= (step * 2), ch -= (step * 2),446color = ColorUtils.darker(color, 0.9)) {447g.setColor(color);448shape.draw(g, cx, cy, cw, ch);449}450451for (cx = bounds.x + bounds.width / 2, cy = bounds.y,452cw = bounds.width / 2, ch = bounds.height;453cw > 0 && ch > 0;454cx += step, cy += step, cw -= (step * 2), ch -= (step * 2)) {455if (g.getColor() == Color.red) {456g.setColor(Color.blue);457} else {458g.setColor(Color.red);459}460461shape.fill(g, cx, cy, cw, ch);462} // end for463} // end paint()464} // end class ShapeTest465466467class ColorUtils {468469static Color brighter(Color c, double factor) {470return new Color(Math.min((int) (c.getRed() * (1 / factor)), 255),471Math.min((int) (c.getGreen() * (1 / factor)), 255),472Math.min((int) (c.getBlue() * (1 / factor)), 255));473}474475static Color darker(Color c, double factor) {476return new Color(Math.max((int) (c.getRed() * factor), 0),477Math.max((int) (c.getGreen() * factor), 0),478Math.max((int) (c.getBlue() * factor), 0));479}480}481482483