Java/2D Graphics GUI/Texture
Содержание
A texture is a bitmap image applied to a shape
<source lang="java"> import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.TexturePaint; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class Textures extends JPanel {
BufferedImage s; public Textures() { try { s = ImageIO.read(this.getClass().getResource("s.png")); } catch (IOException e) { e.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; TexturePaint slatetp = new TexturePaint(s, new Rectangle(0, 0, 90, 60)); g2d.setPaint(slatetp); g2d.fillRect(10, 15, 90, 60); } public static void main(String[] args) { JFrame frame = new JFrame("Textures"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Textures()); frame.setSize(360, 120); frame.setVisible(true); }
}
</source>
A texture is a bitmap image applied to the surface in computer graphics.
<source lang="java"> import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.TexturePaint; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class Textures extends JPanel {
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(new Color(212, 212, 212)); g2d.drawRect(10, 15, 90, 60); BufferedImage bimage1 = null; URL url1 = ClassLoader.getSystemResource("a.png"); try { bimage1 = ImageIO.read(url1); } catch (IOException ioe) { ioe.printStackTrace(); } Rectangle rect1 = new Rectangle(0, 0, bimage1.getWidth(), bimage1.getHeight()); TexturePaint texture1 = new TexturePaint(bimage1, rect1); g2d.setPaint(texture1); g2d.fillRect(10, 15, 90, 60); } public static void main(String[] args) { Textures rects = new Textures(); JFrame frame = new JFrame("Textures"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(rects); frame.setSize(360, 210); frame.setLocationRelativeTo(null); frame.setVisible(true); }
}
</source>
Draw text along a curve
<source lang="java"> import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import javax.swing.JFrame; import javax.swing.JPanel; public class RollingText extends JPanel {
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); String s = "Java Source and Support."; Font font = new Font("Serif", Font.PLAIN, 24); FontRenderContext frc = g2.getFontRenderContext(); g2.translate(40, 80); GlyphVector gv = font.createGlyphVector(frc, s); int length = gv.getNumGlyphs(); for (int i = 0; i < length; i++) { Point2D p = gv.getGlyphPosition(i); double theta = (double) i / (double) (length - 1) * Math.PI / 4; AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY()); at.rotate(theta); Shape glyph = gv.getGlyphOutline(i); Shape transformedGlyph = at.createTransformedShape(glyph); g2.fill(transformedGlyph); } } public static void main(String[] args) { JFrame f = new JFrame("RollingText v1.0"); f.getContentPane().add(new RollingText()); f.setSize(600, 300); f.setVisible(true); }
}
</source>
Set Text Attribute
<source lang="java"> import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.font.FontRenderContext; import java.awt.font.GraphicAttribute; import java.awt.font.ImageGraphicAttribute; import java.awt.font.ShapeGraphicAttribute; import java.awt.font.TextAttribute; import java.awt.font.TextLayout; import java.awt.geom.GeneralPath; import java.awt.image.BufferedImage; import java.text.AttributedCharacterIterator; import java.text.AttributedString; import javax.swing.JFrame; import javax.swing.JPanel; public class AttributesApp extends JPanel {
String text = "Java Source and Support"; AttributedString attribString; AttributedCharacterIterator attribCharIterator; Image image; public static void main(String arg[]) { JFrame frame = new JFrame(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.getContentPane().add("Center", new AttributesApp()); frame.setSize(500, 200); frame.setVisible(true); } public AttributesApp() { setBackground(Color.lightGray); setSize(500, 200); attribString = new AttributedString(text); GeneralPath star = new GeneralPath(); star.moveTo(0, 0); star.lineTo(10, 30); star.lineTo(-10, 10); star.lineTo(10, 10); star.lineTo(-10, 30); star.closePath(); GraphicAttribute starShapeAttr = new ShapeGraphicAttribute(star, GraphicAttribute.TOP_ALIGNMENT, false); attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT, starShapeAttr, 0, 1); attribString.addAttribute(TextAttribute.FOREGROUND, new Color(255, 255, 0), 0, 1); int index = text.indexOf("Java Source"); attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue, index, index + 7); Font font = new Font("sanserif", Font.ITALIC, 40); attribString.addAttribute(TextAttribute.FONT, font, index, index + 7); loadImage(); BufferedImage bimage = new BufferedImage(image.getWidth(this), image .getHeight(this), BufferedImage.TYPE_INT_ARGB); Graphics2D big = bimage.createGraphics(); big.drawImage(image, null, this); GraphicAttribute javaImageAttr = new ImageGraphicAttribute(bimage, GraphicAttribute.TOP_ALIGNMENT, 0, 0); index = text.indexOf("Java"); attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT, javaImageAttr, index - 1, index); font = new Font("serif", Font.BOLD, 60); attribString.addAttribute(TextAttribute.FONT, font, index, index + 4); attribString.addAttribute(TextAttribute.FOREGROUND, new Color(243, 63, 163), index, index + 4); // Start and end indexes. index = text.indexOf("source"); attribString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, index, index + 2); index = text.indexOf("and support"); font = new Font("sanserif", Font.ITALIC, 40); attribString.addAttribute(TextAttribute.FONT, font, index, index + 10); attribString.addAttribute(TextAttribute.FOREGROUND, Color.white, index, index + 2); // Start and end indexes. attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue, index + 3, index + 10); // Start and end indexes. } public void loadImage() { image = Toolkit.getDefaultToolkit().getImage("largejexpLogo.gif"); MediaTracker mt = new MediaTracker(this); mt.addImage(image, 1); try { mt.waitForAll(); } catch (Exception e) { System.out.println("Exception while loading."); } if (image.getWidth(this) == -1) { System.out.println("no images"); System.exit(0); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; attribCharIterator = attribString.getIterator(); FontRenderContext frc = new FontRenderContext(null, false, false); TextLayout layout = new TextLayout(attribCharIterator, frc); layout.draw(g2, 20, 100); }
}
</source>
Text effect: transparent
<source lang="java"> import java.awt.AlphaComposite; import java.awt.Color; import java.awt.ruposite; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class TransparentText extends JPanel {
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // the rendering quality. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // a red rectangle. Rectangle2D r = new Rectangle2D.Double(50, 50, 550, 100); g2.setPaint(Color.red); g2.fill(r); // a composite with transparency. Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f); g2.setComposite(c); // Draw some blue text. g2.setPaint(Color.blue); g2.setFont(new Font("Times New Roman", Font.PLAIN, 72)); g2.drawString("Java Source and Support", 25, 130); } public static void main(String[] args) { JFrame f = new JFrame(); f.getContentPane().add(new TransparentText()); f.setSize(800, 250); f.show(); }
}
</source>
TexturePaint Demo
<source lang="java"> import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.TexturePaint; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; public class TexturePaintDemo extends JPanel {
public void init() { setBackground(Color.white); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); Graphics2D big = bi.createGraphics(); big.setColor(Color.blue); big.fillRect(0, 0, 5, 5); big.setColor(Color.lightGray); big.fillOval(0, 0, 5, 5); Rectangle r = new Rectangle(0, 0, 5, 5); g2.setPaint(new TexturePaint(bi, r)); Rectangle rect = new Rectangle(5,5,200,200); g2.fill(rect); } public static void main(String s[]) { JFrame f = new JFrame(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); TexturePaintDemo p = new TexturePaintDemo(); f.getContentPane().add("Center", p); p.init(); f.pack(); f.setSize(new Dimension(250, 250)); f.show(); }
}
</source>
Text with a Texture
<source lang="java"> /*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002. * All rights reserved. Software written by Ian F. Darwin and others. * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS"" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee * cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s, * pioneering role in inventing and promulgating (and standardizing) the Java * language and environment is gratefully acknowledged. * * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for * inventing predecessor languages C and C++ is also gratefully acknowledged. */
import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.TexturePaint; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import javax.swing.JComponent; /**
* Text with a Texture * * @author Ian Darwin, http://www.darwinsys.ru/ * @version $Id: TexturedText.java,v 1.5 2004/02/09 03:33:49 ian Exp $ */
public class TexturedText extends JComponent {
/** The image we draw in the texture */ protected BufferedImage bim; /** The texture for painting. */ TexturePaint tp; /** The string to draw. */ String mesg = "Stripey"; /** The font */ Font myFont = new Font("Lucida Regular", Font.BOLD, 72); /** "main program" method - construct and show */ public static void main(String[] av) { // create a TexturedText object, tell it to show up final Frame f = new Frame("TexturedText"); TexturedText comp = new TexturedText(); f.add(comp); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { f.setVisible(false); f.dispose(); System.exit(0); } }); f.pack(); f.setLocation(200, 200); f.setVisible(true); } protected static Color[] colors = { Color.red, Color.blue, Color.yellow, }; /** Construct the object */ public TexturedText() { super(); setBackground(Color.white); int width = 8, height = 8; bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bim.createGraphics(); for (int i = 0; i < width; i++) { g2.setPaint(colors[(i / 2) % colors.length]); g2.drawLine(0, i, i, 0); g2.drawLine(width - i, height, width, height - i); } Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight()); tp = new TexturePaint(bim, r); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(tp); g2.setFont(myFont); g2.drawString(mesg, 20, 100); } public Dimension getMinimumSize() { return new Dimension(250, 100); } public Dimension getPreferredSize() { return new Dimension(320, 150); }
}
</source>