Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/TextureChooser.java
41154 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d;323334import static java.awt.Color.GRAY;35import static java.awt.Color.GREEN;36import static java.awt.Color.LIGHT_GRAY;37import static java.awt.Color.WHITE;38import java.awt.BasicStroke;39import java.awt.Color;40import java.awt.Component;41import java.awt.Dimension;42import java.awt.Font;43import java.awt.Frame;44import java.awt.GradientPaint;45import java.awt.Graphics;46import java.awt.Graphics2D;47import java.awt.GridLayout;48import java.awt.Image;49import java.awt.Rectangle;50import java.awt.TexturePaint;51import java.awt.event.MouseAdapter;52import java.awt.event.MouseEvent;53import java.awt.event.WindowAdapter;54import java.awt.event.WindowEvent;55import java.awt.font.FontRenderContext;56import java.awt.font.TextLayout;57import java.awt.geom.Ellipse2D;58import java.awt.image.BufferedImage;59import javax.swing.JPanel;60import javax.swing.border.EtchedBorder;61import javax.swing.border.TitledBorder;626364/**65* Four types of Paint displayed: Geometry, Text & Image Textures and66* a Gradient Paint. Paints can be selected with the Mouse.67*/68@SuppressWarnings("serial")69public final class TextureChooser extends JPanel {70private final DemoInstVarsAccessor demoInstVars;71public Object texture = getGeomTexture();72public int num;7374public TextureChooser(int num, DemoInstVarsAccessor demoInstVars) {75this.num = num;76this.demoInstVars = demoInstVars;7778setLayout(new GridLayout(0, 2, 5, 5));79setBorder(new TitledBorder(new EtchedBorder(), "Texture Chooser"));8081add(new Surface(getGeomTexture(), this, 0));82add(new Surface(getImageTexture(), this, 1));83add(new Surface(getTextTexture(), this, 2));84add(new Surface(getGradientPaint(), this, 3));85}8687public static TexturePaint getGeomTexture() {88BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);89Graphics2D tG2 = bi.createGraphics();90tG2.setBackground(WHITE);91tG2.clearRect(0, 0, 5, 5);92tG2.setColor(new Color(211, 211, 211, 200));93tG2.fill(new Ellipse2D.Float(0, 0, 5, 5));94Rectangle r = new Rectangle(0, 0, 5, 5);95return new TexturePaint(bi, r);96}9798public TexturePaint getImageTexture() {99Image img = DemoImages.getImage("globe.png", this);100int sw = img.getWidth(this);101int sh = img.getHeight(this);102int iw = sw/5;103int ih = sh/5;104BufferedImage bi =105new BufferedImage(iw, ih, BufferedImage.TYPE_INT_ARGB);106Graphics2D tG2 = bi.createGraphics();107tG2.drawImage(img, 0, 0, iw, ih, 0, 0, sw, sh, this);108Rectangle r = new Rectangle(0, 0, iw, ih);109return new TexturePaint(bi, r);110}111112public TexturePaint getTextTexture() {113Font f = new Font(Font.SERIF, Font.BOLD, 10);114TextLayout tl = new TextLayout("OpenJDK", f, new FontRenderContext(null,115false, false));116int sw = (int) tl.getBounds().getWidth();117int sh = (int) (tl.getAscent() + tl.getDescent());118BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);119Graphics2D tG2 = bi.createGraphics();120tG2.setBackground(WHITE);121tG2.clearRect(0, 0, sw, sh);122tG2.setColor(LIGHT_GRAY);123tl.draw(tG2, 0, tl.getAscent());124Rectangle r = new Rectangle(0, 0, sw, sh);125return new TexturePaint(bi, r);126}127128public GradientPaint getGradientPaint() {129return new GradientPaint(0, 0, WHITE, 80, 0, GREEN);130}131132133public class Surface extends JPanel {134135public boolean clickedFrame;136private int num;137private TextureChooser tc;138private boolean enterExitFrame = false;139private Object t;140141public Surface(final Object t, final TextureChooser tc, int num) {142setBackground(WHITE);143this.t = t;144this.tc = tc;145this.clickedFrame = (num == tc.num);146this.num = num;147if (num == tc.num) {148tc.texture = t;149}150addMouseListener(new MouseAdapter() {151152@Override153public void mouseClicked(MouseEvent e) {154tc.texture = t;155clickedFrame = true;156157for (Component comp : tc.getComponents()) {158if (comp instanceof Surface) {159Surface surf = (Surface) comp;160if (!surf.equals(Surface.this) && surf.clickedFrame) {161surf.clickedFrame = false;162surf.repaint();163}164}165}166167// ABP168if (demoInstVars.getControls().textureCB.isSelected()) {169demoInstVars.getControls().textureCB.doClick();170demoInstVars.getControls().textureCB.doClick();171}172}173174@Override175public void mouseEntered(MouseEvent e) {176enterExitFrame = true;177repaint();178}179180@Override181public void mouseExited(MouseEvent e) {182enterExitFrame = false;183repaint();184}185});186}187188@Override189public void paintComponent(Graphics g) {190super.paintComponent(g);191Graphics2D g2 = (Graphics2D) g;192int w = getSize().width;193int h = getSize().height;194if (t instanceof TexturePaint) {195g2.setPaint((TexturePaint) t);196} else {197g2.setPaint((GradientPaint) t);198}199g2.fill(new Rectangle(0, 0, w, h));200if (clickedFrame || enterExitFrame) {201g2.setColor(GRAY);202BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,203BasicStroke.JOIN_MITER);204g2.setStroke(bs);205g2.drawRect(0, 0, w - 1, h - 1);206tc.num = num;207}208}209210@Override211public Dimension getMinimumSize() {212return getPreferredSize();213}214215@Override216public Dimension getMaximumSize() {217return getPreferredSize();218}219220@Override221public Dimension getPreferredSize() {222return new Dimension(30, 30);223}224}225226public static void main(String[] s) {227Frame f = new Frame("J2D Demo - TextureChooser");228f.addWindowListener(new WindowAdapter() {229230@Override231public void windowClosing(WindowEvent e) {232System.exit(0);233}234});235f.add("Center", new TextureChooser(0, new DemoInstVarsAccessorImplBase()));236f.pack();237f.setSize(new Dimension(400, 400));238f.setVisible(true);239}240}241242243