Java Tutorial/2D Graphics/TexturePaint — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:25, 31 мая 2010
A texture is a bitmap image applied to a shape
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);
}
}
TexturePaint with BufferedImage
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextureWithBufferedImage extends JFrame {
public TextureWithBufferedImage() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String arg[]) {
new TextureWithBufferedImage();
}
}
class DrawingCanvas extends JPanel {
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Rectangle2D rec1, rec2, rec3, rec4, rec5;
rec1 = new Rectangle2D.Float(25, 25, 75, 150);
rec2 = new Rectangle2D.Float(125, 25, 10, 75);
rec3 = new Rectangle2D.Float(75, 125, 125, 75);
rec4 = new Rectangle2D.Float(25, 15, 12, 75);
rec5 = new Rectangle2D.Float(15, 50, 15, 15);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1);
g2D.setComposite(ac);
g2D.setStroke(new BasicStroke(5.0f));
g2D.draw(rec1);
GradientPaint gp = new GradientPaint(125f, 25f, Color.yellow, 225f, 100f,
Color.blue);
g2D.setPaint(gp);
g2D.fill(rec2);
BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.setColor(Color.magenta);
big.fillRect(0, 0, 5, 5);
big.setColor(Color.black);
big.drawLine(0, 0, 5, 5);
Rectangle r = new Rectangle(0, 0, 5, 5);
TexturePaint tp = new TexturePaint(bi, r);
g2D.setPaint(tp);
g2D.fill(rec3);
g2D.setColor(Color.green);
g2D.fill(rec4);
g2D.setColor(Color.red);
g2D.fill(rec5);
}
}
TexturePaint with GIF image
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame {
public MainClass() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String arg[]) {
new MainClass();
}
}
class DrawingCanvas extends JPanel {
Font font = new Font("Dialog", Font.BOLD, 40);
FontMetrics fontMetrics;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
fontMetrics = getFontMetrics(font);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Image displayImage = getToolkit().getImage("c:\your.gif");
if (displayImage != null) {
BufferedImage bi = new BufferedImage(
displayImage.getWidth(this),
displayImage.getHeight(this),
BufferedImage.TYPE_INT_RGB);
bi.createGraphics().drawImage(displayImage, 0, 0, this);
Rectangle2D rectangle = new Rectangle2D.Float(0, 0,
displayImage.getWidth(this),
displayImage.getHeight(this));
TexturePaint tp = new TexturePaint(bi, rectangle);
g2D.setPaint(tp);
g2D.fill(new Rectangle2D.Float(0, 0, getWidth(), getHeight()));
}
}
}